How to proceed jpeg Image file size after read--rotate-write operations in Java?

Posted by zamska on Stack Overflow See other posts from Stack Overflow or by zamska
Published on 2011-03-19T08:03:49Z Indexed on 2011/03/19 8:10 UTC
Read the original article Hit count: 230

Filed under:
|
|

Im trying to read a JPEG image as BufferedImage, rotate and save it as another jpeg image from file system. But there is a problem : after these operations I cannot proceed same file size.

Here the code

//read Image
BufferedImage img = ImageIO.read(new File(path));
//rotate Image
BufferedImage rotatedImage = new BufferedImage(image.getHeight(),
            image.getWidth(), BufferedImage.TYPE_3BYTE_BGR);
    Graphics2D g2d = (Graphics2D) rotatedImage.getGraphics();
    g2d.rotate(Math.toRadians(PhotoConstants.ROTATE_LEFT));

    int height=-rotatedImage.getHeight(null);
    g2d.drawImage(image, height, 0, null);
    g2d.dispose();

//Write Image
    Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
    ImageWriter writer = (ImageWriter)iter.next();
    // instantiate an ImageWriteParam object with default compression options
    ImageWriteParam iwp = writer.getDefaultWriteParam();
    try {
        FileImageOutputStream output = null;
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(0.98f);   // an integer between 0 and 1
        // 1 specifies minimum compression and maximum quality

        File file = new File(path);
        output = new FileImageOutputStream(file);
        writer.setOutput(output);
        IIOImage iioImage = new IIOImage(image, null, null);
        writer.write(null, iioImage, iwp);
        output.flush();
        output.close();
        writer.dispose();

Is it possible to access compressionQuality parameter of original jpeg image in the beginning. when I set 1 to compression quality, the image gets bigger size. Otherwise I set 0.9 or less the image gets smaller size. How can i proceed the image size after these operations?

Thank you,

© Stack Overflow or respective owner

Related posts about java

Related posts about size