suda

Suda is the Senior Java EE Developer at IDR Solutions, and specialises in XFA, Fonts, True-Type Fonts, application servers and conversions.
He is a keen science-fiction fan in his spare time.

How to read Tiff images in Java

1 min read

Java’s ImageIO package (built in) does not support Tiff file decoding. So either you need JAI plugin or other third party libraries to convert these files into buffered images. Support for Tiff images in both is not complete.

The third party Java libraries support only a subset of the Tiff files. (example tiling, separated planar, jpeg compression, multi page). So we decided to provide Tiff encoding and decoding support in our JDeli image library.

Here is a simple example of how you can use the library to decode raw tiff bytes:

TiffDecoder decoder = new TiffDecoder(rawTiffData);
BufferedImage decodedImage = decoder.read();

or using RandomAccessFile:

RandomAccessFile raf = new RandomAccessFile("yourFileLocation","r");
TiffDecoder decoder = new TiffDecoder(raf);
BufferedImage decodedImage = decoder.read();
//insert your bufferimage handling code here;
raf.close();

The code below exemplifies how to decode multi page tiff files:

 RandomAccessFile raf = new RandomAccessFile("yourFileLocation", "r");
 TiffDecoder decoder = new TiffDecoder(raf);
 for (int i = 1; i <= decoder.getPageCount(); i++) {
   BufferedImage decodedImage = decoder.read(i);
   // Insert BufferedImage handling code here
 }
 raf.close();

Technical features

The Tiff file format is actually a whole collection of possible settings. Here is the full list which JDeli supports.

Available decompression techniques:

  • uncompressed
  • CCITT group 3 and 4
  • Deflate/Adobe Deflate
  • LZW
  • Packbits
  • Jpeg Technote 2 ( please note: old-style jpeg compression support is not added the reason can be found here).

Tiff Decoder also supports:

  • Colorspace: bilevel, grayscale, rgb, argb, cmyk, acmyk, ycbcr
  • Bits Per Sample : 1 to 32
  • Byte Ordering : Little and Big Endian
  • Other : Single, Multi file, Tiling, Planar (Chunky, Separated), Predictor, 16,32 bit floating samples

I hope you may find it useful and make use of the features in the JDeli Image Library.

TIFFTOJAVA

We have also created a useful how to series on how to read and write various Image formats.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (TwitterFacebook and Google+) or the  Blog RSS.

suda

Suda is the Senior Java EE Developer at IDR Solutions, and specialises in XFA, Fonts, True-Type Fonts, application servers and conversions.
He is a keen science-fiction fan in his spare time.

2 Replies to “How to read Tiff images in Java”

  1. Hi

    I want to get the bit depth of tiff file. i tried through Apache TIKA but able to get only the mime type.

Leave a Reply

Your email address will not be published. Required fields are marked *