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.
How to write out an image as a JPEG file in ImageIO
- Create a File (or OutputStream) object for the JPEG output.
- Pass image, jpg type and File (or OutputStream) object into write method
and the Java code to write JPEG in ImageIO…
File file = new File("/path/to/outputFile.jpg"));
ImageIO.write(bufferedImage, "jpg", file);
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.
How to write out an image as a JPEG file with JDeli
- Add JDeli to your class or module path. (download trial jar).
- Create a File (or OutputStream) object
- Pass image, JPEG type and File (or OutputStream) object into write method
and the Java code to write JPEG in JDeli…
File file = new File("/path/to/outputFile.jpg"));
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
JpegEncoderOptions options = new JpegEncoderOptions();
JDeli.write(bufferedImage, options, file);
In below picture, my original file was a .png image, and I used JDeli to write it out as a .jpg image.
Note you can easily switch to JDeli and replace ImageIO by just changing ImageIO.write to JDeli.write.
Are you a Java Developer working with Image files?
// Read an image
BufferedImage bufferedImage = JDeli.read(dicomImageFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(heicImageFile);
// Write an image
JDeli.write(bufferedImage, "heic", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(jpegImageFile);
// Write an image
JDeli.write(bufferedImage, "jpeg", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(jpeg2000ImageFile);
// Write an image
JDeli.write(bufferedImage, "jpx", outputStreamOrFile);
// Write an image
JDeli.write(bufferedImage, "pdf", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(pngImageFile);
// Write an image
JDeli.write(bufferedImage, "png", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(tiffImageFile);
// Write an image
JDeli.write(bufferedImage, "tiff", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(webpImageFile);
// Write an image
JDeli.write(bufferedImage, "webp", outputStreamOrFile);
Why do developers choose JDeli over free alternatives?
- Works with newer image formats such as AVIF, HEIC, JPEG XL, WEBP
- Better support than alternatives for JPEG, PNG, TIFF.
- Prevent JVM crashes caused by native code in other image libraries
- Better performance than other popular Java image libraries