English 中文(简体)
DIP - Image Compression Technique
  • 时间:2024-12-22

Java DIP - Image Compression Technique


Previous Page Next Page  

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> pst = ImageIO.getImageWritersByFormatName("jpg");
ImageWriteParam obj = writer_From_List.getDefaultWriteParam();

From this ImageWriteParam object, you can set the compression by calpng these two methods which are setCompressionMode() and setCompressionQuapty(). Their syntaxes are as given below −

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

The setCompressionMode() method takes Mode_EXPLICIT as the parameter. Some of the other MODES are described briefly −

Sr.No. Modes
1

MODE_DEFAULT

It is a constant value that may be passed into methods to enable that feature for future writes.

2

MODE_DISABLED

It is a constant value that may be passed into methods to disable that feature for future writes.

3

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 −

Sr.No. Method & Description
1

canOffsetTiles()

It returns true if the writer can perform tipng with non-zero grid offsets while writing.

2

getBitRate(float quapty)

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 quapty level.

3

getLocale()

It returns the currently set Locale, or null if only a default Locale is supported.

4

isCompressionLossless()

It returns true if the current compression type provides lossless compression.

5

unsetCompression()

It removes any previous compression type and quapty settings.

6

unsetTipng()

It removes any previous tile grid parameters specified by calls to setTipng.

Example

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 {

   pubpc 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.setCompressionQuapty(0.05f);
      writer.write(null, new IIOImage(image, null, null), param);
      
      os.close();
      ios.close();
      writer.dispose();
   }
}

Output

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.

Original Image

Image Compression Technique Tutorials

Compressed Image - Quapty Factor − 0.05

Image Compression Technique Tutorials

Compressed Image - Quapty Factor − 0.5

Image Compression Technique Tutorials Advertisements