Can't read and write a TIFF image file using Java ImageIO standard library

旧巷老猫 提交于 2019-11-26 01:36:33

问题


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?

Thanks.


回答1:


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

It supports any baseline TIFF option, along with a lot of standard extensions. From version 3.1 the TIFF plugin also has write support.

With the proper JARs in your class path, usage can be as simple as:

BufferedImage image = ImageIO.read(inputTIFF);
// ...modify image (compose, resize, sharpen, etc)...
ImageIO.write(image, "TIFF", outputTIFF);



回答2:


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.

That means, using Java 9 or later, the following code will just work, without any extra imports or dependencies:

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.




回答3:


I tried JAI, and it didn't work for me.

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



回答4:


Add Maven dependency :

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


来源:https://stackoverflow.com/questions/1954685/cant-read-and-write-a-tiff-image-file-using-java-imageio-standard-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!