图片压缩工具类
依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.24</version>
</dependency>
代码
package com.zhanghuan.material.utils;
import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
@Slf4j
public class ImageCompressUtils {
public static File compressImage(MultipartFile file, String targetDir, long maxSize) throws IOException {
String extName = FileUtil.extName(file.getOriginalFilename());
// 确保目标路径存在
FileUtil.mkdir(targetDir);
// 临时文件路径
String tempPath = targetDir + File.separator + file.getName();
File tempFile = new File(tempPath);
file.transferTo(tempFile);
// 压缩图片
long size = FileUtil.size(tempFile);
int num = 0;
float rate = 0.9f;
while (size > maxSize && num <20) {
compress(tempPath,extName,rate);
size = FileUtil.size(tempFile);
num++;
rate *= rate;
}
log.info("当前图片{}压缩次数:{}",tempPath,num);
return tempFile;
}
public static File compressImage(File file, long maxSize) throws IOException {
String extName = FileUtil.extName(file.getName());
// 压缩图片
long size = FileUtil.size(file);
int num = 0;
float rate = 0.9f;
while (size > maxSize && num <20) {
compress(file.getAbsolutePath(),extName,rate);
size = FileUtil.size(file);
num++;
rate *= rate;
}
log.info("当前图片{}压缩次数:{}",file,num);
return file;
}
public static boolean isImage(File file) {
String extension = FileUtil.getSuffix(file.getName());
String[] imageExtensions = new String[]{"jpg", "jpeg", "png", "gif", "bmp", "webp"};
for (String imgExtension : imageExtensions) {
if (imgExtension.equalsIgnoreCase(extension)) {
return true;
}
}
return false;
}
public static boolean isImage(String filename) {
String extension = FileUtil.getSuffix(filename);
String[] imageExtensions = new String[]{"jpg", "jpeg", "png", "gif", "bmp", "webp"};
for (String imgExtension : imageExtensions) {
if (imgExtension.equalsIgnoreCase(extension)) {
return true;
}
}
return false;
}
private static void compress(String fileFullPathName,String formatName,float rate) throws IOException {
File file = new File(fileFullPathName);
BufferedImage originalImage = ImageIO.read(file);
// 设置压缩后的最大宽度和高度(单位为像素)
int maxWidth = (int) (1600 * rate);
int maxHeight = (int) (1600 * rate);
int width = originalImage.getWidth();
int height = originalImage.getHeight();
// 计算压缩比例
double scaleX = (double)maxWidth / width;
double scaleY = (double)maxHeight / height;
double scaleFactor = Math.min(scaleX, scaleY);
// 创建新的BufferedImage对象,指定压缩后的尺寸
int newWidth = (int)(width * scaleFactor);
int newHeight = (int)(height * scaleFactor);
BufferedImage compImage = new BufferedImage(newWidth, newHeight, originalImage.getType());
Graphics2D g = compImage.createGraphics();
g.drawImage(originalImage, 0, 0, newWidth, newHeight, null);
g.dispose();
//将压缩后的图片保存到文件
ImageIO.write(compImage, formatName, new File(fileFullPathName));
}
public static void main(String[] args) {
String path = "C:\\Users\\Administrator\\Desktop\\test\\img\\7775EF761C1CD6CE8A0C2CA280E8321C.png";
try {
compressImage(new File(path),500*1024);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}还没有评论,来说两句吧
