java图片截取中间指定长度的方图片

1、控制器代码:

 //上传图片
    @PostMapping("/imgOtherUpload")
    public ResponseResult uploadOtherImg(@RequestParam("multipartFile") MultipartFile multipartFile,Integer checkUserId) {
        try {

            if (multipartFile.isEmpty() || StringUtils.isBlank( multipartFile.getOriginalFilename() )) {
                return new ResponseResult( CommonCode.INVALID_PARAM );
            }
            String contentType = multipartFile.getContentType();
            if (!contentType.contains( "image" )) {
                return new ResponseResult( CommonCode.INVALID_PARAM );
            }
            String root_fileName = multipartFile.getOriginalFilename();
            logger.info( "上传图片:name={},type={}", root_fileName, contentType );

            String date = DateFormatUtils.format( new Date(), "/yyyy/MM/dd/" );
            String directoryName = "other";
            String  filePath = location + "/"+directoryName  + date;


            logger.info( "图片保存路径={}", filePath );
            String suff = root_fileName.substring( root_fileName.lastIndexOf( "." ) );
            //原图
            String uuid = UUID.randomUUID().toString();
            String file_name_max = uuid+"_max" + suff;
            try {
                FileUtils.saveFile( multipartFile, filePath, file_name_max );
            } catch (IOException e) {
                e.printStackTrace();
            }
            //指定截取的小图片
            String file_name_min = uuid+"_min" + suff;
            FileUtils.cutImage( multipartFile,filePath,file_name_min,0,0,imgWidth,imgHeight );

            HashMap<String, String> map = new HashMap<>();
            map.put( "img_path", "/"+directoryName+date + file_name_min );
            map.put( "img_show", domain +"/"+directoryName+ date + file_name_min );
            return new ResponseResult( CommonCode.SUCCESS, map );

        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult( CommonCode.FAIL );
        }

    }

2、截取代码:

 /**
     * 指定坐标,截取正方形的图片
     *
     * @param multipartFile 图片
     * @param path          路径
     * @param fileName      文件名
     * @param x             起始x位置
     * @param y             起始y位置
     * @param newWidth      要截取的宽度
     * @param newHeight     要截取的高度
     * @throws IOException
     */
    public static void cutImage(MultipartFile multipartFile, String path, String fileName, int x,
                              int y, int newWidth, int newHeight) throws IOException {
        ImageInputStream imageStream = null;
        OutputStream oStream = null;
        try {
            String imageType = multipartFile.getContentType().substring( multipartFile.getContentType().indexOf( "/" ) + 1 );
            System.out.println(imageType);
            //保存路径
            String des = path + fileName;
            oStream = new FileOutputStream( des );

            //ImageIO获取图片流信息
            Image image = ImageIO.read( multipartFile.getInputStream() );
            int width = image.getWidth( null ); //获取原图宽度
            int height = image.getHeight( null );//获取原图高度
            if (width < newWidth) {
                newWidth = width;
                newHeight = width;
            }
            if (height < newWidth) {
                newWidth = width;
                newHeight = width;
            }
            //从原始图的中间截取方图片
            if (width > newWidth) {
                x = (width - newWidth) / 2;
            }

            if (height > newWidth) {
                y = (height - newWidth) / 2;
            }

            Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName( imageType );
            ImageReader reader = readers.next();
            imageStream = ImageIO.createImageInputStream( multipartFile.getInputStream() );
            reader.setInput( imageStream, true );
            ImageReadParam param = reader.getDefaultReadParam();

            Rectangle rect = new Rectangle( x, y, newWidth, newHeight );
            param.setSourceRegion( rect );
            BufferedImage bi = reader.read( 0, param );
            ImageIO.write( bi, imageType, oStream );
        } finally {
            imageStream.close();
            oStream.close();
        }
    }

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值