161 lines
5.5 KiB
Java
161 lines
5.5 KiB
Java
package com.klp.utils;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import javax.imageio.ImageIO;
|
|
import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.Base64;
|
|
|
|
/**
|
|
* 图片处理工具类
|
|
*/
|
|
@Component
|
|
public class ImageUtils {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ImageUtils.class);
|
|
|
|
/**
|
|
* 将图片转换为data URI格式
|
|
* @param imagePath 图片路径
|
|
* @param maxDimension 最大尺寸
|
|
* @param quality 质量 (1-100)
|
|
* @return data URI字符串
|
|
*/
|
|
public static String imageToDataUri(String imagePath, int maxDimension, int quality) {
|
|
try {
|
|
File file = new File(imagePath);
|
|
if (!file.exists()) {
|
|
throw new RuntimeException("图片文件不存在: " + imagePath);
|
|
}
|
|
|
|
BufferedImage originalImage = ImageIO.read(file);
|
|
if (originalImage == null) {
|
|
throw new RuntimeException("无法读取图片文件: " + imagePath);
|
|
}
|
|
|
|
// 调整图片尺寸
|
|
BufferedImage resizedImage = resizeImage(originalImage, maxDimension);
|
|
|
|
// 转换为JPEG格式
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
ImageIO.write(resizedImage, "JPEG", baos);
|
|
|
|
// 转换为base64
|
|
byte[] imageBytes = baos.toByteArray();
|
|
String base64 = Base64.getEncoder().encodeToString(imageBytes);
|
|
|
|
return "data:image/jpeg;base64," + base64;
|
|
|
|
} catch (IOException e) {
|
|
logger.error("处理图片失败: {}", imagePath, e);
|
|
throw new RuntimeException("处理图片失败: " + imagePath, e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 调整图片尺寸
|
|
* @param originalImage 原始图片
|
|
* @param maxDimension 最大尺寸
|
|
* @return 调整后的图片
|
|
*/
|
|
private static BufferedImage resizeImage(BufferedImage originalImage, int maxDimension) {
|
|
int originalWidth = originalImage.getWidth();
|
|
int originalHeight = originalImage.getHeight();
|
|
|
|
// 计算缩放比例
|
|
double scale = Math.min((double) maxDimension / originalWidth, (double) maxDimension / originalHeight);
|
|
|
|
if (scale >= 1.0) {
|
|
// 如果原图小于最大尺寸,直接返回原图
|
|
return originalImage;
|
|
}
|
|
|
|
int newWidth = (int) (originalWidth * scale);
|
|
int newHeight = (int) (originalHeight * scale);
|
|
|
|
// 创建新图片
|
|
BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
|
|
Graphics2D g2d = resizedImage.createGraphics();
|
|
|
|
// 设置渲染质量
|
|
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
|
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
|
|
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
|
// 绘制调整后的图片
|
|
g2d.drawImage(originalImage, 0, 0, newWidth, newHeight, null);
|
|
g2d.dispose();
|
|
|
|
return resizedImage;
|
|
}
|
|
|
|
/**
|
|
* 检查文件是否为有效的图片文件
|
|
* @param filePath 文件路径
|
|
* @return 是否为有效图片
|
|
*/
|
|
public static boolean isValidImageFile(String filePath) {
|
|
try {
|
|
File file = new File(filePath);
|
|
if (!file.exists() || file.length() == 0) {
|
|
return false;
|
|
}
|
|
|
|
// 检查文件扩展名
|
|
String fileName = file.getName().toLowerCase();
|
|
if (!fileName.endsWith(".png") && !fileName.endsWith(".jpg") && !fileName.endsWith(".jpeg")) {
|
|
return false;
|
|
}
|
|
|
|
// 尝试读取图片
|
|
BufferedImage image = ImageIO.read(file);
|
|
return image != null;
|
|
|
|
} catch (Exception e) {
|
|
logger.warn("检查图片文件失败: {}", filePath, e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取目录下的所有有效图片文件
|
|
* @param directoryPath 目录路径
|
|
* @return 图片文件路径列表
|
|
*/
|
|
public static java.util.List<String> getValidImageFiles(String directoryPath) {
|
|
java.util.List<String> validImages = new java.util.ArrayList<>();
|
|
|
|
try {
|
|
File directory = new File(directoryPath);
|
|
if (!directory.exists() || !directory.isDirectory()) {
|
|
return validImages;
|
|
}
|
|
|
|
File[] files = directory.listFiles((dir, name) -> {
|
|
String lowerName = name.toLowerCase();
|
|
return lowerName.endsWith(".png") || lowerName.endsWith(".jpg") || lowerName.endsWith(".jpeg");
|
|
});
|
|
|
|
if (files != null) {
|
|
for (File file : files) {
|
|
if (isValidImageFile(file.getAbsolutePath())) {
|
|
validImages.add(file.getAbsolutePath());
|
|
}
|
|
}
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
logger.error("获取图片文件列表失败: {}", directoryPath, e);
|
|
}
|
|
|
|
return validImages;
|
|
}
|
|
} |