MultipartFile图片文件转webp格式

前端加载大图片响应慢 提升效率可以把图片转为webp格式(会影响清晰度)

直接看代码吧

获取文件的名称前缀后缀 然后传入写好的工具类 保存到传入的路径

			//判断文件路径是否存在
            String directoryPath = "file/djiAudioFile/";
            FileDirectoryPath.newFile(directoryPath);

			//服务器路径 判断是否有文件后缀得文件夹没有则根据文件后缀创建文件再存入
            String Sfilepash = directoryPath + suffix;
            FileDirectoryPath.newFile(Sfilepash);

			//根据路径写入文件
            WriteToFileExample.writeToFile(file,Sfilepash + "/" + before + "." + suffix);//源文件
			
			String modelOriginalFilename = file.getOriginalFilename();
            String before = modelOriginalFilename.substring(0, modelOriginalFilename.indexOf("."));
            String suffix = modelOriginalFilename.substring(before.length() + 1);

			//传入的图片转webp格式图片
            ImageThumbnailUtil.jpg2webp(Sfilepash + "/" + before + "." + suffix,Sfilepash + "/" + before + "webp.webp");

然后就是工具类

public static void jpg2webp(String oldfile, String newfile){
        try {
            // 获取原始文件的编码
            BufferedImage image = ImageIO.read(new File(oldfile));
            // 创建WebP ImageWriter实例
            ImageWriter writer = ImageIO.getImageWritersByMIMEType("image/webp").next();
            // 配置编码参数
            WebPWriteParam writeParam = new WebPWriteParam(writer.getLocale());
            // 设置压缩模式
            writeParam.setCompressionMode(WebPWriteParam.MODE_DEFAULT);
            // 配置ImageWriter输出
            writer.setOutput(new FileImageOutputStream(new File(newfile)));
            // 进行编码,重新生成新图片
            writer.write(null, new IIOImage(image, null, null), writeParam);
//            System.out.println("jpg文件转成webp格式成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

也可以把webp图片转回去

public static void webp2jpg(String oldfile, String newfile){
        // 创建WebP ImageReader实例
        ImageReader reader = ImageIO.getImageReadersByMIMEType("image/webp").next();
        // 配置解码参数
        WebPReadParam readParam = new WebPReadParam();
        readParam.setBypassFiltering(true);
        // 在ImageReader设置读取的原文件
        try {
            reader.setInput(new FileImageInputStream(new File(oldfile)));
            // 解码图像
            BufferedImage image = reader.read(0, readParam);
            // 设置输入文件的格式和文件名
            ImageIO.write(image, "jpg", new File(newfile)); // 这里也可以使用其他图片格式,但是格式和文件名后缀要保持一致
//            System.out.println("webp文件转成png格式成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

看下效果,内存明显变小,并且清晰度并没有太大的影响
在这里插入图片描述
这是转了过后的
在这里插入图片描述

还可以控制图片大小 我这里有 200x200 和1920x1920的

//200x200缩略图
    public static byte[] createThumbnail200(MultipartFile file) throws IOException {
        // 创建 ByteArrayOutputStream 以便缓存压缩后的图像数据
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        // 使用 Thumbnailator 库创建缩略图,将宽度和高度限制为 200x200
        Thumbnails.of(file.getInputStream())
                .forceSize(200, 200)
                .outputFormat("jpg") // 可以根据需要修改输出格式
                .toOutputStream(outputStream);

        // 将压缩后的图像数据转换为 byte 数组
        byte[] thumbnailBytes = outputStream.toByteArray();

        // 关闭 ByteArrayOutputStream
        outputStream.close();

        return thumbnailBytes;
    }

1920x1920

//1920x1920的缩略图
    public static byte[] createThumbnail1920(MultipartFile file) throws IOException {
        // 创建 ByteArrayOutputStream 以便缓存压缩后的图像数据
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        // 使用 Thumbnailator 库创建缩略图,将宽度和高度限制为 1920 1920
        Thumbnails.of(file.getInputStream())
                .forceSize(1920, 1920)
                .outputFormat("jpg") // 可以根据需要修改输出格式
                .toOutputStream(outputStream);

        // 将压缩后的图像数据转换为 byte 数组
        byte[] thumbnailBytes = outputStream.toByteArray();

        // 关闭 ByteArrayOutputStream
        outputStream.close();

        return thumbnailBytes;
    }

最后就是我截取的是代码块 不是完整的业务代码 需要根据自身需求更改 功能方面实现肯定是够了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值