zoukankan      html  css  js  c++  java
  • 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);
      }
    }
  • 相关阅读:
    day3 集合
    进度条
    day3 文件操作 seek tell 修改
    day3 函数
    同学满分代码,购物车。
    day2杂---三元运算 is
    模块sys os
    day2--列表/元组/字符串/字典
    一、Git配置
    四、TestNG 批量执行脚本Runner.xml
  • 原文地址:https://www.cnblogs.com/makar/p/3250852.html
Copyright © 2011-2022 走看看