imageio

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);
		writer.setOutput(ios);

		ImageWriteParam param = writer.getDefaultWriteParam();

		// compress to a given quality
		param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
		param.setCompressionQuality(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
		is.close();
		os.close();
		ios.close();
		writer.dispose();

	}

}

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

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

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

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
kudo
kudo
4 years ago

Hi.

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

Back to top button