分享一个失真度较小的图片缩小方法

获取 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;
    }

 

posted @ 2013-05-21 11:55  王 庆  阅读(398)  评论(0编辑  收藏  举报