diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/controller/ApiController.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/controller/ApiController.java index 6cb4241..6a62a1a 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/controller/ApiController.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/controller/ApiController.java @@ -8,10 +8,12 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.jeecg.common.api.vo.Result; +import org.jeecg.common.constant.CommonConstant; +import org.jeecg.common.constant.SymbolConstant; +import org.jeecg.common.exception.JeecgBootException; import org.jeecg.common.system.vo.DictModel; -import org.jeecg.common.util.AssertUtils; -import org.jeecg.common.util.RedisUtil; -import org.jeecg.common.util.RestUtil; +import org.jeecg.common.util.*; +import org.jeecg.common.util.filter.FileTypeFilter; import org.jeecg.modules.ocr.entity.OcrIdentify; import org.jeecg.modules.ocr.entity.OcrIdentifyDetail; import org.jeecg.modules.ocr.entity.OcrRuleCheck; @@ -27,10 +29,17 @@ import org.jeecg.modules.system.service.ISysDictService; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.FileCopyUtils; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.MultipartHttpServletRequest; import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.awt.*; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -210,14 +219,34 @@ public class ApiController { public Result restartTask(@RequestBody JSONObject jsonObject) throws InterruptedException { System.out.println(jsonObject.toJSONString()); //获取已执行完的任务 - List list = ocrIdentifyService.list(new LambdaQueryWrapper()); - int i =0; + //List list = ocrIdentifyService.list(new LambdaQueryWrapper()); + List list = ocrIdentifyDetailService.list(); + for (OcrIdentifyDetail ocrIdentifyDetail : list) { + String imgPath = ocrIdentifyDetail.getImageUrl(); + JSONObject semanticResult = JSONObject.parseObject(ocrIdentifyDetail.getSemanticResult()); + File file = new File(imgPath); + //当原图片存在时. + if (file.exists()) { + int i = imgPath.lastIndexOf("/"); + String fileUrl = "/data/ocrImage/drawDashed" + imgPath.substring(0, i); + //判断新目录是否存在,不存在则新建 + FileOUtils.folderCreate(fileUrl); + String outputImagePath = fileUrl + imgPath.substring(i + 1, imgPath.length()); + List> resultToPoints = ImageUtils.semanticResultToPoints(semanticResult); + if (resultToPoints.size()>0) { + ImageUtils.drawDashedRectangleOnImages(file.getAbsolutePath(),resultToPoints,outputImagePath); + ocrIdentifyDetail.setDrawDashedImageUrl(outputImagePath); + } + } + } + ocrIdentifyDetailService.updateBatchById(list); + /*int i =0; for (OcrIdentify ocrIdentify : list) { if (ocrIdentify.getStatus().equals("1")) { ocrIdentifyService.updateTaskResultInfo(ocrIdentify.getId()); }else{} i++; - } + }*/ return Result.OK(list.size()+"个任务."); } @@ -232,8 +261,4 @@ public class ApiController { return Result.OK(""); } - public static void main(String[] args) { - } -} - -//curl --request POST --url http://127.0.0.1:8000/semantic --header 'content-type: application/json' --data '{ "task_id": "1682299148529958914","img_path": "http://47.103.213.109:8072/files/nfs/ocr/shared_directory/09360a383f464ea0ad056145ec5b62e9/4abf97c877bbe4e4d091aef8411edd81.jpeg"}' \ No newline at end of file +} \ No newline at end of file diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/controller/OcrIdentifyDetailController.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/controller/OcrIdentifyDetailController.java index 604e317..1a25e81 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/controller/OcrIdentifyDetailController.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/controller/OcrIdentifyDetailController.java @@ -97,6 +97,14 @@ public class OcrIdentifyDetailController extends JeecgController> resultToPoints = ImageUtils.semanticResultToPoints(semanticResult); + if (resultToPoints.size()>0) { + ImageUtils.drawDashedRectangleOnImages(file.getAbsolutePath(),resultToPoints,outputImagePath); + ocrIdentifyDetail.setDrawDashedImageUrl(outputImagePath); + } } - ocrIdentifyDetail.setThumbnailImageUrl(outputImagePath); } //ocrIdentifyDetail.setStatus(taskStatus); ocrIdentifyDetail.setStatus(""); diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/FileOUtils.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/FileOUtils.java index 473cc6b..30d68fa 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/FileOUtils.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/FileOUtils.java @@ -73,6 +73,25 @@ public class FileOUtils { return files; } + /** + * 判断目录是否存在,不存在就创建 + * @param folder + */ + public static void folderCreate(String folder){ + if (StringUtils.isNotBlank(folder)) { + File file =new File(folder); + if (file.exists()) { + if (file.isDirectory()) {/*System.out.println("dir exists");*/} + else { + System.out.println("the same name file exists, can not create dir"); + } + } else { + //System.out.println("目录不存在,创建"); + file.mkdirs(); + } + } + } + public static void main(String[] args) { List stringList = fileLists(null,"E:\\workspace_2\\ocr\\images1"); System.out.println(stringList); diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/ImageUtils.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/ImageUtils.java index a500c6c..a84a08c 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/ImageUtils.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/ImageUtils.java @@ -1,12 +1,25 @@ package org.jeecg.modules.ocr.utils; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import javafx.util.Pair; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + import net.coobird.thumbnailator.Thumbnails; import org.apache.commons.lang.StringUtils; -import java.awt.Graphics2D; -import java.awt.Image; -import java.awt.image.AffineTransformOp; + +import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; +import java.net.URL; import javax.imageio.ImageIO; /** @@ -19,19 +32,35 @@ public class ImageUtils { 当涉及到图像压缩时,Java中有几个可以使用的库,比如ImageIO、Thumbnails和Java Advanced Imaging API(JAI)。以下是一个使用ImageIO库来进行图片压缩的示例: */ public static void main(String[] args) throws FileNotFoundException { - String sourceImagePath="C:\\Users\\Denim\\Desktop\\aabebdf71887c6c2707c65211cac2d3a.jpeg"; - String outputImagePath="C:\\Users\\Denim\\Desktop\\ds-aabebdf71887c6c2707c65211cac2d3a.jpeg"; - compressImage(sourceImagePath,outputImagePath,0.25f,0.25f); + String sourceImagePath = "C:\\Users\\Denim\\Desktop\\aabebdf71887c6c2707c65211cac2d3a.jpeg"; + String outputImagePath = "C:\\Users\\Denim\\Desktop\\ds-aabebdf71887c6c2707c65211cac2d3a.jpeg"; + //compressImage(sourceImagePath,outputImagePath,0.25f,0.25f); + List points = new ArrayList<>(); + points.add(new Point(9, 2924)); + points.add(new Point(107, 2924)); + points.add(new Point(107, 2959)); + points.add(new Point(9, 2959)); + List points2 = new ArrayList<>(); + points2.add(new Point(1282, 1576)); + points2.add(new Point(2764, 1594)); + points2.add(new Point(2763, 1671)); + points2.add(new Point(1281, 1653)); + List> pointList = new ArrayList<>(); + pointList.add(points); + pointList.add(points2); + drawDashedRectangleOnImages("http://47.103.213.109:8072/files/nfs/ocr/shared_directory/fdd50089f33e4b4391b8c88b6299735b/a86aff69fc3ff5397d038b4dbcde6165.jpeg", pointList, "C:\\Users\\Denim\\Desktop\\output_image.png"); + //drawDashedRectangleOnImage("http://47.103.213.109:8072/files/nfs/ocr/shared_directory/fdd50089f33e4b4391b8c88b6299735b/a86aff69fc3ff5397d038b4dbcde6165.jpeg", points, "C:\\Users\\Denim\\Desktop\\output_image.png"); } /** * 等比例压缩 + * * @param sourceImagePath * @param outputImagePath * @param scale * @param quality */ - public static void compressImage(String sourceImagePath, String outputImagePath,Float scale,Float quality){ + public static void compressImage(String sourceImagePath, String outputImagePath, Float scale, Float quality) { try { Thumbnails.of(new File(sourceImagePath)) .scale(scale) //图片大小(长宽)压缩比例 从0-1,1表示原图 @@ -43,46 +72,109 @@ public class ImageUtils { } } - // 获取图片的旋转角度信息 - private static int getOrientation(File imageFile) throws IOException { - // TODO: 从图片的元数据中获取旋转角度信息,具体实现略 - // 这里假设返回的旋转角度为0 - return 90; - } + /** + * 图片画线 + * + * @param imagePath + * @param points + * @param outputPath + */ + public static void drawDashedRectangleOnImage(String imagePath, List points, String outputPath) { + try { + // Load the image + BufferedImage image = ImageIO.read(new URL(imagePath)); + Graphics2D g2d = image.createGraphics(); + // 设置线的颜色 + g2d.setColor(new Color(255, 30, 30)); + // 定义虚线的样式 + float[] dash = {5.0f}; + BasicStroke dashed = + new BasicStroke(3.0f, + BasicStroke.CAP_BUTT, + BasicStroke.JOIN_MITER, + 5.0f, dash, 0.0f); + g2d.setStroke(dashed); + // 创建一个多边形 + Polygon polygon = new Polygon(); + for (Point point : points) { + polygon.addPoint(point.x, point.y); + } + // 画出多边形 + g2d.drawPolygon(polygon); - // 根据旋转角度调整图片 - private static BufferedImage adjustOrientation(BufferedImage image, int orientation) { - // TODO: 根据旋转角度调整图片,具体实现略 - // 这里假设不进行任何旋转调整 - return image; + g2d.dispose(); + // Save the image + ImageIO.write(image, "png", new File(outputPath)); + } catch (IOException e) { + e.printStackTrace(); + } } - // 压缩图片 - private static BufferedImage compressImage(BufferedImage image, int targetWidth, int targetHeight) { - Image scaledImage = image.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH); - BufferedImage compressedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB); - Graphics2D g2d = compressedImage.createGraphics(); - g2d.drawImage(scaledImage, 0, 0, null); - g2d.dispose(); - - return compressedImage; - } + public static void drawDashedRectangleOnImages(String imagePath, List> pointList, String outputPath) { + try { + // Load the image + BufferedImage image = ImageIO.read(new URL(imagePath)); + for (List points : pointList) { + Graphics2D g2d = image.createGraphics(); + // 设置线的颜色 + g2d.setColor(new Color(255, 30, 30)); + // 定义虚线的样式 + float[] dash = {5.0f}; + BasicStroke dashed = + new BasicStroke(3.0f, + BasicStroke.CAP_BUTT, + BasicStroke.JOIN_MITER, + 5.0f, dash, 0.0f); + g2d.setStroke(dashed); + // 创建一个多边形 + Polygon polygon = new Polygon(); + for (Point point : points) { + polygon.addPoint(point.x, point.y); + } + // 画出多边形 + g2d.drawPolygon(polygon); + g2d.dispose(); + } + // Save the image + ImageIO.write(image, "png", new File(outputPath)); + } catch (IOException e) { + e.printStackTrace(); + } + } - public static void folderCreate(String folder){ - if (StringUtils.isNotBlank(folder)) { - File file =new File(folder); - if (file.exists()) { - if (file.isDirectory()) {/*System.out.println("dir exists");*/} - else { - System.out.println("the same name file exists, can not create dir"); + /** + * 语义化结果中提取 图片点。 + * + * @param semanticResult + * @return + */ + public static List> semanticResultToPoints(JSONObject semanticResult) { + List> lists = new ArrayList<>(); + for (String s : semanticResult.keySet()) { + if ("ocr_res".equals(s) || "其他".equals(s)) { + continue; + } + JSONArray jsonArray = semanticResult.getJSONArray(s); + if (jsonArray.size() > 0) { + List jos = jsonArray.toJavaList(JSONObject.class); + for (JSONObject jo : jos) { + List points = new ArrayList<>(); + if (jo.containsKey("area")) { + List areas = jo.getJSONArray("area").toJavaList(JSONObject.class); + for (JSONObject area : areas) { + Integer[] doubles = area.values().toArray(new Integer[0]); + points.add(new Point(doubles[0], doubles[1])); + } + } + if (points.size() > 0) { + lists.add(points); + } } - } else { - //System.out.println("目录不存在,创建"); - file.mkdirs(); } } + return lists; } }