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

As part of our series of Handling common image file formats in Java I will be writing about how we can read JPeg images in Java where I mention the different libraries that enable us to read JPeg images.

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.

There are many libraries out there that can read JPeg images. These include ImageIO, JAI and JDeli. JDeli can read and write a very wide range of image formats. Here is the full list on Image file conversions supported.

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:

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

You can find the javadocs for JDeli here. JDeli provides a complete JPG Converter and you can find more information here.

We have also created a useful how to series on how to read and write various Image formats. You can find more about this on our Index.

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.

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

How to choose JPG versus JPEG2000 for image files

Since we started to support both JPG and JPG2000 as image file outputs in our software, we have found that this is a very...
Mark Stephens
1 min read

Leave a Reply

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

IDRsolutions Ltd 2019. All rights reserved.