Tuesday, May 21, 2013

Compress image

The ImageCompressor program can be useful for you  to compress images. You can make the image smaller in size and at the same time its original quality reduces little. You need to use the IIOImage class to set the compress quality of the image.

ImageCompression source code:

import javax.imageio.*;
import javax.imageio.stream.*;
import java.io.*;

public class ImageCompressor{

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

}catch(ArrayIndexOutOfBoundsException aie){showMessage();}


public static void makeCompression(String inFileName,String outFileName)  throws Exception{

1 ImageReader imgReader = (ImageReader)ImageIO.getImageReadersByFormatName("jpg").next();
2 ImageWriter imgWriter =(ImageWriter) ImageIO.getImageWritersByFormatName("jpg").next();
3 ImageInputStream imgInputStream = ImageIO.createImageInputStream(new File(inFileName));
4 ImageOutputStream imgOutputStream = ImageIO.createImageOutputStream(new File(outFileName));
5 imgReader.setInput(imgInputStream);
6 imgWriter.setOutput(imgOutputStream);
7 IIOImage iioImg = new IIOImage(imgReader.read(0), null,null);
8 ImageWriteParam jpgWriterParam = imgWriter.getDefaultWriteParam();
9 jpgWriterParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
10 jpgWriterParam.setCompressionQuality(0.7f);
11 imgWriter.write(null, iioImg, jpgWriterParam);
//clean objects

public static void showMessage(){
System.out.println("Invalid files input! Please run the program in the form below:");
System.out.println("java ImageCompressor input_image_file output_image_file");

Size of original image is  108 KB.

image size before compression

 Size of the compressed image is 47.9 KB.

image size after compression

Code Explanation
1 Create a image reader object to read JPG image file.
2 Create a image writer object to write JPG image to an output file.
3 Create an image input stream object to be used for image stream reading.
4 Create an image output stream object to be used for image stream writing.
5 Set the image input stream object to the image reader object so it is ready to retrieve the image stream.
6 Set the image output stream object to the image writer object so it is ready to write the image stream to an output file.
7 Wrap the image stream in IIOImage object so you can specify the compression settings.
8 Create the image writer parameter object.
9 Set the compression mode to the image stream. It is to control the compression settings. Java provides four compression modes: MODE_COPY_FROM_METADATA, MODE_DEFAULT , MODE_DISABLED , and MODE_EXPLICIT.
10 Set the compression quality to the image stream. The compression quality value is in the range of 0.0 to 1.0. The 0.0 value means high compression so it is  the lowest quality compression. The value 1.0 is for
lowest compression so it is the highest quality image compression.
11 Write the image stream with the compession settings that contains in IIOImage object to the output file.

No comments:

Post a Comment