Image-resize servlet

Hi,

I磛e been working on an image-resizing servlet on and off for quite some time now, and I just cant seem to get it to load the image correctly.

I guess its very simple once you figure it out. The following is the interesting part:

Image image = Toolkit.getDefaultToolkit().getImage("../pictures/pic" + pictureId +".jpg");

MediaTracker tracker =new MediaTracker(new JFrame() );

tracker.addImage(image, 1);

// Load image

try{

tracker.waitForAll();

}catch (InterruptedException e){}

// Scale image

image = image.getScaledInstance(width, height, Image.SCALE_DEFAULT);

I then copy the Graphics from the Image to another Graphics before it is sent to the client (have some cache-stuff going on in the middle), but all images turn out beeing plain black.

Anyone with ideas/sample code?

[1227 byte] By [Flaperman] at [2007-9-27 13:55:25]
# 1

hi flaperman

i did something similar in my resizing servlet:

BufferedImage image = ImageIO.read(new File(path));

Image im = image.getScaledInstance(width, height, Image.SCALE_FAST);

BufferedImage thumbnail = toBufferedImage(im);

ImageIO.write(thumbnail, "jpeg", response.getOutputStream());

i found the code of the method toBufferedImage() on the following page http://developers.sun.com/solaris/tech_topics/java/articles/awt.html

maybe this will help you. but i'm afraid it is slow andit seems to be leaking memory (a lot).

cheers

gresli at 2007-7-5 21:44:36 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Here is a servlet I wrote that does image scaling. http://www.bacman.net/apps/thumbnailer/Thumbnailer.java
BacMan at 2007-7-5 21:44:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...