无法使用Java ImageIO标准库读取和写入TIFF图像文件

时间:2022-06-30 21:20:37

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.

4 个解决方案

#1


10  

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

#2


3  

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

#3


3  

According to JEP 262: TIFF Image I/O the 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图像I / O,以前作为JAI一部分的TIFF插件将作为Java SE的一部分提供,从Java 9开始。

That means, using Java 9, 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。

#4


-3  

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

#1


10  

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

#2


3  

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

#3


3  

According to JEP 262: TIFF Image I/O the 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图像I / O,以前作为JAI一部分的TIFF插件将作为Java SE的一部分提供,从Java 9开始。

That means, using Java 9, 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。

#4


-3  

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