如何将 BufferedImage 编写为不压缩的 PNG? [英] How do I write a BufferedImage as a PNG with no compression?

查看:103
本文介绍了如何将 BufferedImage 编写为不压缩的 PNG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 BufferedImage 编写为 .png 格式而不执行压缩.我环顾四周,得出以下代码.

I need to write a BufferedImage as a .png with no compression performed. I've looked around, and come up with the following code.

public void save(String outFilePath) throws IOException {
    Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("png");
    ImageWriter writer = iter.next();

    File file = new File(outFilePath);      
    ImageOutputStream ios = ImageIO.createImageOutputStream(file);
    writer.setOutput(ios);

    ImageWriteParam iwp = writer.getDefaultWriteParam();
    iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    iwp.setCompressionQuality(1.0f);

    IIOImage image = new IIOImage(mapImage, null, null);

    writer.write(null, image, iwp);
    writer.dispose();

    //ImageIO.write(mapImage, "png", file);
}

这是抛出的异常.

Exception in thread "main" java.lang.UnsupportedOperationException: Compression not supported.
    at javax.imageio.ImageWriteParam.setCompressionMode(Unknown Source)
    at Map.MapTransformer.save(MapTransformer.java:246)
    at Map.MapTransformer.main(MapTransformer.java:263)

推荐答案

PNG 图像通过首先应用预测来实现压缩 filter(您可以在五个变体中进行选择),然后使用 ZLIB 压缩预测误差.这两个步骤你不能省略,你可以做的是指定NONE"作为预测过滤器,压缩级别=0为ZLIB压缩,大致对应于未压缩的图像.javax.imageio.* 包不允许(我认为)选择这个参数,也许你可以试试 thisthis>

PNG images achieve compression by first applying a prediction filter (you can choose among five variants), and then compressing the prediction error with ZLIB. You cannot omit these two steps, what you can do is to specify "NONE" as prediction filter, and compressionLevel=0 for the ZLIB compression, which would roughly correspond to a non-compressed image. The javax.imageio.* package does not allow (I think) to select this parameters, perhaps you can try with this or this

这篇关于如何将 BufferedImage 编写为不压缩的 PNG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆