Sylwia Dorota Kedzia Sylwia is a Java developer. She is very passionate about programming and all things Polish.

How to write JPEG Images in Java

1 min read

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

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

What is JPEG?

JPEG stands for "Joint Photographic Experts Group,” and is a file format for compressed images. It uses lossy compression for digital images and is best for saving photographs and paintings of realistic scenes with smooth variations of tone and color (where you do not notice the missing details).

Because JPEG images are not limited to a certain amount of color and contain colorful, high-resolution image data, JPEG is frequently used for storing and transmitting photographic images on the Web and is one of the most common format saved by digital cameras. There is a tradeoff between size and quality.

The file name extensions for JPEG files are: .jpg and .jpeg.

Example Java code to write JPEG images in Java:

Here are some code examples using Image IO, JAI and JDeli to save a BufferedImage to a JPEG

Using ImageIO

Javadoc and included in JDK

//Write Image (can also be OutputStream)
File myNewJPegFile=new File("ImageAsJPeg.jpg");
ImageIO.write(myBufferedImage, "jpg", myNewJPegFile);

Using JAI

Javadoc and Download

//Write Image
File myNewJPegFile=new File("ImageAsJPeg.jpg");
JAI.create("filestore", myBufferedImage, myNewJPegFile, "JPEG");

Using JDeli

Javadoc and Download

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

//Write Image (can also be OutputStream)
File myNewJPegFile=new File("ImageAsJPeg.jpg");
JDeli.write(myBufferedImage, "jpg", myNewJPegFile);

Or you can use the JDeli JpegEncoder directly:

//Write Image
OutputStream os = new FileOutputStream(new File("ImageAsJPeg.jpg"));
JpegEncoder encoder = new JpegEncoder(); 
encoder.write(myBufferedImage, os);

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.

IDRsolutions develop a Java PDF library, a PDF forms to HTML5 converter, a PDF to HTML5 or SVG converter and a Java Image Library that doubles as an ImageIO replacement. On the blog our team post about anything interesting they learn about.

Sylwia Dorota Kedzia Sylwia is a Java developer. She is very passionate about programming and all things Polish.

Leave a Reply

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

IDRsolutions Ltd 2020. All rights reserved.