1

如何在java中压缩tiff文件,当我将jpeg文件添加到tiff时,文件大小正在增加

File f2 = new File("D:/KYCDATA/mani/myimage_compressed.jpeg");

    BufferedImage image3 = ImageIO.read(f2);
    List<BufferedImage> list = new ArrayList<BufferedImage>();
    list.add(image3);

    File tiffFile = new File("D:/KYCDATA/mani/mani.tiff");
    if(tiffFile.exists()) {
        tiffFile.delete();
    }
    ImageOutputStream ios = null;
    ImageWriter writer = null;
    Iterator it = ImageIO.getImageWritersByFormatName("tiff");
    if (it.hasNext()) {
        writer = (ImageWriter) it.next();
    }

    ios = ImageIO.createImageOutputStream(tiffFile);
    writer.setOutput(ios);

    IIOImage iioImage = null;
    for (int i = 0; i < list.size(); i++) {
        iioImage = new IIOImage(list.get(i), null, null);
        if (i == 0) {
            writer.write(iioImage);
        } else {
            writer.writeInsert(i, iioImage, null);
        }
        writer.endWriteSequence();
4

2 回答 2

3

TIFF 使用无损压缩,所以如果我正确理解您的问题,我希望 TIFF 大于原始 JPEG。请参阅此处以获取简单说明:

http://www.cambridgeincolour.com/tutorials/imagetypes.htm

于 2012-12-17T04:49:37.993 回答
0

您需要在 ImageWriteParam 对象上设置压缩模式,然后将此参数传递给编写器。

添加以下代码以获取默认编写器参数并设置压缩模式

ImageWriteParam writeParam = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType("PackBits");

修改您的代码以添加我们在上面设置的 writeParam 来编写方法

   if (i == 0) {
        writer.write( null,iioImage,writeParam);
    } else {
        writer.writeInsert(i, iioImage, writeParam);
    }
于 2017-08-08T16:35:27.630 回答