Zain Zain is a Java developer. His is a knowledge seeker who likes to try out and explore new things.

How to read JPEG images in Java

1 min read

In this article I will show you how to read JPEG images in Java using ImageIO, JAI and JDeli.

We previously used ImageIO to read JPEG files, but over time became increasingly dissatisfied as we discovered more and more issues. Eventually we wrote our own JPEG Decoder which fixes those issues and is now available as part of JDeli.

What is JPEG?

JPEG (Joint Photographic Experts Group) is a file type of images with the extensions .jpg and .jpeg.

It is commonly used for digital images as it gives you high quality images. One main feature of JPeg is that it enables you to choose between quality and file size; you can sacrifice quality for a smaller file or vice versa.

How to read JPEG images in Java?

Here are some code examples using Image IO, JAI and JDeli to read JPEG.

ImageIO:

File jpegFile = new File("jpegImage.jpg");
BufferedImage image = ImageIO.read(jpegFile);

You can find the javadocs for ImageIO here. You can find more information on ImageIO (included with the JDK) on their main page.

JAI:

 RenderedImage image = JAI.create("fileload", "test1.jpeg");

You can find the javadocs for JAI here. More information on JAI can be found here.

JDeli:

Javadoc and Download

You can easily replace ImageIO (and get much better support for JPEG files) by just changing ImageIO.read to JDeli.read:

//Read Image (can also be OutputStream or byte array)
File jpegFile = new File("jpegImage.jpg"); 
BufferedImage image = JDeli.read(jpegFile);

Or you can use the JDeli JpegDecoder directly:

JpegDecoder decoder = new JpegDecoder();
BufferedImage image = decoder.read(rawImageBytes);

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

Zain Zain is a Java developer. His is a knowledge seeker who likes to try out and explore new things.

Leave a Reply

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

IDRsolutions Ltd 2019. All rights reserved.