Image Generation

13 views
Skip to first unread message

Naveen

unread,
Nov 20, 2009, 6:22:02 AM11/20/09
to Google Web Toolkit

Hi all,

Is there a way to display the stream of bytes passed from the server
as an image in GWT.

in AWT we use the tool kit to construct the image..

this is really urgent and i am struck up here.. cannot move further

Any help much appreciated...

Regards,
Naveen

Naveen

unread,
Nov 20, 2009, 4:21:31 AM11/20/09
to Google Web Toolkit
HI..

Greetings...

i am using GWT for one of my application development and found a
problem where i cannot proceed further.

I need to send a stream of bytes from the sever to the GWT client and
create an image using the response in client side. I had tried my best
but cannot achieve this.

i am struck up with this and cannot proceed further

Any help is much appreciated..

Thanks,
Navee

Brendan

unread,
Nov 21, 2009, 5:32:45 PM11/21/09
to Google Web Toolkit
well the first thing you should do, of course, is to see if you can
make the conversion on the server. If it's already sending you the
string of bytes, maybe it can decide what to do with it and then
output an image to download.

If that's not possible (if the client really needs to alter the data,
for instance), you could always do a roundtrip back to the server and
have it generate an image.

Finally, there's canvas. Canvas and its createImageData() method are
not widely supported (in terms of marketshare), but should be able to
do what you need to do.

Here's a project someone did that's really the reverse of what you
want to do, but should give you a good lead:
http://blog.nihilogic.dk/2008/05/compression-using-canvas-and-png.html

bryanb

unread,
Nov 22, 2009, 2:58:19 AM11/22/09
to Google Web Toolkit
Something like this works for me.

On the server:

import java.awt.Container;
import java.awt.Graphics2D;
import java.awt.MediaTracker;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ImageServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse
response) {
double thumbWidth;
double thumbHeight;

String imagefile = request.getParameter("file");
String height = request.getParameter("h");
String width = request.getParameter("w");

// System.err.println(imagefile + ", " + height + ", " + width);

// Set the mime type of the image
response.setContentType("image/png");

try {

java.awt.Image image = Toolkit.getDefaultToolkit().getImage
(imagefile);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);

int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double) imageWidth / (double) imageHeight;

// if width parameter is null, then just use it to scale
thumbHeight = new Double(height);

if (width != null) {
thumbWidth = new Double(width);
// determine size from WIDTH and HEIGHT
double thumbRatio = thumbWidth / thumbHeight;

if (thumbRatio < imageRatio) {
thumbHeight = thumbWidth / imageRatio;
} else {
thumbWidth = thumbHeight * imageRatio;
}
} else {
thumbWidth = thumbHeight * imageRatio;
}

// draw original image to thumbnail image object and scale it to
the new size on-the-fly
BufferedImage thumbImage = new BufferedImage((int) thumbWidth,
(int) thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, (int) thumbWidth, (int)
thumbHeight, null);
graphics2D.dispose();

ImageIO.write(thumbImage, "png", response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();

}
}
}


On the client, create a URL to get the image something like this:

StringBuffer sb = new StringBuffer(GWT.getHostPageBaseURL());
sb.append("ImageServlet?file=");
sb.append(imagefilel);
sb.append("&h=" + Utils.getPageHeight());
sb.append("&w=" + Utils.getPageWidth());
Image image = new Image(sb.toString);

naveen krishnan

unread,
Nov 24, 2009, 12:18:02 AM11/24/09
to google-we...@googlegroups.com
HI,

Tried the same but it was not working...

it gives me an blank image... Please help me out...

try {

                java.awt.Image image = rr.toImage();// An image given by the service

                MediaTracker mediaTracker = new MediaTracker(new Container());
                mediaTracker.addImage(image, 0);
                mediaTracker.waitForID(0);

                BufferedImage bImage = new BufferedImage(698,
                                 440, BufferedImage.TYPE_INT_RGB);
                Graphics2D graphics2D = bImage.createGraphics();
                graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                graphics2D.drawImage(image, 0, 0, (int) 698, (int)
                                            440, null);
                graphics2D.dispose();

                ImageIO.write(bImage, "gif", res.getOutputStream());
        } catch (Exception e) {
                e.printStackTrace();

        }

Here rr is an external service object which gives me an image...

At the client side i have called this servlet...

Regards,
Naveen


--

You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=.


Reply all
Reply to author
Forward
0 new messages