无法使用 Java ImageIO 标准库读写 TIFF 图像文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1954685/
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
Can't read and write a TIFF image file using Java ImageIO standard library
提问by Gle
I don't know what to do with TIFF images, but I can't read or write any of them using straight Java standard ImageIO library. Any thoughts?
我不知道如何处理 TIFF 图像,但我无法使用直接的 Java 标准 ImageIO 库读取或写入它们中的任何一个。有什么想法吗?
Thanks.
谢谢。
回答by MPG
I tried JAI, and it didn't work for me.
我尝试了 JAI,但它对我不起作用。
Where are you stuck? Does the following work for you?
你被困在哪里?以下对您有用吗?
import java.io.File;
import java.io.FileOutputStream;
import java.awt.image.RenderedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import javax.media.jai.NullOpImage;
import javax.media.jai.OpImage;
import com.sun.media.jai.codec.SeekableStream;
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.TIFFDecodeParam;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.ImageCodec;
public class Main {
public static void main(String args[]) {
File file = new File("input.tif");
try {
SeekableStream s = new FileSeekableStream(file);
TIFFDecodeParam param = null;
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
RenderedImage op = new NullOpImage(dec.decodeAsRenderedImage(0),
null,
OpImage.OP_IO_BOUND,
null);
FileOutputStream fos = new FileOutputStream("output.jpg");
JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(fos);
jpeg.encode(op.getData());
fos.close();
}
catch (java.io.IOException ioe) {
System.out.println(ioe);
}
}
}
回答by Miguelin
Add Maven dependency :
添加 Maven 依赖项:
<dependency>
<groupId>org.geotoolkit</groupId>
<artifactId>geotk-coverageio</artifactId>
<version>3.17</version>
</dependency>
Code example :
代码示例:
import org.geotoolkit.image.io.plugin.RawTiffImageReader;
IIORegistry registry = IIORegistry.getDefaultInstance();
registry.registerServiceProvider(new RawTiffImageReader.Spi());
String[] a = ImageIO.getReaderFileSuffixes();
for (int i=0; i<a.length; i++) {
System.out.println(a[i]);
}
BufferedImage image = ImageIO.read(new File("C:\mypic.tiff"));
ImageIO.write(image, "jpg",new File("C:\out.jpg"));
ImageIO.write(image, "gif",new File("C:\out.gif"));
ImageIO.write(image, "png",new File("C:\out.png"));
ImageIO.write(image, "tif",new File("C:\out.tiff"));
回答by haraldK
If you don't like or can't use JAI for any reason I have written a TIFF ImageReader plugin for ImageIO, available on GitHub. It is pure Java and does not need any native installs, and comes with a very friendly open source license (BSD).
如果您不喜欢或因任何原因不能使用 JAI,我为 ImageIO 编写了一个 TIFF ImageReader 插件,可在GitHub上找到。它是纯 Java 的,不需要任何本地安装,并带有非常友好的开源许可证 (BSD)。
It supports any baseline TIFF option, along with a lot of standard extensions. From version 3.1 the TIFF plugin also has write support.
它支持任何基线 TIFF 选项,以及许多标准扩展。从 3.1 版开始,TIFF 插件也有写支持。
With the proper JARs in your class path, usage can be as simple as:
在您的类路径中使用正确的 JAR 文件后,用法可以很简单:
BufferedImage image = ImageIO.read(inputTIFF);
// ...modify image (compose, resize, sharpen, etc)...
ImageIO.write(image, "TIFF", outputTIFF);
回答by haraldK
According to JEP 262: TIFF Image I/Othe TIFF plugin that used to be part of JAI will be available as part of the Java SE, starting from Java 9.
根据JEP 262: TIFF Image I/O,从 Java 9 开始,曾经是 JAI 一部分的 TIFF 插件将作为 Java SE 的一部分提供。
That means, using Java 9 or later, the following code will just work, without any extra imports or dependencies:
这意味着,使用 Java 9 或更高版本,以下代码将正常工作,无需任何额外的导入或依赖项:
BufferedImage image = ImageIO.read(inputTIFF);
// ...modify image (compose, resize, sharpen, etc)...
ImageIO.write(image, "TIFF", outputTIFF);
I haven't yet been able to verify the support for non-baseline TIFF flavors in this plugin, but I assume at least baseline TIFFs should be fully supported.
我还没有能够验证这个插件中对非基线 TIFF 风格的支持,但我认为至少应该完全支持基线 TIFF。