master
周文涛 2 years ago
parent d76fbcc9a8
commit ffd6e0aadd

@ -3,8 +3,11 @@ package org.jeecg.modules.ocr.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.OcrConstant;
import org.jeecg.common.constant.enums.OcrStatusEnum;
@ -19,8 +22,14 @@ import org.jeecg.modules.ocr.entity.OcrIdentify;
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.service.IOcrMetadataConfigService;
import org.jeecg.modules.ocr.service.IOcrRuleCheckService;
import org.jeecg.modules.ocr.service.IOcrTaskTypeService;
import org.jeecg.modules.ocr.utils.DownloadTemplateUtil;
import org.jeecg.modules.ocr.utils.FileOUtils;
import org.jeecg.modules.ocr.vo.OcrIdentifyVo;
import org.jeecg.modules.ocr.vo.OcrMetadataConfigVo;
import org.jeecg.modules.ocr.vo.OcrRuleCheckVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
@ -29,177 +38,197 @@ import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
/**
/**
* @Description: ocr
* @Author: jeecg-boot
* @Date: 2023-07-14
* @Date: 2023-07-14
* @Version: V1.0
*/
@Api(tags="ocr识别")
@Api(tags = "ocr识别")
@RestController
@RequestMapping("/ocrIdentify")
@Slf4j
public class OcrIdentifyController extends JeecgController<OcrIdentify, IOcrIdentifyService> {
@Autowired
private IOcrIdentifyService ocrIdentifyService;
@Autowired
private DownloadTemplateUtil downloadTemplateUtil;
/**
*
*
* @param ocrIdentify
* @param pageNo
* @param pageSize
* @param req
* @return
*/
//@AutoLog(value = "ocr识别-分页列表查询")
@ApiOperation(value="ocr识别-分页列表查询", notes="ocr识别-分页列表查询")
@GetMapping(value = "/list")
public Result<IPage<OcrIdentify>> queryPageList(OcrIdentify ocrIdentify,
@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
HttpServletRequest req) {
QueryWrapper<OcrIdentify> queryWrapper = QueryGenerator.initQueryWrapper(ocrIdentify, req.getParameterMap());
Page<OcrIdentify> page = new Page<OcrIdentify>(pageNo, pageSize);
IPage<OcrIdentify> pageList = ocrIdentifyService.page(page, queryWrapper);
return Result.OK(pageList);
}
/**
*
*
* @param ocrIdentify
* @return
*/
@AutoLog(value = "ocr识别-添加")
@ApiOperation(value="ocr识别-添加", notes="ocr识别-添加")
@Autowired
private IOcrIdentifyService ocrIdentifyService;
@Autowired
private DownloadTemplateUtil downloadTemplateUtil;
@Autowired
private IOcrTaskTypeService ocrTaskTypeService;
@Autowired
private IOcrMetadataConfigService ocrMetadataConfigService;
@Autowired
private IOcrRuleCheckService ocrRuleCheckService;
/**
*
*
* @param ocrIdentify
* @param pageNo
* @param pageSize
* @param req
* @return
*/
//@AutoLog(value = "ocr识别-分页列表查询")
@ApiOperation(value = "ocr识别-分页列表查询", notes = "ocr识别-分页列表查询")
@GetMapping(value = "/list")
public Result<IPage<OcrIdentify>> queryPageList(OcrIdentify ocrIdentify,
@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
HttpServletRequest req) {
QueryWrapper<OcrIdentify> queryWrapper = QueryGenerator.initQueryWrapper(ocrIdentify, req.getParameterMap());
Page<OcrIdentify> page = new Page<OcrIdentify>(pageNo, pageSize);
Map<String, OcrRuleCheckVo> ocrRuleCheckMap = ocrRuleCheckService.listToMap(null);
Map<String, OcrMetadataConfigVo> metadataConfigMap = ocrMetadataConfigService.listToMap(null);
IPage<OcrIdentify> pageList = ocrIdentifyService.page(page, queryWrapper);
if (pageList != null && pageList.getRecords() != null) {
for (OcrIdentify record : pageList.getRecords()) {
OcrRuleCheckVo ocrRuleCheckVo = ocrRuleCheckMap.get(record.getRuleCheck());
if (ocrRuleCheckVo!=null && StringUtils.isNotBlank(ocrRuleCheckVo.getMetadataConfigId())) {
record.setMetadataConfigId(ocrRuleCheckVo.getMetadataConfigId());
OcrMetadataConfigVo ocrMetadataConfigVo = metadataConfigMap.get(ocrRuleCheckVo.getMetadataConfigId());
record.setTaskType_dictText(ocrMetadataConfigVo!=null?ocrMetadataConfigVo.getTaskType_dictText():null);
}
}
}
return Result.OK(pageList);
}
/**
*
*
* @param ocrIdentify
* @return
*/
@AutoLog(value = "ocr识别-添加")
@ApiOperation(value = "ocr识别-添加", notes = "ocr识别-添加")
// @RequiresPermissions("org.jeecg.modules.ocr:ocr_identify:add")
@PostMapping(value = "/add")
public Result<String> add(@RequestBody OcrIdentify ocrIdentify) {
AssertUtils.notEmpty(ocrIdentify.getTaskName(),"[任务名称]-不可为空");
AssertUtils.notEmpty(ocrIdentify.getIdentifyUrl(),"[识别路径]不可为空");
AssertUtils.notEmpty(ocrIdentify.getRuleCheck(),"请选择[规则检查配置]");
AssertUtils.notEmpty(ocrIdentify.getPriority(),"请选择[优先级]");
//获取识别的图片
List<String> fileList=new ArrayList<>();
List<String> fileUrlList= FileOUtils.fileLists(ocrIdentify.getIdentifyUrl());
AssertUtils.notNull(fileUrlList,"图片地址不存在");
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("添加成功!");
}
/**
*
*
* @param ocrIdentify
* @return
*/
@AutoLog(value = "ocr识别-编辑")
@ApiOperation(value="ocr识别-编辑", notes="ocr识别-编辑")
@PostMapping(value = "/add")
public Result<String> add(@RequestBody OcrIdentify ocrIdentify) {
AssertUtils.notEmpty(ocrIdentify.getTaskName(), "[任务名称]-不可为空");
AssertUtils.notEmpty(ocrIdentify.getIdentifyUrl(), "[识别路径]不可为空");
AssertUtils.notEmpty(ocrIdentify.getRuleCheck(), "请选择[规则检查配置]");
AssertUtils.notEmpty(ocrIdentify.getPriority(), "请选择[优先级]");
//获取识别的图片
List<String> fileList = new ArrayList<>();
List<String> fileUrlList = FileOUtils.fileLists(ocrIdentify.getIdentifyUrl());
AssertUtils.notNull(fileUrlList, "图片地址不存在");
for (String fileUrl : fileUrlList) {
//判断附件是否是 图片格式
if (fileUrl.lastIndexOf(".png") != -1 || fileUrl.lastIndexOf(".jpg") != -1 || fileUrl.lastIndexOf(".jpeg") != -1) {
fileList.add(fileUrl);
}
}
AssertUtils.hasSize(fileList, "图片目录下未获取到可使用的图片格式文件");
ocrIdentify.setTaskSource("平台");
ocrIdentifyService.save(ocrIdentify);
//3.请求python ocr识别异步执行
ocrIdentifyService.postSemantic(ocrIdentify, fileList);
//请求ocr识别接口
//ocrIdentifyService.postSemantic(ocrIdentify.getId(),Arrays.asList(ocrIdentify.getIdentifyUrl()));
return Result.OK("添加成功!");
}
/**
*
*
* @param ocrIdentify
* @return
*/
@AutoLog(value = "ocr识别-编辑")
@ApiOperation(value = "ocr识别-编辑", notes = "ocr识别-编辑")
// @RequiresPermissions("org.jeecg.modules.ocr:ocr_identify:edit")
@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
public Result<String> edit(@RequestBody OcrIdentify ocrIdentify) {
AssertUtils.notEmpty(ocrIdentify.getId(),"[id]不可为空");
OcrIdentify identify = ocrIdentifyService.getById(ocrIdentify.getId());
if ("0".equals(identify.getStatus()) || "1".equals(identify.getStatus())) {
throw new JeecgBootException("当前状态不可编辑");
}
ocrIdentifyService.updateById(ocrIdentify);
return Result.OK("编辑成功!");
}
/**
* id
*
* @param id
* @return
*/
@AutoLog(value = "ocr识别-通过id删除")
@ApiOperation(value="ocr识别-通过id删除", notes="ocr识别-通过id删除")
@RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
public Result<String> edit(@RequestBody OcrIdentify ocrIdentify) {
AssertUtils.notEmpty(ocrIdentify.getId(), "[id]不可为空");
OcrIdentify identify = ocrIdentifyService.getById(ocrIdentify.getId());
if ("0".equals(identify.getStatus()) || "1".equals(identify.getStatus())) {
throw new JeecgBootException("当前状态不可编辑");
}
ocrIdentifyService.updateById(ocrIdentify);
return Result.OK("编辑成功!");
}
/**
* id
*
* @param id
* @return
*/
@AutoLog(value = "ocr识别-通过id删除")
@ApiOperation(value = "ocr识别-通过id删除", notes = "ocr识别-通过id删除")
// @RequiresPermissions("org.jeecg.modules.ocr:ocr_identify:delete")
@DeleteMapping(value = "/delete")
public Result<String> delete(@RequestParam(name="id",required=true) String id) {
OcrIdentify ocrIdentify = ocrIdentifyService.getById(id);
AssertUtils.isTrue(OcrStatusEnum.OVER.getStatus().equals(ocrIdentify.getStatus()),"当前状态不可删除");
ocrIdentifyService.removeById(id);
return Result.OK("删除成功!");
}
/**
*
*
* @param ids
* @return
*/
@AutoLog(value = "ocr识别-批量删除")
@ApiOperation(value="ocr识别-批量删除", notes="ocr识别-批量删除")
@DeleteMapping(value = "/delete")
public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
OcrIdentify ocrIdentify = ocrIdentifyService.getById(id);
AssertUtils.isTrue(OcrStatusEnum.OVER.getStatus().equals(ocrIdentify.getStatus()), "当前状态不可删除");
ocrIdentifyService.removeById(id);
return Result.OK("删除成功!");
}
/**
*
*
* @param ids
* @return
*/
@AutoLog(value = "ocr识别-批量删除")
@ApiOperation(value = "ocr识别-批量删除", notes = "ocr识别-批量删除")
// @RequiresPermissions("org.jeecg.modules.ocr:ocr_identify:deleteBatch")
@DeleteMapping(value = "/deleteBatch")
public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
List<OcrIdentify> ocrIdentifies = ocrIdentifyService.listByIds(Arrays.asList(ids.split(",")));
for (OcrIdentify ocrIdentify : ocrIdentifies) {
AssertUtils.isTrue(OcrStatusEnum.OVER.getStatus().equals(ocrIdentify.getStatus()),"当前ocr识别状态不可删除");
}
this.ocrIdentifyService.removeByIds(Arrays.asList(ids.split(",")));
return Result.OK("批量删除成功!");
}
/**
* id
* @param id
* @return
*/
//@AutoLog(value = "ocr识别-通过id查询")
@ApiOperation(value="ocr识别-通过id查询", notes="ocr识别-通过id查询")
@GetMapping(value = "/queryById")
public Result<OcrIdentify> queryById(@RequestParam(name="id",required=true) String id) {
OcrIdentify ocrIdentify = ocrIdentifyService.getById(id);
if(ocrIdentify==null) {
return Result.error("未找到对应数据");
}
return Result.OK(ocrIdentify);
}
@DeleteMapping(value = "/deleteBatch")
public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
List<OcrIdentify> ocrIdentifies = ocrIdentifyService.listByIds(Arrays.asList(ids.split(",")));
for (OcrIdentify ocrIdentify : ocrIdentifies) {
AssertUtils.isTrue(OcrStatusEnum.OVER.getStatus().equals(ocrIdentify.getStatus()), "当前ocr识别状态不可删除");
}
this.ocrIdentifyService.removeByIds(Arrays.asList(ids.split(",")));
return Result.OK("批量删除成功!");
}
/**
* excel
*
* @param request
* @param ocrIdentify
*/
* id
*
* @param id
* @return
*/
//@AutoLog(value = "ocr识别-通过id查询")
@ApiOperation(value = "ocr识别-通过id查询", notes = "ocr识别-通过id查询")
@GetMapping(value = "/queryById")
public Result<OcrIdentify> queryById(@RequestParam(name = "id", required = true) String id) {
OcrIdentify ocrIdentify = ocrIdentifyService.getById(id);
if (ocrIdentify == null) {
return Result.error("未找到对应数据");
}
return Result.OK(ocrIdentify);
}
/**
* excel
*
* @param request
* @param ocrIdentify
*/
// @RequiresPermissions("org.jeecg.modules.ocr:ocr_identify:exportXls")
@RequestMapping(value = "/exportXls")
public ModelAndView exportXls(HttpServletRequest request, OcrIdentify ocrIdentify) {
return super.exportXls(request, ocrIdentify, OcrIdentify.class, "ocr识别");
}
/**
*
*/
@RequestMapping(value = "/downloadTemplate")
public ModelAndView downloadTemplate() {
return downloadTemplateUtil.downloadTemplate(OcrIdentify.class,"ocr识别");
}
/**
*
*/
@RequestMapping(value = "/downloadTemplate")
public ModelAndView downloadTemplate() {
return downloadTemplateUtil.downloadTemplate(OcrIdentify.class, "ocr识别");
}
/**
* excel
*
* @param request
* @param response
* @return
*/
* excel
*
* @param request
* @param response
* @return
*/
// @RequiresPermissions("org.jeecg.modules.ocr:ocr_identify:importExcel")
@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {

@ -3,6 +3,7 @@ package org.jeecg.modules.ocr.entity;
import java.io.Serializable;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@ -82,7 +83,7 @@ public class OcrIdentify implements Serializable {
private String metadataConfigId;
/**任务类型*/
@Excel(name = "任务类型", width = 15, dictTable = "ocr_task_type", dicText = "type_name", dicCode = "id")
@Dict(dictTable = "ocr_task_type", dicText = "type_name", dicCode = "id")
/*@Dict(dictTable = "ocr_task_type", dicText = "type_name", dicCode = "id")*/
@ApiModelProperty(value = "任务类型")
private String taskType;
/**识别路径*/
@ -112,4 +113,12 @@ public class OcrIdentify implements Serializable {
@ApiModelProperty(value = "请求id")
public String requestId;
//======================非数据库映射
/**任务类型_字段中文*/
@ApiModelProperty(value = "任务类型名称")
@Excel(name = "任务类型", width = 15)
@TableField(exist = false)
private String taskType_dictText;
}

@ -8,6 +8,7 @@ import org.jeecg.modules.ocr.model.OcrMetadataConfigSaveModel;
import org.jeecg.modules.ocr.vo.OcrMetadataConfigVo;
import java.util.List;
import java.util.Map;
/**
* @Description:
@ -52,4 +53,6 @@ public interface IOcrMetadataConfigService extends IService<OcrMetadataConfig> {
OcrMetadataConfigVo findById(String id);
void saveModelBatch(List<OcrMetadataConfigSaveModel> saveModelList);
Map<String, OcrMetadataConfigVo> listToMap(List<String> idList);
}

@ -8,6 +8,7 @@ import org.jeecg.modules.ocr.model.OcrRuleCheckSaveModel;
import org.jeecg.modules.ocr.vo.OcrRuleCheckVo;
import java.util.List;
import java.util.Map;
/**
* @Description:
@ -70,4 +71,7 @@ public interface IOcrRuleCheckService extends IService<OcrRuleCheck> {
* @return
*/
List<OcrRuleCheckVo> allList(List<String> selectionList);
Map<String, OcrRuleCheckVo> listToMap(List<String> selectionList);
}

@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
import org.jeecg.modules.ocr.entity.OcrTaskType;
import java.util.List;
import java.util.Map;
/**
* @Description:
@ -27,4 +28,6 @@ public interface IOcrTaskTypeService extends IService<OcrTaskType> {
boolean checkMetadataConfigUse(List<String> taskTypeIdList);
OcrTaskType saveTaskType(String taskTypeName);
Map<String,String> listNameToMap(List<String> taskTypeIdList);
}

@ -130,9 +130,6 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
String fieldName = value.getFieldName();//校验的字段名称
String ruleInfo = value.getRuleInfo();//是否绝对判断 0-绝对判断1-不绝对判断
String inputText = value.getInputText();//校验文本 ocr识别的文本如果不包含该内容则算作失败.
if (StringUtils.isBlank(inputText)) {
continue;
}
List<String> fieldNameList = Arrays.asList(fieldName.split(","));
boolean b = ArrayOUtils.containsStringList(fieldNameList, semanticResult.keySet().stream().collect(Collectors.toList()));
//查看ocr识别返回的字段名称中是否有当前这个字段名称
@ -150,9 +147,11 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
JSONObject ocrItem = ocrArray.get(i);
text = ocrItem.getString("text");//ocr 识别的文本
probability = ocrItem.getDouble("probability");//置信度
// 1 不需要校验只需要有返回ocr识别值
if ("1".equals(ruleInfo) && StringUtils.isNotBlank(text)) {
if(inputText==null){
ocrResultAdd(ocrResultList, field, inputText, text, probability, imgPath, "", true);
fieldRightMap.put(field, true);
}else if ("1".equals(ruleInfo) && StringUtils.isNotBlank(text)) {
// 1 不需要校验只需要有返回ocr识别值
ocrResultAdd(ocrResultList, field, inputText, text, probability, imgPath, "", true);
fieldRightMap.put(field, true);
} else {

@ -94,6 +94,13 @@ public class OcrMetadataConfigServiceImpl extends ServiceImpl<OcrMetadataConfigM
}
}
@Override
public Map<String, OcrMetadataConfigVo> listToMap(List<String> idList) {
List<OcrMetadataConfigVo> metadataConfigVoList =baseMapper.allList(idList!=null&&idList.size()>0?idList:null);
Map<String, OcrMetadataConfigVo> metadataConfigVoMap = metadataConfigVoList.stream().collect(Collectors.toMap(OcrMetadataConfigVo::getId, o -> o));
return metadataConfigVoMap;
}
@Override
public OcrMetadataConfig updateModel(OcrMetadataConfigSaveModel ocrMetadataConfigSaveModel) {

@ -23,6 +23,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
/**
* @Description:
@ -64,7 +65,14 @@ public class OcrRuleCheckServiceImpl extends ServiceImpl<OcrRuleCheckMapper, Ocr
@Override
public List<OcrRuleCheckVo> allList(List<String> selectionList) {
return baseMapper.allList(selectionList);
return baseMapper.allList(selectionList!=null && selectionList.size()>0?selectionList:null);
}
@Override
public Map<String, OcrRuleCheckVo> listToMap(List<String> selectionList) {
List<OcrRuleCheckVo> ocrRuleCheckVoList = allList(selectionList);
Map<String, OcrRuleCheckVo> collect = ocrRuleCheckVoList.stream().collect(Collectors.toMap(OcrRuleCheckVo::getId, o -> o));
return collect;
}
@Override

@ -12,6 +12,7 @@ import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
@ -75,4 +76,14 @@ public class OcrTaskTypeServiceImpl extends ServiceImpl<OcrTaskTypeMapper, OcrTa
super.save(ocrTaskType);
return ocrTaskType;
}
@Override
public Map<String, String> listNameToMap(List<String> taskTypeIdList) {
List<OcrTaskType> list=taskTypeIdList!=null && taskTypeIdList.size()>0 ? super.listByIds(taskTypeIdList):super.list();
if (list==null || list.size()==0) {
return new HashMap<>();
}
Map<String, String> map = list.stream().collect(Collectors.toMap(OcrTaskType::getId, OcrTaskType::getTypeName));
return map;
}
}

Loading…
Cancel
Save