suda Senior Java EE Develope specialises in Pdf forms , Fonts, application servers and Image manipulation, meditates in 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 JDeli (a complete Tiff Converter solution for developers).

Here is a simple example example to read a tiff file:

//Read Image (can also be OutputStream or byte array)
File tiffFile = new File("tiffImage.tiff"); 
BufferedImage image = JDeli.read(tiffFile);

Or you can use the JDeli TiffDecoder directly. 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 buffered image 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.

JDeli can read and write a very wide range of image formats. Here is the full list on Image file conversions supported.

TIFFTOJAVA

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

IDRsolutions develop a Java PDF Viewer and SDK, an Adobe forms to HTML5 forms converter, a PDF to HTML5 converter and a Java ImageIO replacement. On the blog our team post anything interesting they learn about.

suda Senior Java EE Develope specialises in Pdf forms , Fonts, application servers and Image manipulation, meditates in 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 *

IDRsolutions Ltd 2019. All rights reserved.