在 Java 中保存到磁盘之前压缩图像
是否可以在保存图像之前对其进行压缩? 我正在使用 Robot 类来捕获图像,它返回一个 BufferedImage。 如何压缩该图像然后保存?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
.png
文件是(无损)压缩图像。您可以使用 ImageIO.write() 到 保存 .png 图像:
.png
files are (losslessly) compressed images.You can use ImageIO.write() to save a .png image:
有颜色压缩(“压缩质量”)和分辨率压缩(“调整大小”)。例如,我使用非常低的“压缩质量”将 4mb 的照片压缩到 270K,但这看起来很糟糕,但我使用合理的质量但较小的尺寸将其压缩到 12K。
我对调整大小的建议:
https:/ /github.com/rkalla/imgscalr/blob/master/src/main/java/org/imgscalr/Scalr.java
我希望调整大小是标准 Java 库的一部分,但似乎不是,(或者那里标准方法是否存在图像质量问题?)。但是Riyad的图书馆真的很小——只有一个班级。我只是将这个类复制到我的项目中,因为我从未学习过如何使用 Maven,而且它效果很好。
如何使用 java 压缩图像?
我对压缩质量的建议:请参阅fujy的答案:
如何使用java压缩图像?
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?