Compress Image before Saving to disk in Java

安稳与你 提交于 2019-12-06 09:21:27

.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?

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