Compress Image before Saving to disk in Java
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?
.png
files are (losslessly) compressed images.
You can use ImageIO.write() to save a .png image:
ImageIO.write(myBufferedImage, "png", outputfile);
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?
精彩评论