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,
- protected boolean canOffsetTiles
- protected boolean canWriteCompressed
- protected boolean canWriteProgressive
- protected boolean canWriteTiles
- protected int compressionMode
- protected float compressionQuality
- protected String compressionType
- protected String[] compressionTypes
- protected Locale locale
- protected Dimension[] preferredTileSizes
- protected int progressiveMode
- protected int tileGridXOffset
- protected int tileGridYOffset
- protected int tileHeight
- 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
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(); } }
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.