zoukankan      html  css  js  c++  java
  • 分享一个失真度较小的图片缩小方法

    获取 BufferedImage对象:

    BufferedImage bufferedImage = null;
            try {
                bufferedImage = ImageIO.read(new FileInputStream(path));
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }

    计算缩放比例按宽度缩放:

      double bo = (double) maxWidth / (double) imgWidth;
                bo = Math.round(bo * 10000) / 10000.0;

    实际缩放方法:

     /**
         * 图片等比缩放
         * 缩小后失真较小
         *
         * @param originalPic
         * @param bo
         * @return
         */
        public static BufferedImage reduceImg(BufferedImage originalPic, double bo) {
            // 获得原始图片的宽度。
            int originalImageWidth = originalPic.getWidth();
            // 获得原始图片的高度。
            int originalImageHeight = originalPic.getHeight();
    
            // 根据缩放比例获得处理后的图片宽度。
            int changedImageWidth = (int) (originalImageWidth * bo);
            // 根据缩放比例获得处理后的图片高度。
            int changedImageHeight = (int) (originalImageHeight * bo);
    
            BufferedImage tag = new BufferedImage(changedImageWidth, changedImageHeight,
                    BufferedImage.TYPE_INT_RGB);
    
            tag.getGraphics().drawImage(originalPic.getScaledInstance(changedImageWidth, changedImageHeight, Image.SCALE_SMOOTH), 0, 0, null);
    
            return tag;
        }
  • 相关阅读:
    pod
    jquery日历插件
    web前端常用api
    VUE.JS——脚手架安装
    github入门到上传本地项目
    appach修改默认端口之后数据库的访问
    在脚本中刷新impala元信息
    在脚本中刷新impala元信息
    不同hadoop集群之间迁移hive数据
    不同hadoop集群之间迁移hive数据
  • 原文地址:https://www.cnblogs.com/zhishan/p/3090469.html
Copyright © 2011-2022 走看看