【问题标题】:Convert a tiff into a buffered image (Java)将 tiff 转换为缓冲图像 (Java)
【发布时间】:2013-07-08 10:11:06
【问题描述】:

我需要将 tiff 文件转换为 BufferedImage。我写了以下代码:

String filepath = "C:\\tiffFolder\\";
String filename = "myTiffImage.tif";
File myFile = new File (filepath + filename); 
BufferedImage img = ImageIO.read(myFile);

我确定 myFile 已正确实例化:问题是在第四行代码之后 img 仍然为空。

我做错了什么? 非常感谢!

编辑

解决了,我用了以下代码:

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();

【问题讨论】:

  • myFile.canRead() 返回什么? [第 3 行和第 4 行之间]
  • @Burkhard,它返回 true
  • @DanieleMilani 检查以下行打印出什么:System.out.println(Arrays.toString(ImageIO.getReaderFileSuffixes())); 在我的机器上是[bmp, jpg, jpeg, wbmp, png, gif]。没有tif。
  • 尝试使用其他图片(jpg),我不确定ImageBuffer是否支持tif格式。
  • @johnchen902 它打印 [bmp, jpg, wbmp, jpeg, png, gif]...我认为这是问题所在!是否可以在 ImageIo 支持的后缀中添加 tif 和 tiff?

标签: java file bufferedimage tiff javax.imageio


【解决方案1】:

您正在尝试读取 ImageIO 不支持的文件格式。

正如 johnchen902 所指出的,ImageIO.getReaderFileSuffixes() 返回支持的后缀列表。 tiff 不在该列表中。这就是为什么你不能那样读它的原因。一些外部库可能会对您有所帮助。 例如:Java Advanced Imaging API 支持 TIFF。详情here.

【讨论】:

  • 链接已修复。很抱歉。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多