主要是从手机端传图片到服务器,服务器会对图片进行一下压缩,
但是现在一直没有好的压缩方法,现在用的压缩方法,
总是有一定的概率出现图片是全白的情况,而且概率还不低,有2%~5%左右。
各位有什么好的压缩图片的方法吗?
或者说针对图片全白的情况有什么建议,
手机端上传图片到服务器有什么好的办法吗?

解决方案 »

  1.   

    javaweb的图片上传,倒是有图片上传,上传成功后,再压缩的方法倒是有,楼主要吗?
      

  2.   

    这个其实也是从网上摘的,希望有参考:
    ~~~~~~~~~~~~~~~~~~~~
    import java.awt.Graphics2D;
    import java.awt.geom.AffineTransform;
    import java.awt.image.AffineTransformOp;
    import java.awt.image.BufferedImage;
    import java.awt.Color;
    import java.io.*;
    import java.util.*;
    ...
    private String generateThumbnail(String fileNameRaw, String fileExt, int thumbWidth, int thumbHeight) {
    String ret = null;
    try {
    // Calculate thumbnail rate.
    File fileRaw = new File(fileNameRaw);
    BufferedImage rawImage = ImageIO.read(fileRaw);
    double rawWidth = rawImage.getWidth();
    double rawHeight = rawImage.getHeight();
    double rateWidth = thumbWidth / rawWidth;
    double rateHeight = thumbHeight / rawHeight;
    double rate = Math.min(rateWidth, rateHeight);
    rate = (Math.rint((rate * 100) + 0.5)) / 100;

    // Generate thumbnail image, using ext jpg.
    BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_USHORT_565_RGB);
    Graphics2D thumbGraph = thumbImage.createGraphics();
    thumbGraph.setBackground(Color.WHITE);
    thumbGraph.clearRect(0, 0, thumbWidth, thumbHeight);
    AffineTransform trans = new AffineTransform();
    trans.scale(rate, rate);
    AffineTransformOp transOP = new AffineTransformOp(trans, AffineTransformOp.TYPE_BILINEAR);
    thumbGraph.drawImage(rawImage, transOP, (int) (thumbWidth - (rawWidth * rate)) / 2, (int) (thumbHeight - (rawHeight * rate)) / 2);
    String fileNameThumb = fileNameRaw + fileExt;
    File fileThumb = new File(fileNameThumb);
    if (fileThumb.exists()) {
    fileThumb.delete();
    }
    fileThumb.createNewFile();
    ImageIO.write(thumbImage, "jpg", fileThumb);
    ret = fileNameThumb;
    } catch (Exception e) {

    }
    return ret;
    }
      

  3.   

    我有批量压缩图片的代码
    http://blog.csdn.net/yjflinchong/article/details/7579077刚发布的
      

  4.   

    ImageGaphick  压缩质量好,图片还非常的清晰,我就是用的这个库。