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

JPG icon

In this article, I will show you how to write JPEG images in Java. We also have a related article covering how to read JPG files in Java.

ImageIO is able to write images as JPEG files, but it offers very little control over the process or the output. So we added the ability to write out images as JPEG files to our JDeli Image library. In this article, I show you how to use either JDeli or ImageIO and cover the benefits of JDeli.

What is JPEG?

JPEG stands for "Joint Photographic Experts Group,” and is a file format for compressed images. It uses lossy or lossless 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 (it is supported by all modern Internet browsers) and is one of the most common formats saved by digital cameras. There is a tradeoff between size and quality.

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

How to write out an image as a JPEG file in ImageIO

Step 1 Create a File (or OutputStream) object for the JPEG output.

File file = new File("/path/to/outputFile.jpg"));

Step 2 Pass image, jpg type and File (or OutputStream) object into write method

ImageIO.write(bufferedImage, "jpg", file);

Note you can easily switch to JDeli and replace ImageIO by just changing ImageIO.write to JDeli.write.

How to write out an image as a JPEG file with JDeli

Step 1 Add JDeli to your class or module path. (download link to the trial jar).

Step 2 Create a File (or OutputStream) object

File file = new File("/path/to/outputFile.jpx"));

Step 3 Pass image, JPEG type and File (or OutputStream) object into write method

JDeli.write(bufferedImage, "JPEG", file);

In JDeli you can also use a typesafe version

JDeli.write(bufferedImage, OutputFormat.JPEG, file);

or pass in a JpegEncoderOptions object for more control over JPEG image output such as level of Compression, choice of jp2 or jpx output, etc.

JpegEncoderOptions options = new JpegEncoderOptions();
JDeli.write(bufferedImage, options, file);


Start reading and writing images with one line of code

Read: BufferedImage image = JDeli.read(streamOrFile);

Write: JDeli.write(myBufferedImage, OutputFormat.HEIC, outputStreamOrFile)

Learn more >>

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 2022. All rights reserved.