How to compress JPEG in Java

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


In this code, we implement a method for compressing a JPEG file with a specific compression quality.

// Reads the jpeg image in infile, compresses the image,
// and writes it back out to outfile.
// compressionQuality ranges between 0 and 1,
// 0-lowest, 1-highest.
public void compressJpegFile(File infile, File outfile, float compressionQuality) {
    try {
        // Retrieve jpg image to be compressed
        RenderedImage rendImage = ImageIO.read(infile);
 
        // Find a jpeg writer
        ImageWriter writer = null;
        Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
        if (iter.hasNext()) {
            writer = (ImageWriter)iter.next();
        }
 
        // Prepare output file
        ImageOutputStream ios = ImageIO.createImageOutputStream(outfile);
        writer.setOutput(ios);
 
        // Set the compression quality
        ImageWriteParam iwparam = new MyImageWriteParam();
        iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) ;
        iwparam.setCompressionQuality(compressionQuality);
 
        // Write the image
        writer.write(null, new IIOImage(rendImage, null, null), iwparam);
 
        // Cleanup
        ios.flush();
        writer.dispose();
        ios.close();
    } catch (IOException e) {
    }
}
 
// This class overrides the setCompressionQuality() method to workaround
// a problem in compressing JPEG images using the javax.imageio package.
public class MyImageWriteParam extends JPEGImageWriteParam {
    public MyImageWriteParam() {
        super(Locale.getDefault());
    }
 
    // This method accepts quality levels between 0 (lowest) and 1 (highest) and simply converts
    // it to a range between 0 and 256; this is not a correct conversion algorithm.
    // However, a proper alternative is a lot more complicated.
    // This should do until the bug is fixed.
    public void setCompressionQuality(float quality) {
        if (quality < 0.0F || quality > 1.0F) {
            throw new IllegalArgumentException("Quality out-of-bounds!");
        }
        this.compressionQuality = 256 - (quality * 256);
    }
}





0 points

please give me the full code, badly i need it..

Anonymous's picture
Created by Anonymous

Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.