springBoot处理上传图片进行角度旋转

springBoot处理上传图片进行旋转

/**
* 处理图片公共方法
**/
public class ImageUtil {

    public static MultipartFile changeThePicture(MultipartFile file,double degree) throws IOException{
        // 读取图片
        BufferedImage srcImage = ImageIO.read(file.getInputStream());

        // 计算旋转后的新尺寸
        int width = srcImage.getWidth();
        int height = srcImage.getHeight();
        double sin = Math.abs(Math.sin(Math.toRadians(degree))),
                cos = Math.abs(Math.cos(Math.toRadians(degree)));
        int newWidth = (int) Math.floor(width * cos + height * sin),
                newHeight = (int) Math.floor(height * cos + width * sin);

        // 创建一个新的空白图片用于将原始图片绘制上去
        BufferedImage rotatedImage = new BufferedImage(newWidth, newHeight, srcImage.getType());
        Graphics2D g2d = rotatedImage.createGraphics();

        // 设置旋转的中心点为新图片的中心
        AffineTransform at = new AffineTransform();
        at.translate((newWidth - width) / 2, (newHeight - height) / 2);
        at.rotate(Math.toRadians(degree), width / 2, height / 2);
        g2d.setTransform(at);

        // 绘制图片
        g2d.drawImage(srcImage, 0, 0, null);
        g2d.dispose();

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        //这里是注意点:文件类型 要和 原始文件的后缀类型一致,否则会出现下面 byte[] imageBytes =null的情况
        ImageIO.write(rotatedImage, "png", outputStream);
        byte[] imageBytes = outputStream.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(imageBytes);
        // 这里使用的是Spring的MockMultipartFile,实际项目中可能需要自定义MULTIPARTFILE的实现
        String ex = file.getOriginalFilename().substring(0,file.getOriginalFilename().lastIndexOf("."));
        MultipartFile multipartFile = new MockMultipartFile(ex, file.getOriginalFilename(), "image/jpeg", bais);
        return multipartFile;
    }
}
/**
*controller接口
**/
@PostMapping("/uploadSignePicture")
    @Operation(summary = "上传签名文件")
    public CommonResult<Map<String, String>> uploadSignePicture(@RequestParam("file") MultipartFile file) {
        // 创建上传容器对象
        try {
            S3FileClient fileClient = new S3FileClient(1L, s3FileClientConfig);
            fileClient.init();
            //图片翻转270
            file =  ImageUtil.changeThePicture(file,270);


            InputStream in = file.getInputStream();
            byte[] content = in.readAllBytes();
            String originalFilename = file.getOriginalFilename();
            if ("".equals(originalFilename)) {
                return CommonResult.error(500, "请选择文件上传!");
            }
            String path = FileUtils.generatePath(content, originalFilename);
            //将文件根据项目名、日期进行分类管理
            String nowDate = String.valueOf(LocalDate.now());
            path = applicationName + "/" + nowDate + "/" + path;

            // 文件类型 非后缀
            String type = FileTypeUtils.getMineType(content, originalFilename);
            in.close();

            //文件大小
            String url = fileClient.upload(content, path, type);

            Map<String, String> map = new HashMap<>();
            map.put("filePath", String.format("/%s%s", applicationName, url.substring(url.lastIndexOf("/", url.lastIndexOf("/") - 10))));
            map.put("fileName", originalFilename);
            return success(map);
        } catch (Exception e) {
            log.error("minio文件上传失败", e);
            return CommonResult.error(500, "文件上传失败,请稍后再试!");
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@hhr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值