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;
        }
  • 相关阅读:
    前端常用布局
    Vue, React, AngularJS, Angular2 我们对流行JavaScript框架们的选择
    使用vue-cli创建vue项目
    Null component Catalina
    初识MongoDB
    小米平板2 win10 MIUI互刷教程
    tomcat报错
    websocket
    前后端数据交互方法
    二叉排序树
  • 原文地址:https://www.cnblogs.com/zhishan/p/3090469.html
Copyright © 2011-2022 走看看