前端加载大图片响应慢 提升效率可以把图片转为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;
}
最后就是我截取的是代码块 不是完整的业务代码 需要根据自身需求更改 功能方面实现肯定是够了