From e4b06cc9cc62d8ef127990c4b9129ae63bf190c3 Mon Sep 17 00:00:00 2001 From: zhouwentao <1577701412@qq.com> Date: Wed, 26 Jul 2023 14:08:37 +0800 Subject: [PATCH] updates --- .../ocr/controller/OcrApiController.java | 35 ++++---------- .../ocr/controller/OcrIdentifyController.java | 14 ++++++ .../service/impl/OcrIdentifyServiceImpl.java | 23 ++++----- .../{ArrayCUtils.java => ArrayOUtils.java} | 2 +- .../jeecg/modules/ocr/utils/FileOUtils.java | 47 +++++++++++++++++++ 5 files changed, 81 insertions(+), 40 deletions(-) rename jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/{ArrayCUtils.java => ArrayOUtils.java} (95%) create mode 100644 jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/FileOUtils.java diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/controller/OcrApiController.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/controller/OcrApiController.java index 43cdd8c..21de01a 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/controller/OcrApiController.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/controller/OcrApiController.java @@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.FileUtils; import org.jeecg.common.api.vo.Result; import org.jeecg.common.util.AssertUtils; import org.jeecg.common.util.RestUtil; @@ -15,6 +16,7 @@ import org.jeecg.modules.ocr.service.IOcrApiCallStatisticsService; import org.jeecg.modules.ocr.service.IOcrIdentifyService; import org.jeecg.modules.ocr.service.IOcrRuleCheckService; import org.jeecg.modules.ocr.service.IOcrTaskTypeService; +import org.jeecg.modules.ocr.utils.FileOUtils; import org.jeecg.modules.ocr.vo.OcrRuleCheckVo; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestBody; @@ -71,32 +73,14 @@ public class OcrApiController { //获取识别的图片 List fileList=new ArrayList<>(); - if (sourceImages.indexOf("http://")!=-1||sourceImages.indexOf("https://")!=-1) { + List fileUrlList= FileOUtils.fileLists(sourceImages); + for (String fileUrl : fileUrlList) { //判断附件是否是 图片格式 - AssertUtils.isTrue(sourceImages.lastIndexOf(".png")!=-1 || sourceImages.lastIndexOf(".jpg")!=-1|| - sourceImages.lastIndexOf(".jpeg")!=-1,"当前网络图片格式不支持"); - //网络图片 - fileList.add(sourceImages); - }else{ - //本地附件 - if(false){ - File sourceImagesIile = new File(sourceImages); - AssertUtils.isTrue(sourceImagesIile.exists(),"当前本地目录不存在"); - File[] files = sourceImagesIile.listFiles(); - for (File file : files) { - if (file.getAbsolutePath().lastIndexOf(".png")==-1&&file.getAbsolutePath().lastIndexOf(".jpg")==-1&& - file.getAbsolutePath().lastIndexOf(".jpeg")==-1) { - continue; - } - fileList.add(file.getAbsolutePath()); - } - }else{ - AssertUtils.notTrue(true,"当前仅支持网络图片"); - AssertUtils.isTrue(sourceImages.lastIndexOf(".png")!=-1 || sourceImages.lastIndexOf(".jpg")!=-1|| - sourceImages.lastIndexOf(".jpeg")!=-1,"当前图片格式不支持"); + if (fileUrl.lastIndexOf(".png")!=-1 || fileUrl.lastIndexOf(".jpg")!=-1|| fileUrl.lastIndexOf(".jpeg")!=-1) { + fileList.add(fileUrl); } } - AssertUtils.isTrue(fileList.size()!=0,"未获取到图片真实地址"); + AssertUtils.hasSize(fileList,"图片目录下未获取到可使用的图片格式文件"); OcrIdentify ocrIdentify=new OcrIdentify(); //2.创建识别任务 if(true){ @@ -108,6 +92,7 @@ public class OcrApiController { if (scenes.equals("door")) { ocrIdentify.setTaskType("1683412752926986241");//任务类型 } + ocrIdentify.setPriority(priority.toString()); ocrIdentify.setMetadataConfigId(ruleCheck.getMetadataConfigId());//规则配置的元数据id ocrIdentify.setTaskSource("API");//任务来源 ocrIdentify.setIdentifyUrl(sourceImages);//识别图片地址 @@ -116,9 +101,7 @@ public class OcrApiController { ocrIdentifyService.save(ocrIdentify); } //3.请求python ocr识别,异步执行 - for (String file : fileList) { - ocrIdentifyService.postSemantic(ocrIdentify,fileList); - } + ocrIdentifyService.postSemantic(ocrIdentify,fileList); return Result.OK("请求成功"); } diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/controller/OcrIdentifyController.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/controller/OcrIdentifyController.java index 9701c1e..3868c1f 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/controller/OcrIdentifyController.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/controller/OcrIdentifyController.java @@ -1,5 +1,6 @@ package org.jeecg.modules.ocr.controller; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javax.servlet.http.HttpServletRequest; @@ -18,6 +19,7 @@ import org.jeecg.modules.ocr.entity.OcrMetadataConfig; import org.jeecg.modules.ocr.service.IOcrIdentifyService; import org.jeecg.common.system.base.controller.JeecgController; import org.jeecg.modules.ocr.utils.DownloadTemplateUtil; +import org.jeecg.modules.ocr.utils.FileOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; @@ -75,7 +77,19 @@ public class OcrIdentifyController extends JeecgController add(@RequestBody OcrIdentify ocrIdentify) { + //获取识别的图片 + List fileList=new ArrayList<>(); + List fileUrlList= FileOUtils.fileLists(ocrIdentify.getIdentifyUrl()); + for (String fileUrl : fileUrlList) { + //判断附件是否是 图片格式 + if (fileUrl.lastIndexOf(".png")!=-1 || fileUrl.lastIndexOf(".jpg")!=-1|| fileUrl.lastIndexOf(".jpeg")!=-1) { + fileList.add(fileUrl); + } + } + AssertUtils.hasSize(fileList,"图片目录下未获取到可使用的图片格式文件"); ocrIdentifyService.save(ocrIdentify); + //3.请求python ocr识别,异步执行 + ocrIdentifyService.postSemantic(ocrIdentify,fileList); //请求ocr识别接口 //ocrIdentifyService.postSemantic(ocrIdentify.getId(),Arrays.asList(ocrIdentify.getIdentifyUrl())); return Result.OK("添加成功!"); diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/service/impl/OcrIdentifyServiceImpl.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/service/impl/OcrIdentifyServiceImpl.java index 5d8a0fa..85255a3 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/service/impl/OcrIdentifyServiceImpl.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/service/impl/OcrIdentifyServiceImpl.java @@ -1,35 +1,29 @@ package org.jeecg.modules.ocr.service.impl; -import cn.hutool.core.util.ArrayUtil; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; -import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; -import org.checkerframework.checker.units.qual.C; import org.jeecg.common.api.vo.Result; import org.jeecg.common.constant.OcrConstant; import org.jeecg.common.util.RedisUtil; import org.jeecg.common.util.RestUtil; import org.jeecg.modules.ocr.entity.OcrIdentify; import org.jeecg.modules.ocr.entity.OcrIdentifyDetail; -import org.jeecg.modules.ocr.entity.OcrRuleCheck; import org.jeecg.modules.ocr.mapper.OcrIdentifyMapper; import org.jeecg.modules.ocr.model.CheckSemanticModel; import org.jeecg.modules.ocr.model.OcrResult; import org.jeecg.modules.ocr.model.SourceImage; import org.jeecg.modules.ocr.model.TaskModel; import org.jeecg.modules.ocr.service.*; -import org.jeecg.modules.ocr.utils.ArrayCUtils; +import org.jeecg.modules.ocr.utils.ArrayOUtils; import org.jeecg.modules.ocr.vo.OcrIdentifyVo; import org.jeecg.modules.ocr.vo.OcrRuleCheckVo; -import org.springframework.beans.BeanUtils; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.bind.annotation.RequestBody; import javax.annotation.Resource; import java.util.*; @@ -77,12 +71,12 @@ public class OcrIdentifyServiceImpl extends ServiceImpl fieldNameList = Arrays.asList(fieldName.split(",")); - boolean b = ArrayCUtils.containsStringList(fieldNameList, semanticResult.keySet().stream().collect(Collectors.toList())); + boolean b = ArrayOUtils.containsStringList(fieldNameList, semanticResult.keySet().stream().collect(Collectors.toList())); //查看ocr识别返回的字段名称中是否有当前这个字段名称 if (b) { //TODO 注意,ocr 识别返回的 字段是多个结果(数组),有一个值匹配上即为正确 @@ -229,6 +223,7 @@ public class OcrIdentifyServiceImpl extends ServiceImpl t.equals(task_id)).collect(Collectors.joining(",")); + String collect = Arrays.asList(masterTask.split(",")).stream().filter(t -> !t.equals(task_id)).collect(Collectors.joining(",")); if (StringUtils.isBlank(collect)) { //如果主任务下的子任务已清空,删除key - redisUtil.removeAll(masterTaskId); + redisUtil.del(masterTaskId); //刷新Ocr识别任务状态 updateOcrIdentifyStatus(masterTaskId,"1"); + log.debug("更新任务状态,id:"+masterTaskId); }else{ //主任务还存在,刷新主任务明细 redisUtil.set(masterTaskId,collect); diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/ArrayCUtils.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/ArrayOUtils.java similarity index 95% rename from jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/ArrayCUtils.java rename to jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/ArrayOUtils.java index de3ca4e..091f567 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/ArrayCUtils.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/ArrayOUtils.java @@ -10,7 +10,7 @@ import java.util.List; * @Date 2023/7/24 14:46 */ @Slf4j -public class ArrayCUtils { +public class ArrayOUtils { public static boolean containsStringList(List objects,List objects2){ boolean flag=false; for (String object : objects) { 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 new file mode 100644 index 0000000..ce61226 --- /dev/null +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/FileOUtils.java @@ -0,0 +1,47 @@ +package org.jeecg.modules.ocr.utils; + +import org.apache.commons.io.FileUtils; +import org.jeecg.common.exception.JeecgBootException; +import org.jeecg.common.util.AssertUtils; + +import java.io.File; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.List; + +/** + * @Description + * @Author ZhouWenTao + * @Date 2023/7/26 13:52 + */ +public class FileOUtils { + /** + * 判断附件目录是否存在,并返回目录下的附件目录 + * @param sourceImages + * @return + */ + public static List fileLists(String sourceImages) { + List files=new ArrayList<>(); + if (sourceImages.indexOf("http://")!=-1||sourceImages.indexOf("https://")!=-1) { + //网络目录 + try { + URL url = new URL(sourceImages); + // 返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。 + URLConnection uc = url.openConnection(); + // 打开的连接读取的输入流。 + InputStream in = uc.getInputStream(); + //附件存在 + files.add(sourceImages); + } catch (Exception e) { + //附件不存在 + return null; + } + }else{ + //本地文件 + throw new JeecgBootException("当前不支持本地目录"); + } + return files; + } +}