imageio

Compress a JPEG file

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: May 9th, 2013
1 2,097 1 minute read

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 usingnewFile("myimage.jpg").
  • Create a BufferedImage usingImageIO.read(is).
  • Get an image writer usingImageIO.getImageWritersByFormatName("jpg").
  • Create aImageWriteParam.
  • UsesetCompressionMode(ImageWriteParam.MODE_EXPLICIT) to set the compression mode.
  • UsesetCompressionQuality(quality) to set the compression quality.
  • Usewriter.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 InputStreamBufferedImage image = ImageIO.read(is);// get all image writers for JPG formatIterator<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 qualityparam.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 outputwriter.write(null, new IIOImage(image, null, null), param);// close all streamsis.close();os.close();ios.close();writer.dispose();}}

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

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
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 ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: May 9th, 2013
1 2,097 1 minute read
Photo of Ilias Tsagklis

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor atJava Code Geeks.

Related Articles

Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.