Home » Desktop Java » imageio » Compress a JPEG file

About Ilias Tsagklis

Avatar photo
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Compress a JPEG file

With this example we are going to learn how to compress a JPEG file. When saving space is important for your system, you have to consider compressing your images using JPEG format. This format compresses the image but its high quality can be preserved.

In order to compress a JPEG file all you have to do is:

  • Open a new file to the image using new File("myimage.jpg").
  • Create a BufferedImage using ImageIO.read(is).
  • Get an image writer using ImageIO.getImageWritersByFormatName("jpg").
  • Create a ImageWriteParam.
  • Use setCompressionMode(ImageWriteParam.MODE_EXPLICIT) to set the compression mode.
  • Use setCompressionQuality(quality) to set the compression quality.
  • Use writer.write(null, new IIOImage(image, null, null), param) that appends a complete image stream containing a single image and associated stream and image metadata and thumbnails to the output.

Let’s see the code:

package com.javacodegeeks.snippets.desktop;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

public class CompressJPEGFile {

	public static void main(String[] args) throws IOException {

		File imageFile = new File("myimage.jpg");
		File compressedImageFile = new File("myimage_compressed.jpg");

		InputStream is = new FileInputStream(imageFile);
		OutputStream os = new FileOutputStream(compressedImageFile);

		float quality = 0.5f;

		// create a BufferedImage as the result of decoding the supplied InputStream
		BufferedImage image = ImageIO.read(is);

		// get all image writers for JPG format
		Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");

		if (!writers.hasNext())
			throw new IllegalStateException("No writers found");

		ImageWriter writer = (ImageWriter) writers.next();
		ImageOutputStream ios = ImageIO.createImageOutputStream(os);

		ImageWriteParam param = writer.getDefaultWriteParam();

		// compress to a given quality

		// appends a complete image stream containing a single image and
	    //associated stream and image metadata and thumbnails to the output
		writer.write(null, new IIOImage(image, null, null), param);

		// close all streams



This was an example on how to compress a JPEG file.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
2 years ago


Thank you for this example.
I have a problem with it: The image is rotated after the compression!