如何使用Apache Commons-imaging压缩png文件

时间:2013-11-07 18:52:12

标签: java png apache-commons

我正在尝试使用Apache Commons-imaging读取/写入PNG。

文件名:big-buck-bunny-1080p.png(1920 * 1080)

尺寸:1,45 Mo(1 527 631个八位字节)

我正在使用java-image-scaling来缩放我的图像。

对于此测试,我尝试使用与源相同的宽度和高度缩放文件,并将结果写入png。

    BufferedImage image = ImageIO.read(fileIn);
    ResampleOpSingleThread resampleOp = new ResampleOpSingleThread(imageDimension.getWidth(), imageDimension.getHeight());
    resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Normal);

    BufferedImage resizedImage = resampleOp.filter(image, null);

    final Map<String, Object> writeParams = new HashMap<String, Object>();
    writeParams.put(PngConstants.PARAM_KEY_FORMAT, ImageFormat.IMAGE_FORMAT_PNG);
    writeParams.put(PngConstants.PARAM_KEY_PNG_FORCE_TRUE_COLOR, Boolean.TRUE);
    //writeParams.put(PngConstants.PARAM_KEY_COMPRESSION, TiffConstants.TIFF_COMPRESSION_LZW);

    byte bytes[] = Imaging.writeImageToBytes(resizedImage, ImageFormat.IMAGE_FORMAT_PNG, writeParams);

    FileUtils.writeByteArrayToFile(fileOut, bytes);

但输出文件太大的问题:

2,53 Mo(2 655 456个八位字节)

压缩png文件需要什么?

如果我使用库输出文件信息。我得到了这个:

Format Details: Png
Bits Per Pixel: 24
Comments: 0
Format: PNG
Format Name: PNG Portable Network Graphics
Compression Algorithm: PNG Filter
Height: 1080
MimeType: image/png
Number Of Images: 1
Physical Height Dpi: -1
Physical Height Inch: -1.0
Physical Width Dpi: -1
Physical Width Inch: -1.0
Width: 1920
Is Progressive: false
Is Transparent: false
Color Type: RGB
Uses Palette: false

我在这里取样:http://static.pcinpact.com/images/bd/news/93707-big-buck-bunny.png

我在这段代码中遇到了同样的问题

BufferedImage image = Imaging.getBufferedImage(new File("./src/test/resources/big-buck-bunny-1080p.png"));

Imaging.writeImage(image, new File("output.png"), ImageFormat.IMAGE_FORMAT_PNG, null);

以下是原始文件:http://postimg.org/image/xob1ss6gj/

这里是上面snipet的输出文件(读,写):http://postimg.org/image/qu0r33dtf/

0 个答案:

没有答案