fix: 图片包含中文

dev
Vincent 1 week ago
parent 0c388e0183
commit 8edf271fa3

@ -33,7 +33,9 @@ import java.util.Map;
public class PictureImgToLocalTask implements Runnable { public class PictureImgToLocalTask implements Runnable {
protected Logger logger = LoggerFactory.getLogger(getClass()); protected Logger logger = LoggerFactory.getLogger(getClass());
private FormRecord pictureImgToLocal; private final String localPath = System.getProperty("user.dir");
private final FormRecord pictureImgToLocal;
public PictureImgToLocalTask(FormRecord pictureImgToLocal) { public PictureImgToLocalTask(FormRecord pictureImgToLocal) {
this.pictureImgToLocal = pictureImgToLocal; this.pictureImgToLocal = pictureImgToLocal;
@ -75,10 +77,8 @@ public class PictureImgToLocalTask implements Runnable {
} }
if (ObjectUtil.isNotEmpty(task)) { if (ObjectUtil.isNotEmpty(task)) {
task.setLocalImageUrl(livePhoto.getFileUrl());
try { try {
// picture.setLocalImageUrl(ImageUtils.generateThumbnail("D:\\code\\ocr-activity-backend\\1.jpeg",182)); task.setLocalImageUrl(localPath + "/" + ImageUtils.generateThumbnail(livePhoto.getFileName(), 182));
task.setLocalImageUrl(ImageUtils.generateThumbnail(livePhoto.getFileName(), 182));
// picture.setServerThumbnailUrl(ocrPictureService.getServerUrl()+ImageUtils.getFileName(picture.getLocalThumbnailUrl())); // picture.setServerThumbnailUrl(ocrPictureService.getServerUrl()+ImageUtils.getFileName(picture.getLocalThumbnailUrl()));
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);

@ -5,6 +5,9 @@ import net.coobird.thumbnailator.Thumbnails;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* @Title: * @Title:
@ -15,30 +18,58 @@ import java.io.IOException;
*/ */
public class ImageUtils { public class ImageUtils {
public static String generateThumbnail(String inputImagePath, Integer newWidth) throws IOException { public static String generateThumbnail(String inputImagePath, Integer newWidth) throws IOException {
if(newWidth==null){ if (newWidth == null) {
newWidth=182; newWidth = 182;
} }
// 获取目录路径
String directoryPath = inputImagePath.substring(0, inputImagePath.lastIndexOf("/") + 1); String directoryPath = inputImagePath.substring(0, inputImagePath.lastIndexOf("/") + 1);
File inputFile = new File(inputImagePath); File inputFile = new File(inputImagePath);
String outputImagePath = directoryPath+ "thumbnail_" + inputFile.getName();
// 获取文件扩展名
String fileExtension = getFileExtension(inputFile.getName());
// 生成随机 ID 替换中文
String fileName = inputFile.getName();
String newFileName = replaceChineseWithRandomId(fileName);
// 构造输出路径
String outputImagePath = directoryPath + "thumbnail_" + newFileName;
// 生成缩略图
BufferedImage bufferedImage = Thumbnails.of(inputFile).scale(1).asBufferedImage(); BufferedImage bufferedImage = Thumbnails.of(inputFile).scale(1).asBufferedImage();
int width = bufferedImage.getWidth(); int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight(); int height = bufferedImage.getHeight();
int newHeight = (int) Math.round((double) height * newWidth / width); int newHeight = (int) Math.round((double) height * newWidth / width);
Thumbnails.of(inputFile).size(newWidth, newHeight).outputFormat( getFileExtension(inputFile.getName())).toFile(new File(outputImagePath)); Thumbnails.of(inputFile)
.size(newWidth, newHeight)
.outputFormat(fileExtension)
.toFile(new File(outputImagePath));
return outputImagePath; return outputImagePath;
} }
private static String getFileExtension(String fileName) {
int lastDotIndex = fileName.lastIndexOf(".");
return lastDotIndex > 0 ? fileName.substring(lastDotIndex + 1) : "";
}
private static String replaceChineseWithRandomId(String fileName) {
// 正则表达式匹配中文字符
Pattern chinesePattern = Pattern.compile("[\\u4e00-\\u9fa5]+");
Matcher matcher = chinesePattern.matcher(fileName);
public static String getFileExtension(String fileName) { // 如果找到中文,替换为随机 UUID取前 8 位以保持简洁)
if (fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) { if (matcher.find()) {
return fileName.substring(fileName.lastIndexOf(".") + 1); String randomId = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 8);
} else { return matcher.replaceAll(randomId);
return "jpg"; // 没有后缀名或者文件名以点号开头
} }
// 如果没有中文,直接返回原文件名
return fileName;
} }
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
String url="C:\\Users\\郭向斌\\Downloads\\1-星浩.png"; String url="C:\\Users\\郭向斌\\Downloads\\1-星浩.png";
String s = generateThumbnail(url,180); String s = generateThumbnail(url,180);

Loading…
Cancel
Save