This article will explain how to read Tiff files in Java. We also have a related article covering how to write TIFF files in Java.
ImageIO does not read TIFF file types by default so you will need to use an ImageIO plugin or an external library. We will demonstrate using an Open source ImageIO plugin called TwelveMonkeys (which extends ImageIO to provide TIFF reading support) and using the JDeli Image Library.
How to read a TIFF image in Java with ImageIO
- Download TwelveMonkeys plugin and add to your class path.
- Create a File handle, InputStream, or URL pointing to the raw TIFF image.
- ImageIO will now be able to read a TIFF file into a BufferedImage.
and the Java code to read a TIFF with ImageIO…
File file = new File(“/path/to/tiffFile.tiff”);
BufferedImage image = ImageIO.read(file);
How to read a TIFF image in Java with JDeli
- Add JDeli to your class or module path. (download the trial jar).
- Create a File handle, InputStream pointing to the raw TIFF image. You can also use a byte[] containing the image data.
- Read the TIFF image into a BufferedImage
and the Java code to read a TIFF with JDeli…
File file = new File(“/path/to/tiffFile.tiff”);
BufferedImage image = JDeli.read(file);
Try print the result: if it works, it will print the image information
How to read a multi-file TIFF image in Java with JDeli
- Add JDeli to your class or module path. (download the trial jar).
- Create a File handle, InputStream pointing to the raw TIFF image. You can also use a byte[] containing the image data.
- Read the TIFF file into a TiffDecoder instance
- Loop through the separate images in the TIFF file
and the Java code to read a multi-TIFF with JDeli…
RandomAccessFile raf = new RandomAccessFile("yourTiffFileLocation", "r");
TiffDecoder decoder = new TiffDecoder(raf);
for (int i = 1; i <= decoder.getPageCount(); i++) {
BufferedImage decodedImage = decoder.read(i);
}
raf.close();
Are you a Java Developer working with Image files?
// Read an image
BufferedImage bufferedImage = JDeli.read(dicomImageFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(heicImageFile);
// Write an image
JDeli.write(bufferedImage, "heic", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(jpegImageFile);
// Write an image
JDeli.write(bufferedImage, "jpeg", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(jpeg2000ImageFile);
// Write an image
JDeli.write(bufferedImage, "jpx", outputStreamOrFile);
// Write an image
JDeli.write(bufferedImage, "pdf", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(pngImageFile);
// Write an image
JDeli.write(bufferedImage, "png", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(tiffImageFile);
// Write an image
JDeli.write(bufferedImage, "tiff", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(webpImageFile);
// Write an image
JDeli.write(bufferedImage, "webp", outputStreamOrFile);
Why do developers choose JDeli over free alternatives?
- Works with newer image formats such as AVIF, HEIC, JPEG XL, WEBP
- Better support than alternatives for JPEG, PNG, TIFF.
- Prevent JVM crashes caused by native code in other image libraries
- Better performance than other popular Java image libraries
Hi
I want to get the bit depth of tiff file. i tried through Apache TIKA but able to get only the mime type.
Hi Vikash,
Getting the bit depth is something that we could add to our image library, JDeli. If you are interested in purchasing JDeli you can find out more here.