1

I have an image a.jpg with size 4.7kb, when its upload to web-server, its size become 15.5KB ...

I am using following code.

BufferedImage bi = ImageIO.read(business.getImage()); //business is sturts2 Form & image instance of File class
            int height = bi.getHeight();
            int width = bi.getWidth();

            if (height > Constants.BIZ_IMAGE_HEIGHT || width > Constants.BIZ_IMAGE_WIDTH) {
                height = Constants.BIZ_IMAGE_HEIGHT;
                width = Constants.BIZ_IMAGE_WIDTH;
            }

            InputStream is = UtilMethod.scaleImage(new FileInputStream(business.getImage()), width, height);
            File f = new File(businessImagesPath, business.getImageFileName());
            UtilMethod.saveImage(f, is);
            is.close();

UtilMethod.scaleImage(..) ... is as follow:

public static InputStream scaleImage(InputStream p_image, int p_width, int p_height) throws Exception {

    InputStream imageStream = new BufferedInputStream(p_image);
    Image image = (Image) ImageIO.read(imageStream);

    int thumbWidth = p_width;
    int thumbHeight = p_height;

    // Make sure the aspect ratio is maintained, so the image is not skewed
    double thumbRatio = (double) thumbWidth / (double) thumbHeight;
    int imageWidth = image.getWidth(null);
    int imageHeight = image.getHeight(null);
    double imageRatio = (double) imageWidth / (double) imageHeight;
    if (thumbRatio < imageRatio) {
        thumbHeight = (int) (thumbWidth / imageRatio);
    } else {
        thumbWidth = (int) (thumbHeight * imageRatio);
    }

    // Draw the scaled image
    BufferedImage thumbImage = new BufferedImage(thumbWidth,
            thumbHeight, BufferedImage.TYPE_INT_RGB);

    Graphics2D graphics2D = (Graphics2D) thumbImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, Color.WHITE, null);


    // Write the scaled image to the outputstream
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);

    int quality = 85; // Use between 1 and 100, with 100 being highest quality
    quality = Math.max(0, Math.min(quality, 100));

    param.setQuality((float) quality / 100.0f, false);
    encoder.setJPEGEncodeParam(param);        
    encoder.encode(thumbImage);
    ImageIO.write(thumbImage, "png", out);

    ByteArrayInputStream bis = new ByteArrayInputStream(out.toByteArray());
    return bis;
}

Any other size and quality optimization idea while saving images using java. I am using struts2 MVC ... thank u so much.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
Asif Shahzad
  • 843
  • 2
  • 11
  • 21

1 Answers1

2
int quality = 85; // Use between 1 and 100, with 100 being highest quality

This seems like a high quality for a JPEG thumbnail. Try around 60 or 50.

quality = Math.max(0, Math.min(quality, 100));

Huh?

param.setQuality((float) quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);        

OK..

encoder.encode(thumbImage);
ImageIO.write(thumbImage, "png", out);

But huh? Why set a JPEGEncodeParam and store as a PNG? Does that even have any effect? Try..

ImageIO.write(thumbImage, "jpg", out);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • yea, some code is just redundant ... I just got from some article and give it a try. if u know a better alternative ... I can replace this whole code. u r right .. it do not have any effect ... whether we write png, jpg, .. etc. at 50 ... quality value .. the image quality goes bad. although size is decreased by about 35%. thanks – Asif Shahzad Jan 06 '11 at 11:38