Reading TIFF image from file using BufferedImage and JAI

Kyle Gregorio picture Kyle Gregorio · Jan 2, 2012 · Viewed 11.8k times · Source

I'm trying to read a TIFF image from file using BufferedImage. The following is my code:

String filename = "/image/parrot.tiff";
File f = new File (filename);
try{
        BufferedImage img = ImageIO.read(f);
}catch (Exception e){
        System.out.println("Something went wrong!");
}

But it isn't working. I have a method called testInput just to test if the file was read properly:

public void testInput(){
    System.out.println(f.exists());
    System.out.println(f.canRead());
    System.out.println(f.canWrite());
}*/

The three of them would always return "false" and the above code always returns "Something went wrong!". I already added JAI ImageIO for the plug-in to read TIFF image. Any idea what seems to be the problem?

Answer

plinth picture plinth · Jan 10, 2012

You need to make sure that you actually have JAI at the ready. JAI is a plug-in extension to ImageIO and if it's not there then you can't decode TIFFs. Here's a quick unit test:

@Test
public void canGetTiffDecoder()
{
    Iterator<ImageReader> reader = ImageIO.getImageReadersByFormatName("TIFF");
    assertNotNull(reader);
    assertTrue("No tiff decoder", reader.hasNext());
}