Java DIP - Image Compression Technique

An image can easily be compressed and stored through Java. Compression of image involves converting an image into jpg and storing it.
In order to compress an image, we read the image and convert into BufferedImage object.
Further, we 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 syntaxes are as given below −
obj.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
obj.setCompressionQuality(0.05f);

The setCompressionMode() method takes Mode_EXPLICIT as the parameter. Some of the other MODES are described briefly −
MODE_DEFAULT
It is a constant value that may be passed into methods to enable that feature for future writes.
MODE_DISABLED
It is a constant value that may be passed into methods to disable that feature for future writes.
MODE_EXPLICIT
It is 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 provided by the ImageWriteParam class. They are described briefly −
canOffsetTiles()
It returns true if the writer can perform tiling with non-zero grid offsets while writing.
getBitRate(float quality)
It 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.
getLocale()
It  returns the currently set Locale, or null if only a default Locale is supported.
isCompressionLossless()
It returns true if the current compression type provides lossless compression.
unsetCompression()
It removes any previous compression type and quality settings.
unsetTiling()
It removes any previous tile grid parameters specified by calls to setTiling.
The following example demonstrates the use of ImageWriteParam class to compress an image −
import java.io.*;
import java.util.*;
import java.awt.image.*;

import javax.imageio.*;
import javax.imageio.stream.ImageOutputStream;

class Compression {

   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();
   }
}
When you execute the given code, it compresses the image digital_image_processing.jpg to its equivalent compressed image and writes it on the hard disk with the name compress.jpg.

    16 Lectures 
     2 hours 

 Malhar Lathkar

    19 Lectures 
     5 hours 

 Malhar Lathkar

    25 Lectures 
     2.5 hours 

 Anadi Sharma

    126 Lectures 
     7 hours 

  Tushar Kale

    119 Lectures 
     17.5 hours 

 Monica Mittal

    76 Lectures 
     7 hours 

 Arnab Chakraborty
 Print
 Add Notes
 Bookmark this page