Image resizing with a servlet

I'm trying to write a servlet that will take an image's location and then save 3 different sized versions of it.

I've looked at the example that Joe Bella went through on the sun developer network and i understand most of it, but i can't seem to compile it. Here's the code:

publicclass ResizeImageServletextends HttpServlet

{

private String imageDir ="";

publicfinalvoid init( ServletConfig config )throws ServletException

{

// No initialization necessary

}

publicfinalvoid doGet( HttpServletRequest req, HttpServletResponse res )

throws ServletException, IOException

{

// No difference to us if it's a get or a post.

this.doPost(req,res);

}

publicfinalvoid doPost( HttpServletRequest req, HttpServletResponse res )

throws ServletException, IOException

{

try

{

int targetWidth=0;

int targetHeight=0;

// Get a path to the image to resize.

// ImageIcon is a kluge to make sure the image is fully

// loaded before we proceed.

Image sourceImage =new ImageIcon(Toolkit.getDefaultToolkit().

getImage(req.getPathTranslated())).getImage();

// Calculate the target width and height

float scale = Float.parseFloat(req.getParameter("scale"))/100;

targetWidth = (int)(sourceImage.getWidth(null)*scale);

targetHeight = (int)(sourceImage.getHeight(null)*scale);

BufferedImage resizedImage = this.scaleImage

(sourceImage,targetWidth,targetHeight);

// Output the finished image straight to the response as a JPEG!

res.setContentType("image/jpeg");

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder

(res.getOutputStream());

encoder.encode(resizedImage);

}

catch(Exception e)

{

res.sendError(HttpServletResponse.SC_BAD_REQUEST);

}

}

private BufferedImage scaleImage(Image sourceImage,int width,int height)

{

ImageFilter filter =new ReplicateScaleFilter(width,height);

ImageProducer producer =new FilteredImageSource

(sourceImage.getSource(),filter);

Image resizedImage = Toolkit.getDefaultToolkit().createImage(producer);

return this.toBufferedImage(resizedImage);

}

private BufferedImage toBufferedImage(Image image)

{

image =new ImageIcon(image).getImage();

BufferedImage bufferedImage =new BufferedImage(image.getWidth(null)

,image.getHeight(null),BufferedImage.TYPE_INT_RGB);

Graphics g = bufferedImage.createGraphics();

g.setColor(Color.white);

g.fillRect(0,0,image.getWidth(null),image.getHeight(null));

g.drawImage(image,0,0,null);

g.dispose();

return bufferedImage;

}

}

I've tried importing java.awt.* and java.util.* but it still doesn't recognise almost all of the methods at compile.

What am i doing wrong?

Thanks in advance

[5038 byte] By [_toma_a] at [2007-10-2 1:25:21]
# 1
If you are asking questions about error messages, you need to post them here!Copy andpaste the full/exact text
A_Sailora at 2007-7-15 18:47:05 > top of Java-index,Java Essentials,Java Programming...
# 2

the main problem i'm having is that javax.servlet and javax.servlet.http are not installed on my machine - nor can i work out how to do so now i've downloaded them. So, i've rewritten it as a bean (as it doesn't really have to be a servlet anyway).

However, here are the errors i'm still getting:

ResizeImage.java:46: cannot find symbol

symbol : class BufferedImage

location: class resizeImage.ResizeImage

private BufferedImage scaleImage(Image sourceImage, int width, int height)

^

ResizeImage.java:56: cannot find symbol

symbol : class BufferedImage

location: class resizeImage.ResizeImage

private BufferedImage toBufferedImage(Image image)

^

ResizeImage.java:23: cannot find symbol

symbol : class ImageIcon

location: class resizeImage.ResizeImage

Image sourceImage = new ImageIcon(Toolkit.getDefaultToolkit().

^

ResizeImage.java:24: cannot find symbol

symbol : variable req

location: class resizeImage.ResizeImage

getImage(req.getPathTranslated())).getImage();

^

ResizeImage.java:27: cannot find symbol

symbol : variable req

location: class resizeImage.ResizeImage

float scale = Float.parseFloat(req.getParameter("scale"))/100;

^

ResizeImage.java:31: cannot find symbol

symbol : class BufferedImage

location: class resizeImage.ResizeImage

BufferedImage resizedImage = this.scaleImage

^

ResizeImage.java:35: cannot find symbol

symbol : variable res

location: class resizeImage.ResizeImage

res.setContentType("image/jpeg");

^

ResizeImage.java:36: cannot find symbol

symbol : class JPEGImageEncoder

location: class resizeImage.ResizeImage

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder

^

ResizeImage.java:37: cannot find symbol

symbol : variable res

location: class resizeImage.ResizeImage

(res.getOutputStream());

^

ResizeImage.java:36: cannot find symbol

symbol : variable JPEGCodec

location: class resizeImage.ResizeImage

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder

^

ResizeImage.java:42: cannot find symbol

symbol : variable HttpServletResponse

location: class resizeImage.ResizeImage

res.sendError(HttpServletResponse.SC_BAD_REQUEST);

^

ResizeImage.java:42: cannot find symbol

symbol : variable res

location: class resizeImage.ResizeImage

res.sendError(HttpServletResponse.SC_BAD_REQUEST);

^

ResizeImage.java:48: cannot find symbol

symbol : class ImageFilter

location: class resizeImage.ResizeImage

ImageFilter filter = new ReplicateScaleFilter(width,height);

^

ResizeImage.java:48: cannot find symbol

symbol : class ReplicateScaleFilter

location: class resizeImage.ResizeImage

ImageFilter filter = new ReplicateScaleFilter(width,height);

^

ResizeImage.java:49: cannot find symbol

symbol : class ImageProducer

location: class resizeImage.ResizeImage

ImageProducer producer = new FilteredImageSource

^

ResizeImage.java:49: cannot find symbol

symbol : class FilteredImageSource

location: class resizeImage.ResizeImage

ImageProducer producer = new FilteredImageSource

^

ResizeImage.java:58: cannot find symbol

symbol : class ImageIcon

location: class resizeImage.ResizeImage

image = new ImageIcon(image).getImage();

^

ResizeImage.java:59: cannot find symbol

symbol : class BufferedImage

location: class resizeImage.ResizeImage

BufferedImage bufferedImage = new BufferedImage(image.getWidth(null)

^

ResizeImage.java:59: cannot find symbol

symbol : class BufferedImage

location: class resizeImage.ResizeImage

BufferedImage bufferedImage = new BufferedImage(image.getWidth(null)

^

ResizeImage.java:60: cannot find symbol

symbol : variable BufferedImage

location: class resizeImage.ResizeImage

,image.getHeight(null),BufferedImage.TYPE_INT_RGB);

^

I know its an import error, i'm just not sure what i need to import

_toma_a at 2007-7-15 18:47:05 > top of Java-index,Java Essentials,Java Programming...
# 3

You need to read the doc for the classes you are trying to use.

Open Sun's java to the API page. On the lower left is a list of classes.

Open the ones for the "not found symbol"s and you'll see what package it is in.

I'll give you one: java.awt.image.BufferedImage

For this add:import java.awt.image.BufferedImage;

A_Sailora at 2007-7-15 18:47:05 > top of Java-index,Java Essentials,Java Programming...