Compress Images Java Example
This tutorial shows how to compress images in Java. You can reduce an image file size by compressing an image. This file reduction sometimes leads to increased performance and decreased network bandwidth. But when you compress an image you’ll loose some information/quality of the image.
Compress Images Java Example
The following example demonstrates the use of ImageWriteParam
class to compress an image. We obtain the ImageWriteParam
by calling the ImageWriter.getDefaultWriteParam()
. Next we need to check if the image can be compressed by calling the ImageWriteParam.canWriteCompressed()
. Note: this’ll return false when reading PNG
images. We set the compressionMode
to ImageWriteParam.MODE_EXPLICIT
and the compressionQuality
to 0.5f
.
ImageWriteParam.MODE_DISABLED
– when this mode is set the stream will not be tiled, progressive, or compressed..ImageWriteParam.MODE_DEFAULT
– when this mode is enabled the stream will be tiled, progressive, or compressed according to a sensible default chosen internally by the writer in a plug-in dependent way.ImageWriteParam.MODE_EXPLICIT
– when this mode is set the stream will be tiled or compressed according to additional information supplied to the correspondingset
methods in this class. Note that this mode is not supported for progressive output.ImageWriteParam.MODE_COPY_FROM_METADATA
– when this mode is enabled the stream will be tiled, progressive, or compressed based on the contents of stream and/or image metadata passed into the write operation.
package com.memorynotfound.image;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CompressImages {
public static void main(String... args) throws IOException {
File input = new File("/tmp/duke.jpg");
BufferedImage image = ImageIO.read(input);
File output = new File("/tmp/duke-compressed-005.jpg");
OutputStream out = new FileOutputStream(output);
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
ImageOutputStream ios = ImageIO.createImageOutputStream(out);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
if (param.canWriteCompressed()){
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.05f);
}
writer.write(null, new IIOImage(image, null, null), param);
out.close();
ios.close();
writer.dispose();
}
}
Converted Images
To demonstrate the result, we added the following table. The image on the left is the original. The second compressed image has a quality factor of 0.05f
. The last compressed image has a quality factor of 0.005f
. Notice the different file sizes of each image. Consequently the lower you set the quality factor, the lower the image quality becomes but also the lower the file size becomes.
duke.jpg (original) | duke-compressed-05.jpg | duke-compressed-005.jpg |
---|---|---|
Size: 12KB | Size: 9KB | Size: 4KB |