Link
Skip to main content

Java JPEG Writer

Java’s built-in ImageIO has limited support for writing JPEG files and provides little control over encoding quality and format-specific options. JDeli is a 100% Java JPEG writer that gives you full encoding control with no dependencies.

Getting started

Add JDeli to your project via Maven or Gradle, or see using JDeli with Java modules.

What is supported

The JDeli JPEG writer supports the following features:

Feature Supported
Alpha transparency Yes
Colour optimisation Yes
BufferedImage input Yes
Compression - 8bit Quantised Yes
Compression - JPEG Yes
Metadata EXIF
Output to file Yes
Output to OutputStream Yes
Output to byte[] Yes
Pure Java implementation Yes

Quick start or to replace in existing code using ImageIO:

The simplest way to write a JPEG image is:

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

Or to a byte array:

byte[] jpgData = JDeli.write(bufferedImage, "jpg");

Or to an OutputStream:

JDeli.write(bufferedImage, "jpg", os);

Simple usage

For better type safety, use OutputFormat:

JDeli.write(myBufferedImage, OutputFormat.JPEG, outputStreamOrFile);

OutputFormat allows setting of any supported Image Format

Benefits:

  • Compile-time safety
  • Cleaner code
  • Recommended for new applications

Advanced usage: controlling the output

Use jpgEncoderOptions when you need control over encoding behaviour.

final JpegEncoderOptions options = new JpegEncoderOptions();

//set any options in options instance - examples below
options.setQuality(100);//0-100

//write out
JDeli.write(myBufferedImage, options, outputStreamOrFile);

JpegEncoderOptions allows setting of specific options.

Various image processing operations can be conducted on the image, detailed documentation can be found here.

Performance comparisons:

These figures were generated using jmh (as documented on our blog) with a standard set of images (also documented). They should be easy to replicate if you wish to validate, the code is on GitHub.

The higher the number, the better.
Benchmark results using JMH:
Mode: Throughput Count: 25 Units: ops/s

Benchmark Score Error
ImageIO 13.090 ± 0.046
ImageIO_high 9.844 ± 0.044
ImageIO_low 13.276 ± 0.147
JDeli 26.899 ± 0.237
JDeli_high 15.479 ± 0.125
JDeli_low 28.729 ± 0.260
ICafe 12.998 ± 0.522

Number of files: 166

Benchmark AVG Size unreadable output AVG SSIM score AVG Similarity
ImageIO 1.547KB 36 0.9848556782742076 Very similar (high quality)
ImageIO_high 7.097KB 36 0.9956989675945417 Identical or virtually identical
ImageIO_low 0.849KB 36 0.7523266496966906 Moderately similar (visible differences)
JDeli 1.397KB 0 0.9379582984498058 Very similar (high quality)
JDeli_high 8.945KB 0 0.9538428669497789 Very similar (high quality)
JDeli_low 0.796KB 0 0.7083322490195157 Moderately similar (visible differences)
ICafe 5.388KB 0 0.9394471069552716 Similar (noticeable but minor differences)

Tested on 2020 13inch M1 MacBook Pro using JDK 18.0.1.1


Frequently asked questions

Does JDeli produce better JPEG encoding than Java ImageIO?

Yes. While ImageIO can write JPEG, JDeli gives you full control over encoding options such as compression and quality, and produces more consistent output across platforms.

Does writing or encoding JPEG in Java with JDeli require additional libraries?

No. JDeli is a 100% Java library with no dependencies. It runs on any platform where the JVM runs with no additional installation.

What output types does JDeli support for writing JPEG?

JDeli can write and encode JPEG to a File, OutputStream, or return the encoded data as a byte array.

See also


Why JDeli?

  • Support image formats such as AVIF, HEIC and JPEG XL that are not supported in Java.
  • Process images up to 3x faster than ImageIO and alternative Java image libraries.
  • Prevent JVM crashes caused by native code in other image libraries such as ImageIO.
  • Handle JPEG, PNG, TIFF image file formats fully in Java.
  • Keep your Image files secure as JDeli makes no calls to any external system or third party library.

Learn more about JDeli

Start Your Free Trial