如何使用java的ImageIO保存优化后的png图像?

发布于 2024-08-29 22:01:34 字数 370 浏览 4 评论 0原文

我在 java 中生成大量图像并通过 ImageIO.write 方法保存它们,如下所示:

final BufferedImage img = createSomeImage();
ImageIO.write( img, "png", new File( "/some/file.png" );

我对结果很满意,直到 Google 的 firefox 插件“Page Speed”告诉我最多可以保存 60 个图像如果我优化图像,则占大小的%。图片是二维码,每个大小约为900B,firefox插件优化版本约为300B。 我想直接从java保存这样优化的300B图像。

所以我的问题又来了:如何使用java的ImageIO保存优化的png图像?

I am generating lots of images in java and saving them through the ImageIO.write method like this:

final BufferedImage img = createSomeImage();
ImageIO.write( img, "png", new File( "/some/file.png" );

I was happy with the results until Google's firefox addon 'Page Speed' told me that i can save up to 60% of the size if i optimize the images. The images are QR codes, their size is around 900B each and the firefox-plugin optimized versions are around 300B.
I'd like to save such optimized 300B Images directly from java.

So here my question again: How to save optimized png images with java's ImageIO?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮世清欢 2024-09-05 22:01:34

使用 PngEncoderB 将 BufferedImage 转换为 PNG 编码的字节数组。

您可以对其应用滤镜,这有助于准备图像以进行更好的优化。这就是 OptiPNG 所做的,只有 OptiPNG 计算哪个过滤器将为您提供最佳压缩效果。

您可能需要尝试应用每个过滤器,看看哪一个始终更适合您。对于 2 位颜色,我认为唯一可能有帮助的过滤器是“向上”,所以我猜这就是要使用的过滤器。

将图像获取到 PNG 编码的字节数组后,您可以将其直接写入文件。

Use PngEncoderB to convert your BufferedImage into a PNG encoded byte array.

You can apply a filter to it, which helps prepare the image for better optimization. This is what OptiPNG does, only OptiPNG calculates which filter will get you the best compression.

You might have to try applying each filter to see which one is consistently better for you. With 2 bit color, I think the only filter that might help is "up", so I'm guessing that's the one to use.

Once you get the image to a PNG encoded byte array, you can write that directly to a file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文