java创建缩略图

public void createSmallPic(int height, int width){
  String smallPath = "smallPath"; // 小图存放目录   
  if(!new File(smallPath).exists()) { 
    new File(smallPath).mkdirs(); 
  }
  double ratio = 0.0;
  File file = new File("d:/a.jpg"); // 大图路径
  BufferedImage bi = ImageIO.read(file); // 读取大图
  if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
    if (bi.getHeight() > bi.getWidth()) { 
      ratio = (new Integer(height)).doubleValue() / bi.getHeight();
    } else {
      ratio = (new Integer(width)).doubleValue() / bi.getWidth();
    }
    file = new File(smallPath + File.separator + file.getName() + "_" + 
                         height + "_" + width + ".jpg"); // 缩略图路径
    Image img = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
    AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
    img = op.filter(bi, null);
    ImageIO.write((BufferedImage) img, "jpg", file);
  }
}
原文地址:https://www.cnblogs.com/makar/p/3250852.html