使用Java ImageIO进行Tiff压缩

我在将png转换为tiff时遇到问题。转换很好,但是图像很大。我认为问题是我没有正确执行压缩?有人有什么建议吗?

这是代码示例

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()仅创建一个ImageWriteParam对象,不会将其链接回其他任何对象。

我看不到您的代码中有任何机制param可用于ImageWriter

我相信代替:

writer.write(bi);

您需要使用:

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

以上是 使用Java ImageIO进行Tiff压缩 的全部内容, 来源链接: utcz.com/qa/430234.html

回到顶部