• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How To Compress Image Using Java

April 21, 2014 //  by Krishna Srinivasan//  Leave a Comment

Compression of image file is one of the important task when it comes to save the large number of image files. It saves lot of space if you could compress the images when it is necessary. This example demonstrates how to compress the the JPEG file and reduce the size.

ImageWriteParam class is mainly used for compression the images. It has two methods which is mostly used for changing the compression settings.
setCompressionQuality() method used for setting the quality of the compressed image the value between 0 to 1. A compression quality setting of 0.0 is most generically interpreted as “high compression is important,” while a setting of 1.0 is most generically interpreted as “high image quality is important.” Also we used method setCompressionMode() to set the quality of the compression.

Here is the list of methods that are available in the class ImageWriteParam,

  1. protected boolean canOffsetTiles
  2. protected boolean canWriteCompressed
  3. protected boolean canWriteProgressive
  4. protected boolean canWriteTiles
  5. protected int compressionMode
  6. protected float compressionQuality
  7. protected String compressionType
  8. protected String[] compressionTypes
  9. protected Locale locale
  10. protected Dimension[] preferredTileSizes
  11. protected int progressiveMode
  12. protected int tileGridXOffset
  13. protected int tileGridYOffset
  14. protected int tileHeight
  15. protected boolean tilingSet

Lets look at the example which compress the given image and output the image with small in size. Lets try this example, post your comments.

CompressJPEGFileExample.ajav

[code lang=”java”] package javabeat.net.core;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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;

/**
* Compress JPEG File Example
*
* @author Krishna
*
*/
public class CompressJPEGFileExample {
public static void main(String[] args) throws FileNotFoundException, IOException{
File imageFile = new File("Desert.jpg");
File compressedImageFile = new File("compressed_file.jpg");

InputStream inputStream = new FileInputStream(imageFile);
OutputStream outputStream = new FileOutputStream(compressedImageFile);

float imageQuality = 0.3f;

//Create the buffered image
BufferedImage bufferedImage = ImageIO.read(inputStream);

//Get image writers
Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersByFormatName("jpg");

if (!imageWriters.hasNext())
throw new IllegalStateException("Writers Not Found!!");

ImageWriter imageWriter = (ImageWriter) imageWriters.next();
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputStream);
imageWriter.setOutput(imageOutputStream);

ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam();

//Set the compress quality metrics
imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
imageWriteParam.setCompressionQuality(imageQuality);

//Created image
imageWriter.write(null, new IIOImage(bufferedImage, null, null), imageWriteParam);

// close all streams
inputStream.close();
outputStream.close();
imageOutputStream.close();
imageWriter.dispose();
}
}
[/code]

I hope this tutorial helped you to understand how to compress a file using Java. There are various techniques to compress the image file, if you have used any other good techniques to compress a image files, please share with us here.

Category: JavaTag: Java ImageIO

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « How To Get Image Format Using Java
Next Post: How To Get Supported Image Formats Using Java »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

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

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact