From 8edf271fa33a935321e1d98bcddf454d5a2c6a44 Mon Sep 17 00:00:00 2001 From: Vincent <19330835921@163.com> Date: Tue, 24 Jun 2025 15:47:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=9B=BE=E7=89=87=E5=8C=85=E5=90=AB?= =?UTF-8?q?=E4=B8=AD=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../thread/tasks/PictureImgToLocalTask.java | 8 +-- .../module/custom/ocr/utils/ImageUtils.java | 49 +++++++++++++++---- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/jeecg-module-custom/src/main/java/org/jeecg/module/custom/ocr/api/thread/tasks/PictureImgToLocalTask.java b/jeecg-module-custom/src/main/java/org/jeecg/module/custom/ocr/api/thread/tasks/PictureImgToLocalTask.java index 62e998c..d80b394 100644 --- a/jeecg-module-custom/src/main/java/org/jeecg/module/custom/ocr/api/thread/tasks/PictureImgToLocalTask.java +++ b/jeecg-module-custom/src/main/java/org/jeecg/module/custom/ocr/api/thread/tasks/PictureImgToLocalTask.java @@ -33,7 +33,9 @@ import java.util.Map; public class PictureImgToLocalTask implements Runnable { 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) { this.pictureImgToLocal = pictureImgToLocal; @@ -75,10 +77,8 @@ public class PictureImgToLocalTask implements Runnable { } if (ObjectUtil.isNotEmpty(task)) { - task.setLocalImageUrl(livePhoto.getFileUrl()); try { -// picture.setLocalImageUrl(ImageUtils.generateThumbnail("D:\\code\\ocr-activity-backend\\1.jpeg",182)); - task.setLocalImageUrl(ImageUtils.generateThumbnail(livePhoto.getFileName(), 182)); + task.setLocalImageUrl(localPath + "/" + ImageUtils.generateThumbnail(livePhoto.getFileName(), 182)); // picture.setServerThumbnailUrl(ocrPictureService.getServerUrl()+ImageUtils.getFileName(picture.getLocalThumbnailUrl())); } catch (IOException e) { throw new RuntimeException(e); diff --git a/jeecg-module-custom/src/main/java/org/jeecg/module/custom/ocr/utils/ImageUtils.java b/jeecg-module-custom/src/main/java/org/jeecg/module/custom/ocr/utils/ImageUtils.java index 43cb082..c17d1dc 100644 --- a/jeecg-module-custom/src/main/java/org/jeecg/module/custom/ocr/utils/ImageUtils.java +++ b/jeecg-module-custom/src/main/java/org/jeecg/module/custom/ocr/utils/ImageUtils.java @@ -5,6 +5,9 @@ import net.coobird.thumbnailator.Thumbnails; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; +import java.util.UUID; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * @Title: @@ -15,30 +18,58 @@ import java.io.IOException; */ public class ImageUtils { public static String generateThumbnail(String inputImagePath, Integer newWidth) throws IOException { - if(newWidth==null){ - newWidth=182; + if (newWidth == null) { + newWidth = 182; } + // 获取目录路径 String directoryPath = inputImagePath.substring(0, inputImagePath.lastIndexOf("/") + 1); 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(); int width = bufferedImage.getWidth(); int height = bufferedImage.getHeight(); 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; } + 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) { - if (fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) { - return fileName.substring(fileName.lastIndexOf(".") + 1); - } else { - return "jpg"; // 没有后缀名或者文件名以点号开头 + // 如果找到中文,替换为随机 UUID(取前 8 位以保持简洁) + if (matcher.find()) { + String randomId = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 8); + return matcher.replaceAll(randomId); } + + // 如果没有中文,直接返回原文件名 + return fileName; } + public static void main(String[] args) throws IOException { String url="C:\\Users\\郭向斌\\Downloads\\1-星浩.png"; String s = generateThumbnail(url,180);