简体   繁体   English

使用Java ImageIO进行Tiff压缩

[英]Tiff compression using Java ImageIO

I am having issues converting a png to tiff. 我在将png转换为tiff时遇到问题。 The conversion goes fine, but the image is huge. 转换很顺利,但图像很大。 I think the issue is that I am not doing the compression correctly? 我认为问题是我没有正确进行压缩? Anyone have any suggestions?? 任何人都有任何建议?

Here is the code sample 这是代码示例

public static void test() throws IOException {

    // String fileName = "4958813_1";
    String fileName = "4848970_1";
    String inFileType = ".PNG";
    String outFileType = ".TIFF";

    ImageIO.scanForPlugins();

    File fInputFile = new File("I:/HPF/UU/" + fileName + inFileType);
    InputStream fis = new BufferedInputStream(new FileInputStream(
            fInputFile));
    PNGImageReaderSpi spi = new PNGImageReaderSpi();
    ImageReader reader = spi.createReaderInstance();

    ImageInputStream iis = ImageIO.createImageInputStream(fis);
    reader.setInput(iis, true);
    BufferedImage bi = reader.read(0);

    TIFFImageWriterSpi tiffspi = new TIFFImageWriterSpi();
    ImageWriter writer = tiffspi.createWriterInstance();
    //Iterator<ImageWriter> iter =  ImageIO.getImageWritersByFormatName("TIFF");
    //ImageWriter writer = iter.next();

    ImageWriteParam param = writer.getDefaultWriteParam();
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);

    param.setCompressionType("LZW");
    param.setCompressionQuality(0.5f);
    File fOutputFile = new File("I:\\HPF\\UU\\" + fileName + outFileType);
    ImageOutputStream ios = ImageIO.createImageOutputStream(fOutputFile);
    writer.setOutput(ios);
    writer.write(bi);

}

Writer.getDefaultWriteParam() only creates an ImageWriteParam object, it doesn't link it back to anything else. Writer.getDefaultWriteParam()只创建一个ImageWriteParam对象,它不会将其链接回任何其他对象。

I don't see any mechanism in your code for your modified param object to be subsequently used in the ImageWriter . 我没有在您的代码中看到任何机制,您的修改后的param对象随后将在ImageWriter

I believe that instead of: 我相信而不是:

writer.write(bi);

you need to use: 你需要使用:

writer.write(null, new IIOImage(bi, null, null), param);

I don't know Java IO, but generally you want to look at a few things 我不了解Java IO,但一般来说你想看几件事

  1. Can you use JPEG compression instead of LZW? 你能用JPEG压缩代替LZW吗?
  2. See how to set the TIFF strip size -- if small size is what you want, set it to the height of the image. 了解如何设置TIFF条带大小 - 如果您想要小尺寸,请将其设置为图像的高度。

Edit: Looks like a TiffWriteParam has the following methods 编辑:看起来像TiffWriteParam有以下方法

tiffWriteParam.setTilingMode(ImageWriteParam.MODE_EXPLICIT);
tiffWriteParam.setTiling(imageWidth, imageHeight, 0, 0);

set the imageWidth and imageHeight vars to your image's size. 将imageWidth和imageHeight变量设置为图像的大小。 The downside is that it will be slower to read out regions of the image. 缺点是读出图像区域会更慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM