views:

366

answers:

3

Hi I would like to know if there is any way in Java to reduce the size of an image (use any kind of compression) that was loaded as a BufferedImage and is going to be saved as an PNG.

Maybe some sort of png imagewriteparam? I didnt find anything helpful so im stuck.

heres a sample how the image is loaded and saved

public static BufferedImage load(String imageUrl) {         
    Image image = new ImageIcon(imageUrl).getImage();
    bufferedImage = new BufferedImage(image.getWidth(null),
                                                    image.getHeight(null),
                                                    BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2D = bufferedImage.createGraphics();
    g2D.drawImage(image, 0, 0, null);
    return bufferedImage;
}

public static void storeImageAsPng(BufferedImage image, String imageUrl) throws IOException {
    ImageIO.write(image, "png", new File(imageUrl));
}
+1  A: 

Have a look at the ImageWriterParam class in the same package as the ImageIO class. It mentions compression.

http://java.sun.com/j2se/1.4.2/docs/api/javax/imageio/ImageWriteParam.html

Also look at the example at http://www.exampledepot.com/egs/javax.imageio/JpegWrite.html and see if it translates well for PNG files.

aioobe
A: 

Please refer http://forums.sun.com/thread.jspa?threadID=5436752

Sujal Shelke
From that very post: "Problem is i want to reduce the size of a PNG image which is of 2000X3000 pixels to 150X200 pixels" I don't believe that is related to this issue. Correct me if I'm wrong.
aioobe
A: 

If it's going to be saved as PNG, compression will be done at that stage. PNG has a lossless compression algorithm (basically prediction followed by lempel-ziv compression) with few adjustable parameters (types of "filters") and not much impact in compression amount - in general the default will be optimal.

leonbloy
From what I've read about gimps png-save options, the compression level (a setting from 1 to 9) determines the compression (effort of the encoder), although it is loss-less no matter what you set.
aioobe
Yes, its just a matter of choosing one of the four 'filters' (actually predictors, see above link) to use, perhaps using distinct filters for different lines. It always is lossless.
leonbloy