spring boot实现图片裁剪

Java对图片按需进行裁剪

  1. 按照需求对图片进行裁剪
  2. 例如 原图 2000*600 目标图片 1000*300

/**
     * 图片裁剪
     */
    public void contextLoads() {
        try {
            InputStream inputStream = new FileInputStream("D:\\upload\\cp0.jpg");
            BufferedImage bufferedImage = ImageIO.read(inputStream);
            BufferedImage bf = new BufferedImage(1000,300,BufferedImage.TYPE_INT_BGR);  //1000*300需要裁剪的大小
            bf.getGraphics().drawImage(bufferedImage,0,0,1000,300,null);
            
            FileOutputStream outImgStream = new FileOutputStream("D:\\upload\\cp01.jpg");  
            ImageIO.write(bufferedImage, "jpg", outImgStream);   //直接存储到本地
            

            //暂时存储为字节流用于上传到服务器,我用的是ftp上传
            ByteArrayOutputStream baty = new ByteArrayOutputStream();
            ImageIO.write(bf,"jpg",baty);
            byte[]  buff =  baty.toByteArray();
            InputStream in =  new ByteArrayInputStream(buff); //存储为字节流,用于上传到服务器
            
            FileOutputStream fos = new FileOutputStream("d:\\aa.jpg");  //转换为字节流存储到本地的测试方法,证明转换成功
            
            byte[] b = new byte[1024];
            int length;
            while((length = in.read(b)) != -1 ){
                fos.write(b,0,length);  
            }
            in.close();
            fos.close();

        }catch (Exception e){
            e.printStackTrace();
        }

    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值