views:

449

answers:

4

Hi ,

I am doing a web project on media manager. I upload high resolution images and need to convert these images to low resolution, so that the resulting image is compressed into a smaller size. How is this possible using Java?

Please post source code, if possible.

Thanks, Naveen

A: 

I've seen JAI used for this purpose.

Dan
JAI as Dan suggested is probably your best option if it is practical, JAI is not always available and depending on your deployment requirements/runtime constraints you may not be able to rely on it.
vickirk
A: 

You could add an parameter onto your image reader if you don't need to do anything with the high-res image, e.g.

ImageReadParam param = reader.getDefaultReadParam();
param.setSourceSubsampling(4, 4, 0, 0);
img = reader.read(0);

This will skip 3 out of 4 rows/column in the source data.

vickirk
+1  A: 

not sure if I got it right, but if you want to change the size of an image, the Image class has a getScaledInstance method:

Image theImage = ...
Image scaled = theImage.getScaledInstance(32, -1, Image.SCALE_FAST);

For details see the documentation of Image

Carlos Heuberger
naveen may also need to read/write the image to a JPG or PNG.
Suppressingfire
which, BTW can be done with IamgeIO: http://java.sun.com/javase/6/docs/api/javax/imageio/ImageIO.html
Suppressingfire
Depending on your requirements, you may be better off with Image.SMOOTH than Image.SCALE_FAST. Otherwise, good call.
markusk
it's just an example... same for the size of 32 pixel... kind of hard without knowing all details.
Carlos Heuberger
A: 

If you're running on a Linux server with the imagemagick binaries installed, you could just do an Runtime.getRuntime.exec() on /usr/bin/convert. Just make sure you read/write to unique names (you can use File.createTempFile() to ensure that).

Suppressingfire