亲宝软件园·资讯

展开

Java图片比率缩放

Yweir 人气:0

通过Thumbnails实现图片缩放

需要导入pom依赖,可以到中央仓库获取最小的工具包

<dependency>
       <groupId>net.coobird</groupId>
       <artifactId>thumbnailator</artifactId>
        <version>0.4.8</version>
</dependency>
//读取图片
BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(out.toByteArray()));
ByteArrayOutputStream out2 = new ByteArrayOutputStream();  
    Thumbnails.of(bufferedImage).size(750,1344).outputFormat("png").toOutputStream(out2);//缩放图片
    InitImage("缩放图", out2.toByteArray());//显示图片

java API实现图片缩放

调用方法

InitImage("自定义压缩图",  zoomBySize(750,1334,bufferedImage,"png"));//调用方法

具体方法实现1

  /**
     *@description: 按比例对图片进行缩放. 检测图片是横图还是竖图
     *@param : width 缩放后的宽
     *@param : height 缩放后的高
     *@param : img BufferedImage
     *@param : ext 图片类型 如: jpg
     *@author: YWR
     *@return:
     *@throws:
     *@date: 2020/9/17 0:08
     */
    public static byte[] zoomBySize(int width, int height, BufferedImage img, String ext) throws IOException {
        //横向图
        if (img.getWidth() >= img.getHeight()) {
            double ratio = CalculateZoomRatio(width, img.getWidth());
            //获取压缩对象
            BufferedImage newbufferedImage = zoomByScale(ratio, img, ext);
            //当图片大于图片压缩高时 再次缩放
            if (newbufferedImage.getHeight() > height) {
                ratio = CalculateZoomRatio(height, newbufferedImage.getHeight());
                newbufferedImage = zoomByScale(ratio, img, ext);

            }
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(newbufferedImage, ext, out);
            return out.toByteArray();
        }
        //纵向图
        if (img.getWidth() < img.getHeight()) {
            double ratio = CalculateZoomRatio(height, img.getHeight());
            //获取压缩对象
            BufferedImage newbufferedImage = zoomByScale(ratio, img, ext);
            //当图片宽大于图片压缩宽时 再次缩放
            if (newbufferedImage.getHeight() > height) {
                ratio = CalculateZoomRatio(width, newbufferedImage.getWidth());
                newbufferedImage = zoomByScale(ratio, img, ext);
            }
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(newbufferedImage, ext, out);
            return out.toByteArray();
        }

        //以上都不符合时 强制缩放
        Image _img = img.getScaledInstance(width, height, Image.SCALE_DEFAULT);
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = image.createGraphics();
        graphics.drawImage(_img, 0, 0, null);
        graphics.dispose();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(image, ext, out);
        return out.toByteArray();
    }

    /**
     *@description: 按比例对图片进行缩放.
     *@param : scale 缩放比率
     *@param : img BufferedImage
     *@param : ext 图片类型
     *@author: YWR
     *@return:
     *@throws:
     *@date: 2020/9/17 0:07
     */
    public static BufferedImage zoomByScale(double scale, BufferedImage img, String ext) throws IOException {
        //获取缩放后的长和宽
        int _width = (int) (scale * img.getWidth());
        int _height = (int) (scale * img.getHeight());
        //获取缩放后的Image对象
        Image _img = img.getScaledInstance(_width, _height, Image.SCALE_DEFAULT);
        //新建一个和Image对象相同大小的画布
        BufferedImage image = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB);
        //获取画笔
        Graphics2D graphics = image.createGraphics();
        //将Image对象画在画布上,最后一个参数,ImageObserver:接收有关 Image 信息通知的异步更新接口,没用到直接传空
        graphics.drawImage(_img, 0, 0, null);
        //释放资源
        graphics.dispose();
        return image;
    }
    /**
     *@description: 缩放比率计算
     *@param : divisor
     *@param : dividend
     *@author: YWR
     *@return:
     *@throws:
     *@date: 2020/9/17 0:07
     */
    private static double CalculateZoomRatio(int divisor, int dividend) {
        return BigDecimal.valueOf(divisor).divide(BigDecimal.valueOf(dividend), 6, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

实现方法2

 /**
     *@description: 按比例对图片进行缩放. 检测图片是横图还是竖图
     *@param : width 缩放后的宽
     *@param : height 缩放后的高
     *@param : img BufferedImage
     *@param : ext 图片类型 如: jpg
     *@author: YWR
     *@return:
     *@throws:
     *@date: 2020/9/17 0:08
     */
    public static byte[] zoomByScale(int width, int height, BufferedImage img, String ext) throws IOException {
        //横向图
        if (img.getWidth() >= img.getHeight()) {
            double ratio = CalculateZoomRatio(width, img.getWidth());
            //获取压缩对象
            BufferedImage newbufferedImage = zoomByScale(ratio, img, ext);
            //当图片大于图片压缩高时 再次缩放
            if (newbufferedImage.getHeight() > height) {
                ratio = CalculateZoomRatio(height, newbufferedImage.getHeight());
                newbufferedImage = zoomByScale(ratio, img, ext);

            }
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(newbufferedImage, ext, out);
            return out.toByteArray();
        }
        //纵向图
        if (img.getWidth() < img.getHeight()) {
            double ratio = CalculateZoomRatio(height, img.getHeight());
            //获取压缩对象
            BufferedImage newbufferedImage = zoomByScale(ratio, img, ext);
            //当图片宽大于图片压缩宽时 再次缩放
            if (newbufferedImage.getHeight() > height) {
                ratio = CalculateZoomRatio(width, newbufferedImage.getWidth());
                newbufferedImage = zoomByScale(ratio, img, ext);
            }
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(newbufferedImage, ext, out);
            return out.toByteArray();
        }

        //以上都不符合时 强制缩放
        Image _img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        // 获取缩放比例
        double wr=0,hr=0;
        wr = width * 1.0 / img.getWidth(); 
        hr = height * 1.0 / img.getHeight();
        AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(wr, hr), null);
        _img = ato.filter(img, null);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write((BufferedImage) _img,ext,out);//写入缩减后的图片
        return out.toByteArray();
    }

    /**
     *@description: 按比例对图片进行缩放.
     *@param : scale 缩放比率
     *@param : img BufferedImage
     *@param : ext 图片类型
     *@author: YWR
     *@return:
     *@throws:
     *@date: 2020/9/17 0:07
     */
    public static BufferedImage zoomByScale(double scale, BufferedImage img, String ext) throws IOException {
        //获取缩放后的长和宽
        int _width = (int) (scale * img.getWidth());
        int _height = (int) (scale * img.getHeight());
      //设置缩放目标图片模板
        Image _img = img.getScaledInstance(_width, _height, Image.SCALE_SMOOTH);
        //缩放图片
        AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(scale, scale), null);
        _img = ato.filter(img, null);
        return (BufferedImage) _img;
    }
    /**
     *@description: 缩放比率计算
     *@param : divisor
     *@param : dividend
     *@author: YWR
     *@return:
     *@throws:
     *@date: 2020/9/17 0:07
     */
    private static double CalculateZoomRatio(int divisor, int dividend) {
        return BigDecimal.valueOf(divisor).divide(BigDecimal.valueOf(dividend), 6, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

加载全部内容

相关教程
猜你喜欢
用户评论