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);

--

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


Reply via email to