3

Is it possible to compress an image before saving it? I'm using the Robot class to capture images, and it returns a BufferedImage. How can I compress this image and then save it?

knurdy
  • 496
  • 6
  • 18
  • 1
    Images are generally compressed pretty well anyway if you store them in JPG or PNG format. The advantage being they are ready to use from a multitude of applications. – trojanfoe Apr 15 '11 at 14:53

2 Answers2

3

.png files are (losslessly) compressed images.

You can use ImageIO.write() to save a .png image:

ImageIO.write(myBufferedImage, "png", outputfile);
BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
0

There is colour compression ("compression quality") and there is resolution compression ("resizing"). E.g. I got a 4mb photo to 270K using a very low "compression quality", but that looked awful, but I got it down to 12K using a reasonable quality but a smaller size.

My recommendation for resizing:

https://github.com/rkalla/imgscalr/blob/master/src/main/java/org/imgscalr/Scalr.java

I wish resizing was part of the standard Java libraries, but it seems it's not, (or there are image quality problems with the standard methods?). But Riyad's library is really small - it's just one class. I just copied this class into my project, because I never learnt how to use Maven, and it works great. How can I compress images using java?

My recommendation for compression quality: See fujy's answer in:

How can I compress images using java?

Tim Cooper
  • 10,023
  • 5
  • 61
  • 77