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 write our own TIFF Decoder which implements those features and is now available as part of JDeli.

Javadoc and Download

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();

Watch a video on how to Read and Write TIFF files in Java with JDeli

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

Why use JDeli?

JDeli offers a range of advantages over ImageIO and alternatives, including:

  • prevents heap related JVM crashes
  • implements unsupported image formats
  • reduce output file size
  • improve read/write performance
  • supports threading
  • superior image scaling algorithms

Learn more about JDeli, or download to try it yourself.

 

 

This article is part of our series on reading and writing image files in Java.

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.