Compress Image before Saving to disk in Java

谁都会走 提交于 2019-12-07 20:16:25

问题


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?


回答1:


.png files are (losslessly) compressed images.

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

ImageIO.write(myBufferedImage, "png", outputfile);



回答2:


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?



来源:https://stackoverflow.com/questions/5678493/compress-image-before-saving-to-disk-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!