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;
        }
  • 相关阅读:
    java解析xml
    支持向量机SVM
    资源-菜单
    GIT
    基于OpenCV的图书扫描识别程序开发
    最大公约数(gcd)还有最小公倍数(lcm)的共通之处
    python网页分析
    python爬虫的基本知识储备
    大数加法 (A + B Problem II)
    Andy's First Dictionary (set)
  • 原文地址:https://www.cnblogs.com/zhishan/p/3090469.html
Copyright © 2011-2022 走看看