Image Compression Technique Tutorial

Advertisements


An image can easily be compressed and stored through Java. Compression of image involves converting an image to jpg and storing it.

In order to compress an image, first thing you need to do is to read the image and convert into BufferedImage object.

After that you need to get an ImageWriter from getImageWritersByFormatName method found in the ImageIO class. From this ImageWriter, create an ImageWriteParam object. Its syntax is given below:

Iterator<ImageWriter> list = ImageIO.getImageWritersByFormatName("jpg");
ImageWriteParam obj = writer_From_List.getDefaultWriteParam();

From this ImageWriteParam object, you can set the compression by calling these two methods which are setCompressionMode and setCompressionQuality.Their syntax is given below:

obj.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
obj.setCompressionQuality(0.05f);

The setCompressionMode methods takes Mode_EXPLICIT as the parameter. Some of the other MODES are listed below:

Sr.NoMode & Descrioption
1MODE_DEFAULT
A constant value that may be passed into methods to enable that feature for future writes.
2MODE_DISABLED
A constant value that may be passed into methods to disable that feature for future writes.
3MODE_EXPLICIT
A constant value that may be passed into methods to enable that feature for future writes.

Apart from the compressions methods, there are other methods provide by the ImageWriteParam class. They are listed below:

Sr.NoMethod & Description
1canOffsetTiles()
Returns true if the writer can perform tiling with non-zero grid offsets while writing.
2getBitRate(float quality)
Returns a float indicating an estimate of the number of bits of output data for each bit of input image data at the given quality level.
3getLocale()
Returns the currently set Locale, or null if only a default Locale is supported.
4isCompressionLossless()
Returns true if the current compression type provides lossless compression.
5unsetCompression()
Removes any previous compression type and quality settings.
6unsetTiling()
Removes any previous tile grid parameters specified by calls to setTiling.

Example

The following example demonstrates the use of ImageWriteParam class to compress an image.

import java.io.*;
import java.util.*;
import javax.imageio.*;
import java.awt.image.*;
import javax.imageio.stream.ImageOutputStream;

class Compresssion {

   public static void main(String[] args) throws IOException {
      File input = new File("digital_image_processing.jpg");
      BufferedImage image = ImageIO.read(input);

      File compressedImageFile = new File("compress.jpg");
      OutputStream os =new FileOutputStream(compressedImageFile);

      Iterator<ImageWriter>writers = 
      ImageIO.getImageWritersByFormatName("jpg");
      ImageWriter writer = (ImageWriter) writers.next();

      ImageOutputStream ios = ImageIO.createImageOutputStream(os);
      writer.setOutput(ios);

      ImageWriteParam param = writer.getDefaultWriteParam();
      param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
      param.setCompressionQuality(0.05f);
      writer.write(null, new IIOImage(image, null, null), param);
      os.close();
      ios.close();
      writer.dispose();
   }
}

Output

When you run the above example, it would compress an image name digital_image_processing.jpg to its equivalent compressed image and write it on hard disk with name compress.jpg.

Original Image

Image Compression Technique Tutorials

Compressed Image - Quality Factor: 0.05

Image Compression Technique Tutorials

Compressed Image - Quality Factor: 0.5

Image Compression Technique Tutorials

Advertisements
Advertisements