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

查看:268
本文介绍了如何在不压缩的情况下将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图片通过首先应用预测来实现压缩过滤器(您可以在五个变体中进行选择),然后使用ZLIB压缩预测误差.您不能省略这两个步骤,您可以做的是将"NONE"指定为预测过滤器,将ZLIB压缩指定为compressionLevel = 0,这将大致对应于未压缩的图像. javax.imageio.*包不允许(我认为)选择此参数,也许您可​​以尝试使用

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天全站免登陆