java开发等比例压缩照片,传入byte数组,返回byte数组

    public static byte [] scaleImage(byte [] photo) {
        try {
            ByteArrayInputStream in = new ByteArrayInputStream(photo);    //将b作为输入流;
            BufferedImage image = ImageIO.read(in);     //将in作为输入流,读取图片存入image中,而这里in可以为ByteArrayInputStream();
            int imageWidth = image.getWidth();
            double scale = (double) 600 / imageWidth;
            int imageHeight = image.getHeight();
            int height = (int) (imageHeight * scale);
            int width = (int) (imageWidth * scale);
            AffineTransform scaleTransform = AffineTransform.getScaleInstance(scale, scale);
            AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);
            BufferedImage image1 = bilinearScaleOp.filter(image, new BufferedImage(width, height, image.getType()));
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            boolean flag = ImageIO.write(image1, "jpg", out);
            if(!flag){
                logger.info("压缩上传大数据抓拍照片失败");
            }
            photo = out.toByteArray();
        } catch (Exception e) {
            logger.error("图片压缩出错:", e);
            return photo;
        }
        return photo;
    }

借鉴:https://blog.csdn.net/wfs31415926/article/details/79392357/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值