将 tiff 转换为缓冲图像(Java)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17524062/
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
Convert a tiff into a buffered image (Java)
提问by Daniele Milani
I need to convert a tiff file into a BufferedImage. I wrote the following code:
我需要将 tiff 文件转换为 BufferedImage。我写了以下代码:
String filepath = "C:\tiffFolder\";
String filename = "myTiffImage.tif";
File myFile = new File (filepath + filename);
BufferedImage img = ImageIO.read(myFile);
I know for sure myFile is correctly instantiated: the problem is that after the fourth line of code img is still null.
我确信 myFile 已正确实例化:问题是在代码的第四行之后 img 仍然为空。
What am I doing wrong? Thanks so much!
我究竟做错了什么?非常感谢!
Edit
编辑
Solved, I used the following code:
解决了,我使用了以下代码:
FileSeekableStream stream = new FileSeekableStream(filepath + filename);
TIFFDecodeParam decodeParam = new TIFFDecodeParam();
decodeParam.setDecodePaletteAsShorts(true);
ParameterBlock params = new ParameterBlock();
params.add(stream);
RenderedOp image1 = JAI.create("tiff", params);
BufferedImage img = image1.getAsBufferedImage();
采纳答案by Burkhard
You are trying to read a file format that is not supported by ImageIO.
您正在尝试读取 ImageIO 不支持的文件格式。
As johnchen902 pointed out, the ImageIO.getReaderFileSuffixes()
returns a list of suffixes that are supported. tiff is not in that list. That's why you cannot read it that way. Some external libaries might help you.
For instance: The Java Advanced Imaging API supports TIFF. Details here.
正如 johnchen902 指出的,ImageIO.getReaderFileSuffixes()
返回支持的后缀列表。tiff 不在该列表中。这就是为什么你不能那样阅读。一些外部库可能会帮助你。例如:Java Advanced Imaging API 支持 TIFF。详情请看这里。