2011-01-06 62 views
1

降低图像的缩略图大小与大小4.7kb的图像A.JPG,当其上传至网络服务器,它的大小成为15.5KB ...我如何在Java

我使用下面的代码。

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(..)......情况如下:

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; 
} 

任何其它尺寸和质量优化理念,同时使用Java保存图像。我正在使用struts2 MVC ...非常感谢你。

回答

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

这看起来像是JPEG缩略图的高质量。尝试60或50左右。

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

咦?

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

OK ..

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

不过吧?为什么设置JPEGEncodeParam并将其存储为PNG?这甚至有什么作用?试试..

ImageIO.write(thumbImage, "jpg", out); 
+0

是的,有些代码只是多余的...我刚刚从一些文章,并试试看。如果你知道更好的选择......我可以替换这整个代码。 对不起...它没有任何作用...是否我们写png,jpg,..等 在50 ...质量值..图像质量变坏。虽然规模减少了约35%。谢谢 – 2011-01-06 11:38:57