Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a tiff into a buffered image (Java)

I need to convert a tiff file into a BufferedImage. I wrote the following code:

String filepath = "C:\\tiffFolder\\";
String filename = "myTiffImage.tif";
File myFile = new File (filepath + filename); 
BufferedImage img = ImageIO.read(myFile);

I know for sure myFile is correctly instantiated: the problem is that after the fourth line of code img is still null.

What am I doing wrong? Thanks so much!

Edit

Solved, I used the following code:

FileSeekableStream stream = new FileSeekableStream(filepath + filename);
TIFFDecodeParam decodeParam = new TIFFDecodeParam();
decodeParam.setDecodePaletteAsShorts(true);
ParameterBlock params = new ParameterBlock();
params.add(stream);
RenderedOp image1 = JAI.create("tiff", params);
BufferedImage img = image1.getAsBufferedImage();
like image 707
Daniele Milani Avatar asked Jul 08 '13 10:07

Daniele Milani


1 Answers

You are trying to read a file format that is not supported by ImageIO.

As johnchen902 pointed out, the ImageIO.getReaderFileSuffixes() returns a list of suffixes that are supported. tiff is not in that list. That's why you cannot read it that way. Some external libaries might help you. For instance: The Java Advanced Imaging API supports TIFF. Details here.

like image 81
Burkhard Avatar answered Sep 28 '22 17:09

Burkhard