Reputation: 41
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?
Upvotes: 2
Views: 12394
Reputation: 3260
It also matters what format of tif is used. Even with the JAI plug-in, only some forms of tiff are supported. For instance, when I downloaded a bluemarble.tif image, deep in the inner workings I got:
Caused by: java.lang.RuntimeException: Planar (band-sequential) format TIFF is not supported.
So certain tif file formats are not supported without the help of specialized libraries. One of those libraries is GDAL. http://gdal.org/java/ However many of the support libraries will require native code, and may not work in a portable purely java context.
Upvotes: 1
Reputation: 49179
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());
}
Upvotes: 5
Reputation: 1535
It may seem silly, but are you sure your file is placed in
/image/parrot.tiff
?
According to the Javadoc, exists() returns :
true if and only if the file or directory denoted by this abstract pathname exists; false otherwise
So I think the path may be wrong. According to your comment, I think the correct path should be
src/image/parrot.tiff
If it isn't, try
src/image/parrot.tiff
In all cases, you must understand better how file path are managed in Java (and in most other languages) ;-)
Upvotes: 2