使用 Java ImageIO 进行 Tiff 压缩
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/281512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tiff compression using Java ImageIO
提问by CFreiner
I am having issues converting a png to 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??
我在将 png 转换为 tiff 时遇到问题。转换进行得很好,但图像很大。我认为问题是我没有正确进行压缩?有人有什么建议吗??
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);
}
采纳答案by Alnitak
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);
回答by Lou Franco
I don't know Java IO, but generally you want to look at a few things
我不懂Java IO,但一般你想看看一些东西
- Can you use JPEG compression instead of LZW?
- See how to set the TIFF strip size -- if small size is what you want, set it to the height of the image.
- 您可以使用 JPEG 压缩代替 LZW 吗?
- 查看如何设置 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. The downside is that it will be slower to read out regions of the image.
将 imageWidth 和 imageHeight 变量设置为您的图像大小。缺点是读取图像区域会更慢。