-1

How do i read a TIFF image using Java IMAGEIO library??(I am using Eclipse Luna)..And once i download the plugin(JAR files) how to give the Classpath so that it can read my input TIFF image file?

Codebeginner
  • 193
  • 4
  • 14
  • 2
    Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it. – Jens Mar 17 '15 at 10:40
  • Okay.I wil post the code snippet. – Codebeginner Mar 17 '15 at 10:42
  • 1
    TIFF is not supported by default by ImageIO. See [Can't read and write a TIFF image file using Java ImageIO standard library](http://stackoverflow.com/questions/1954685/cant-read-and-write-a-tiff-image-file-using-java-imageio-standard-library). And search the web for "java imageio tiff". – Jesper Mar 17 '15 at 10:44
  • I searched the same.I got jai_imageio-1.1.jar.zip Now i dont know how to use that.. – Codebeginner Mar 17 '15 at 10:50
  • at com.sun.media.imageioimpl.plugins.tiff.TIFFImageReader.read(TIFFImageReader.java:1154) at javax.imageio.ImageIO.read(ImageIO.java:1448) at javax.imageio.ImageIO.read(ImageIO.java:1308) at Median3x3.(Median3x3.java:43) at Median3x3.main(Median3x3.java:108) .....Getting this error – Codebeginner Mar 17 '15 at 11:50
  • Please *update your question* with the code you have and and the *full* stack trace you get. Possibly also attach or link the TIFF file you used. If we can't reproduce your problem, it's hard for us to help. :-) – Harald K Mar 17 '15 at 12:03
  • Hey I got it.Actually the size of the image was causing the above error.It was too huge. – Codebeginner Mar 17 '15 at 12:30

2 Answers2

4

Here a quick example to convert a TIFF image into a PNG image.

// quick conversion example
File inputFile = new File("image.tiff");
File outputFile = new File("output.png");
BufferedImage image = ImageIO.read(inputFile);
ImageIO.write(image, "png", outputFile);

Print a list of all supported formats of the JAI ImageIO library.

import javax.imageio.ImageIO;
...
for (String format : ImageIO.getWriterFormatNames()) {
    System.out.println("format = " + format);
}

note For the convertion of image formats which have no built-in support a supporting library must be in the classpath. To find the supported formats check https://docs.oracle.com/javase/tutorial/2d/images/loadimage.html or the snippet above.

e.g. for TIFF you could use the jai_imageio-1.1.jar (or newer).

javac -cp jai_imageio-1.1.jar:. Main.java
java -cp jai_imageio-1.1.jar:. Main

If no TIFF format supporting library is in the classpath the above convertion snippet fails with java.lang.IllegalArgumentException: image == null!.

Following formats have built-in support (Java 8)

BMP
GIF
JPEG
PNG
WBMP

jai_imageio-1.1.jar adds support for

JPEG2000
PNM
RAW
TIFF

edit As times goes by and Java 9 is released, a small update, because Java 9 supports TIFF now out of the box.

compile and run with Java 9 without an additional library

import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
class TiffToPng {
    public static void main(String[] args) throws Exception {
        File inputFile = new File("image.tiff");
        File outputFile = new File("output.png");
        BufferedImage image = ImageIO.read(inputFile);
        ImageIO.write(image, "png", outputFile);
    }
}

to find supported ImageReader / ImageWriter formats resp. MIME types you could use following snippets

for (String format : ImageIO.getReaderFormatNames()) {
    System.out.println("format = " + format);
}
...
for (String format : ImageIO.getReaderMIMETypes()) {
    System.out.println("format = " + format);
}


for (String format : ImageIO.getWriterFormatNames()) {
    System.out.println("format = " + format);
}
...
for (String format : ImageIO.getWriterMIMETypes()) {
    System.out.println("format = " + format);
}
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • Thank you.But i dont want to convert it to PNG format.I wanna keep TIFF format itself for I/O operations – Codebeginner Mar 17 '15 at 11:20
  • 1
    @Codebeginner it was only a simple example. If you want to work with the TIFF image itself, then you should operate on the `BufferedImage`. Have a look on the [Oracle Java 2D tutorial](http://docs.oracle.com/javase/tutorial/2d/images/) for further informations. – SubOptimal Mar 17 '15 at 12:05
  • Does the ImageIO come from javax? – Jürgen K. Aug 08 '17 at 13:58
2

If you are getting the error:

java.lang.IllegalArgumentException: image == null!

Just put the below jar :-

  • If you are using eclipse, just add it to referenced libraries.
  • If you are using just simple java file and running it through console, just paste this jar in the class path.

jai_imageio-1.1.jar | http://www.java2s.com/Code/JarDownload/jai/jai_imageio-1.1.jar.zip

And Import the following :

import com.sun.media.imageio.plugins.tiff.*;
Adarsh Singh
  • 208
  • 1
  • 3
  • 17