master
周文涛 2 years ago
parent 61e7a4d50e
commit 6cd0cdf306

@ -72,6 +72,8 @@ public class OcrIdentifyController extends JeecgController<OcrIdentify, IOcrIden
@PostMapping(value = "/add")
public Result<String> add(@RequestBody OcrIdentify ocrIdentify) {
ocrIdentifyService.save(ocrIdentify);
//请求ocr识别接口
ocrIdentifyService.postSemantic(ocrIdentify.getId(),ocrIdentify.getIdentifyUrl());
return Result.OK("添加成功!");
}

@ -1,10 +1,15 @@
package org.jeecg.modules.ocr.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@ -13,6 +18,8 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.AssertUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.config.JeecgBaseConfig;
import org.jeecg.modules.ocr.entity.OcrMetadataConfig;
import org.jeecg.modules.ocr.entity.OcrMetadataConfigDetail;
import org.jeecg.modules.ocr.model.OcrMetadataConfigSaveModel;
@ -20,6 +27,9 @@ import org.jeecg.modules.ocr.service.IOcrMetadataConfigService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.ocr.utils.DownloadTemplateUtil;
import org.jeecg.modules.ocr.vo.OcrMetadataConfigVo;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
@ -29,38 +39,40 @@ import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
/**
/**
* @Description:
* @Author: jeecg-boot
* @Date: 2023-07-14
* @Date: 2023-07-14
* @Version: V1.0
*/
@Api(tags="元数据配置")
@Api(tags = "元数据配置")
@RestController
@RequestMapping("/ocrMetadataConfig")
@Slf4j
public class OcrMetadataConfigController extends JeecgController<OcrMetadataConfig, IOcrMetadataConfigService> {
@Autowired
private IOcrMetadataConfigService ocrMetadataConfigService;
@Autowired
private DownloadTemplateUtil downloadTemplateUtil;
/**
*
*
* @param ocrMetadataConfig
* @param pageNo
* @param pageSize
* @param req
* @return
*/
//@AutoLog(value = "元数据配置-分页列表查询")
@ApiOperation(value="元数据配置-分页列表查询", notes="元数据配置-分页列表查询")
@GetMapping(value = "/list")
public Result<IPage<OcrMetadataConfigVo>> queryPageList(OcrMetadataConfig ocrMetadataConfig,
@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
HttpServletRequest req) {
@Autowired
private IOcrMetadataConfigService ocrMetadataConfigService;
@Autowired
private DownloadTemplateUtil downloadTemplateUtil;
@Resource
private JeecgBaseConfig jeecgBaseConfig;
/**
*
*
* @param ocrMetadataConfig
* @param pageNo
* @param pageSize
* @param req
* @return
*/
//@AutoLog(value = "元数据配置-分页列表查询")
@ApiOperation(value = "元数据配置-分页列表查询", notes = "元数据配置-分页列表查询")
@GetMapping(value = "/list")
public Result<IPage<OcrMetadataConfigVo>> queryPageList(OcrMetadataConfig ocrMetadataConfig,
@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
HttpServletRequest req) {
/*if (StringUtils.isNotBlank(ocrMetadataConfig.getConfigName())){
ocrMetadataConfig.setConfigName("*"+ocrMetadataConfig.getConfigName()+"*");
}
@ -70,146 +82,175 @@ public class OcrMetadataConfigController extends JeecgController<OcrMetadataConf
if (StringUtils.isNotBlank(ocrMetadataConfig.getGetField())){
ocrMetadataConfig.setGetField("*"+ocrMetadataConfig.getGetField()+"*");
}*/
//QueryWrapper<OcrMetadataConfig> queryWrapper = QueryGenerator.initQueryWrapper(ocrMetadataConfig, req.getParameterMap());
Page<OcrMetadataConfig> page = new Page<OcrMetadataConfig>(pageNo, pageSize);
IPage<OcrMetadataConfigVo> pageList = ocrMetadataConfigService.pageVo(page, ocrMetadataConfig);
return Result.OK(pageList);
}
@ApiOperation(value="元数据配置-下拉列表", notes="元数据配置-下拉列表")
@GetMapping(value = "/allList")
public Result<List<OcrMetadataConfigVo>> allList(HttpServletRequest req){
List<OcrMetadataConfigVo> allList= ocrMetadataConfigService.allList();
return Result.OK(allList);
}
/**
*
*
* @param ocrMetadataConfigSaveModel
* @return
*/
@AutoLog(value = "元数据配置-添加")
@ApiOperation(value="元数据配置-添加", notes="元数据配置-添加")
//QueryWrapper<OcrMetadataConfig> queryWrapper = QueryGenerator.initQueryWrapper(ocrMetadataConfig, req.getParameterMap());
Page<OcrMetadataConfig> page = new Page<OcrMetadataConfig>(pageNo, pageSize);
IPage<OcrMetadataConfigVo> pageList = ocrMetadataConfigService.pageVo(page, ocrMetadataConfig);
return Result.OK(pageList);
}
@ApiOperation(value = "元数据配置-下拉列表", notes = "元数据配置-下拉列表")
@GetMapping(value = "/allList")
public Result<List<OcrMetadataConfigVo>> allList(HttpServletRequest req) {
List<OcrMetadataConfigVo> allList = ocrMetadataConfigService.allList(null);
return Result.OK(allList);
}
/**
*
*
* @param ocrMetadataConfigSaveModel
* @return
*/
@AutoLog(value = "元数据配置-添加")
@ApiOperation(value = "元数据配置-添加", notes = "元数据配置-添加")
// @RequiresPermissions("org.jeecg.modules.ocr:ocr_metadata_config:add")
@PostMapping(value = "/add")
@Transactional(rollbackFor = Exception.class)
public Result<String> add(@RequestBody OcrMetadataConfigSaveModel ocrMetadataConfigSaveModel) {
checkSaveModel(ocrMetadataConfigSaveModel);
String configName = ocrMetadataConfigSaveModel.getConfigName();
boolean existsFlag=ocrMetadataConfigService.existsByConfigName(configName,null);//true 已存在,false不存在
AssertUtils.notTrue(existsFlag,String.format("[配置名称]-%s 已存在",configName));
ocrMetadataConfigService.saveModel(ocrMetadataConfigSaveModel);
return Result.OK("添加成功!");
}
/**
*
* @param ocrMetadataConfigSaveModel
*/
public void checkSaveModel(OcrMetadataConfigSaveModel ocrMetadataConfigSaveModel){
AssertUtils.notEmpty(ocrMetadataConfigSaveModel.getConfigName(),"[配置名称]不可为空");
if (ocrMetadataConfigSaveModel.getOcrMetadataConfigDetailList()!=null) {
for (OcrMetadataConfigDetail detail : ocrMetadataConfigSaveModel.getOcrMetadataConfigDetailList()) {
AssertUtils.notEmpty(detail.getFieldName(),"[字段名称]不可为空");
AssertUtils.notEmpty(detail.getGetField(),"[获取字段]不可为空");
}
}
}
/**
*
*
* @param ocrMetadataConfigSaveModel
* @return
*/
@AutoLog(value = "元数据配置-编辑")
@ApiOperation(value="元数据配置-编辑", notes="元数据配置-编辑")
@PostMapping(value = "/add")
@Transactional(rollbackFor = Exception.class)
public Result<String> add(@RequestBody OcrMetadataConfigSaveModel ocrMetadataConfigSaveModel) {
checkSaveModel(ocrMetadataConfigSaveModel);
String configName = ocrMetadataConfigSaveModel.getConfigName();
boolean existsFlag = ocrMetadataConfigService.existsByConfigName(configName, null);//true 已存在,false不存在
AssertUtils.notTrue(existsFlag, String.format("[配置名称]-%s 已存在", configName));
ocrMetadataConfigService.saveModel(ocrMetadataConfigSaveModel);
return Result.OK("添加成功!");
}
/**
*
*
* @param ocrMetadataConfigSaveModel
*/
public void checkSaveModel(OcrMetadataConfigSaveModel ocrMetadataConfigSaveModel) {
AssertUtils.notEmpty(ocrMetadataConfigSaveModel.getConfigName(), "[配置名称]不可为空");
if (ocrMetadataConfigSaveModel.getOcrMetadataConfigDetailList() != null) {
for (OcrMetadataConfigDetail detail : ocrMetadataConfigSaveModel.getOcrMetadataConfigDetailList()) {
AssertUtils.notEmpty(detail.getFieldName(), "[字段名称]不可为空");
AssertUtils.notEmpty(detail.getGetField(), "[获取字段]不可为空");
}
}
}
/**
*
*
* @param ocrMetadataConfigSaveModel
* @return
*/
@AutoLog(value = "元数据配置-编辑")
@ApiOperation(value = "元数据配置-编辑", notes = "元数据配置-编辑")
// @RequiresPermissions("org.jeecg.modules.ocr:ocr_metadata_config:edit")
@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
public Result<String> edit(@RequestBody OcrMetadataConfigSaveModel ocrMetadataConfigSaveModel) {
checkSaveModel(ocrMetadataConfigSaveModel);
AssertUtils.notEmpty(ocrMetadataConfigSaveModel.getId(),"[id]不可为空");
String configName = ocrMetadataConfigSaveModel.getConfigName();
boolean existsFlag=ocrMetadataConfigService.existsByConfigName(configName,ocrMetadataConfigSaveModel.getId());//true 已存在,false不存在
AssertUtils.notTrue(existsFlag,String.format("[配置名称]-%s 已存在",configName));
ocrMetadataConfigService.updateModel(ocrMetadataConfigSaveModel);
return Result.OK("编辑成功!");
}
/**
* id
*
* @param id
* @return
*/
@AutoLog(value = "元数据配置-通过id删除")
@ApiOperation(value="元数据配置-通过id删除", notes="元数据配置-通过id删除")
@RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
public Result<String> edit(@RequestBody OcrMetadataConfigSaveModel ocrMetadataConfigSaveModel) {
checkSaveModel(ocrMetadataConfigSaveModel);
AssertUtils.notEmpty(ocrMetadataConfigSaveModel.getId(), "[id]不可为空");
String configName = ocrMetadataConfigSaveModel.getConfigName();
boolean existsFlag = ocrMetadataConfigService.existsByConfigName(configName, ocrMetadataConfigSaveModel.getId());//true 已存在,false不存在
AssertUtils.notTrue(existsFlag, String.format("[配置名称]-%s 已存在", configName));
ocrMetadataConfigService.updateModel(ocrMetadataConfigSaveModel);
return Result.OK("编辑成功!");
}
/**
* id
*
* @param id
* @return
*/
@AutoLog(value = "元数据配置-通过id删除")
@ApiOperation(value = "元数据配置-通过id删除", notes = "元数据配置-通过id删除")
// @RequiresPermissions("org.jeecg.modules.ocr:ocr_metadata_config:delete")
@DeleteMapping(value = "/delete")
public Result<String> delete(@RequestParam(name="id",required=true) String id) {
ocrMetadataConfigService.removeById(id);
return Result.OK("删除成功!");
}
/**
*
*
* @param ids
* @return
*/
@AutoLog(value = "元数据配置-批量删除")
@ApiOperation(value="元数据配置-批量删除", notes="元数据配置-批量删除")
@DeleteMapping(value = "/delete")
public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
ocrMetadataConfigService.removeById(id);
return Result.OK("删除成功!");
}
/**
*
*
* @param ids
* @return
*/
@AutoLog(value = "元数据配置-批量删除")
@ApiOperation(value = "元数据配置-批量删除", notes = "元数据配置-批量删除")
// @RequiresPermissions("org.jeecg.modules.ocr:ocr_metadata_config:deleteBatch")
@DeleteMapping(value = "/deleteBatch")
public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
this.ocrMetadataConfigService.removeByIds(Arrays.asList(ids.split(",")));
return Result.OK("批量删除成功!");
}
/**
* id
*
* @param id
* @return
*/
//@AutoLog(value = "元数据配置-通过id查询")
@ApiOperation(value="元数据配置-通过id查询", notes="元数据配置-通过id查询")
@GetMapping(value = "/queryById")
public Result<OcrMetadataConfigVo> queryById(@RequestParam(name="id",required=true) String id) {
OcrMetadataConfigVo ocrMetadataConfig = ocrMetadataConfigService.findById(id);
if(ocrMetadataConfig==null) {
return Result.error("未找到对应数据");
}
return Result.OK(ocrMetadataConfig);
}
@DeleteMapping(value = "/deleteBatch")
public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
this.ocrMetadataConfigService.removeByIds(Arrays.asList(ids.split(",")));
return Result.OK("批量删除成功!");
}
/**
* id
*
* @param id
* @return
*/
//@AutoLog(value = "元数据配置-通过id查询")
@ApiOperation(value = "元数据配置-通过id查询", notes = "元数据配置-通过id查询")
@GetMapping(value = "/queryById")
public Result<OcrMetadataConfigVo> queryById(@RequestParam(name = "id", required = true) String id) {
OcrMetadataConfigVo ocrMetadataConfig = ocrMetadataConfigService.findById(id);
if (ocrMetadataConfig == null) {
return Result.error("未找到对应数据");
}
return Result.OK(ocrMetadataConfig);
}
/**
* excel
*
* @param request
* @param ocrMetadataConfig
*/
* excel
*
* @param request
* @param ocrMetadataConfig
*/
// @RequiresPermissions("org.jeecg.modules.ocr:ocr_metadata_config:exportXls")
@RequestMapping(value = "/exportXls")
public ModelAndView exportXls(HttpServletRequest request, OcrMetadataConfig ocrMetadataConfig) {
return super.exportXls(request, ocrMetadataConfig, OcrMetadataConfig.class, "元数据配置");
// 过滤选中数据
String selections = request.getParameter("selections");
String title = "元数据配置";
ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
List<OcrMetadataConfigVo> exportList = new ArrayList<>();
if (oConvertUtils.isNotEmpty(selections)) {
List<String> selectionList = Arrays.asList(selections.split(","));
exportList = ocrMetadataConfigService.allList(selectionList);
}
if(true){
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
// Step.2 获取导出数据
// Step.3 AutoPoi 导出Excel
//此处设置的filename无效 ,前端会重更新设置一下
mv.addObject(NormalExcelConstants.FILE_NAME, title);
mv.addObject(NormalExcelConstants.CLASS, OcrMetadataConfigVo.class);
//update-begin--Author:liusq Date:20210126 for图片导出报错ImageBasePath未设置--------------------
ExportParams exportParams = new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title);
exportParams.setImageBasePath(jeecgBaseConfig.getPath().getUpload());
//update-end--Author:liusq Date:20210126 for图片导出报错ImageBasePath未设置----------------------
mv.addObject(NormalExcelConstants.PARAMS, exportParams);
mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
}
return mv;
}
/**
*
*/
@RequestMapping(value = "/downloadTemplate")
public ModelAndView downloadTemplate() {
return downloadTemplateUtil.downloadTemplate(OcrMetadataConfig.class,"元数据配置");
}
/**
*
*/
@RequestMapping(value = "/downloadTemplate")
public ModelAndView downloadTemplate() {
return downloadTemplateUtil.downloadTemplate(OcrMetadataConfig.class, "元数据配置");
}
/**
* excel
*
* @param request
* @param response
* @return
*/
* excel
*
* @param request
* @param response
* @return
*/
// @RequiresPermissions("org.jeecg.modules.ocr:ocr_metadata_config:importExcel")
@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {

@ -60,8 +60,8 @@ public class OcrMetadataConfig implements Serializable {
@ApiModelProperty(value = "配置名称")
private String configName;
/**结果名称*/
@Excel(name = "结果名称", width = 15)
@ApiModelProperty(value = "结果名称")
@Excel(name = "字段名称", width = 15)
@ApiModelProperty(value = "字段名称")
@TableField(exist = false)
private String resultName;
/**获取字段*/

@ -0,0 +1,16 @@
package org.jeecg.modules.ocr.model;
import lombok.Data;
/**
* @Description
* @Author ZhouWenTao
* @Date 2023/7/21 11:42
*/
@Data
public class CheckSemanticModel {
private String field; //字段名
private String fieldName; //字段中文含义
private String inputText; // 匹配值
private String ruleInfo="1"; // 1/0
}

@ -0,0 +1,27 @@
package org.jeecg.modules.ocr.model;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @Description ocr
* @Author ZhouWenTao
* @Date 2023/7/21 14:51
*/
@Data
public class OcrResult {
@ApiModelProperty(value = "检索字段名称")
private String tag;
@ApiModelProperty(value = "api输入值")
private String inputText;
@ApiModelProperty(value = "ocr识别文本")
private String ocrText;
@ApiModelProperty(value = "ocr识别置信度")
private Double ocrPrecisionRate;
@ApiModelProperty(value = "图片来源")
private SourceImage sourceImage;
@ApiModelProperty(value = "失败原因")
private String failureReason;
@ApiModelProperty(value = "规则验证结果")
private Boolean ruleValidation=false;
}

@ -0,0 +1,16 @@
package org.jeecg.modules.ocr.model;
import lombok.Data;
/**
* @Description
* @Author ZhouWenTao
* @Date 2023/7/21 14:52
*/
@Data
public class SourceImage {
//附件名称
private String fileName;
//附件地址
private String path;
}

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.IService;
import org.jeecg.modules.ocr.entity.OcrIdentify;
import org.jeecg.modules.ocr.vo.OcrIdentifyVo;
import org.springframework.scheduling.annotation.Async;
/**
* @Description: ocr
@ -18,4 +19,7 @@ public interface IOcrIdentifyService extends IService<OcrIdentify> {
* @param responseBody
*/
public void getSemanticInfo(JSONObject responseBody);
@Async
void postSemantic(String id, String identifyUrl);
}

@ -47,7 +47,7 @@ public interface IOcrMetadataConfigService extends IService<OcrMetadataConfig> {
*/
IPage<OcrMetadataConfigVo> pageVo(Page<OcrMetadataConfig> page, OcrMetadataConfig ocrMetadataConfig);
List<OcrMetadataConfigVo> allList();
List<OcrMetadataConfigVo> allList(List<String> idList);
OcrMetadataConfigVo findById(String id);
}

@ -1,11 +1,16 @@
package org.jeecg.modules.ocr.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.C;
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.service.IOcrIdentifyDetailService;
import org.jeecg.modules.ocr.service.IOcrIdentifyService;
import org.jeecg.modules.ocr.service.IOcrRuleCheckDetailService;
@ -13,10 +18,14 @@ import org.jeecg.modules.ocr.service.IOcrRuleCheckService;
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.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
@ -38,7 +47,9 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
public OcrIdentifyVo findById(String id){
OcrIdentifyVo ocrIdentifyVo=baseMapper.findById(id);
if (StringUtils.isNotBlank(ocrIdentifyVo.getSourceJson())) {
ocrIdentifyVo.setSourceJsonObject(JSONObject.parseObject(ocrIdentifyVo.getSourceJson()));
JSONArray jsonArray = JSONObject.parseArray(ocrIdentifyVo.getSourceJson());
List<JSONObject> jsonObjects = jsonArray.toJavaList(JSONObject.class);
ocrIdentifyVo.setSourceJsonObjects(jsonObjects);
}
//规则检查配置信息
String ruleCheck = ocrIdentifyVo.getRuleCheck();
@ -82,27 +93,181 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
OcrIdentifyVo ocrIdentifyVo = this.findById(identifyId);
OcrRuleCheckVo ocrRuleCheckVo = ocrIdentifyVo.getOcrRuleCheckVo();
// field,1/0
Map<String, String> configRuleMap = ocrRuleCheckVo.getConfigRuleMap();
// field,fieldname
Map<String, String> fieldMap = ocrRuleCheckVo.getFieldMap();
//对参数做匹配
JSONObject sourceJsonObject = ocrIdentifyVo.getSourceJsonObject();
//将 语义化结果 返回的 key 转换成 字段
for (String field : sourceJsonObject.keySet()) {
String fieldName = sourceJsonObject.getString(field);
semanticResultFor: for (String s : semanticResult.keySet()) {
if (fieldName.contains(s)) {
break semanticResultFor;
Map<String, CheckSemanticModel> checkSemanticModelMap = getCheckSemanticModelMap(ocrRuleCheckVo.getConfigRuleMap(), ocrRuleCheckVo.getFieldMap(),ocrIdentifyVo.getSourceJsonObjects());
////
String text;//ocr 识别的文本,
Double probability=0d;
OcrResult copyOcrResult=new OcrResult();
//用于数据结构化的对象
List<OcrResult> ocrResultList=new ArrayList<>();
StringBuffer rMessage=new StringBuffer();
rMessage.append(message).append("<br/>");
Map<String,Boolean> fieldRightMap=new LinkedHashMap<>();//存放 字段判断正确map
//==========================
checkSemanticFor: for (CheckSemanticModel value : checkSemanticModelMap.values()) {
String field = value.getField();
String fieldName = value.getFieldName();//校验的字段名称
String ruleInfo = value.getRuleInfo();//是否绝对判断 0-绝对判断1-不绝对判断
String inputText = value.getInputText();//校验文本 ocr识别的文本如果不包含该内容则算作失败.
//查看ocr识别返回的字段名称中是否有当前这个字段名称
if (semanticResult.keySet().contains(fieldName)) {
//TODO 注意ocr 识别返回的 字段是多个结果(数组),有一个值匹配上即为正确。
JSONArray ocrArray = semanticResult.getJSONArray(fieldName);
ocrArrayFor:for (int i = 0; i < ocrArray.size(); i++) {
copyOcrResult=new OcrResult();
JSONObject ocrItem = ocrArray.getJSONObject(i);
text = ocrItem.getString("text");//ocr 识别的文本
probability = ocrItem.getDouble("probability");//置信度
// 1 不需要校验只需要有返回ocr识别值
if ("1".equals(ruleInfo) && StringUtils.isNotBlank(text)) {
ocrResultAdd(ocrResultList,field,inputText,text,probability,imgPath,"",true);
fieldRightMap.put(field,true);
}else{
if (StringUtils.isBlank(text) ||!text.contains(inputText)) {
//识别结果为空,识别结果 匹配不上 校验文本
mapPutIfTrue(fieldRightMap,field,false);
rMessage.append(value.getFieldName()+"不匹配,");
ocrResultAdd(ocrResultList,field,inputText,text,probability,imgPath,value.getFieldName()+"不匹配",false);
}else{
ocrResultAdd(ocrResultList,field,inputText,text,probability,imgPath,"",true);
fieldRightMap.put(field,true);
}
}
}
}
rMessage.append(field+"_未查到ocr结果,");
ocrResultAdd(ocrResultList,field,inputText,null,0d,imgPath,"未查到ocr结果",false);
ocrResultList.add(copyOcrResult);
fieldRightMap.put(field,false);
}
for (String s : semanticResult.keySet()) {
//其中有一个字段 全部都是 失败,则该图片失败.
if (fieldRightMap!=null&&fieldRightMap.values().size()>0&&!fieldRightMap.containsValue(false)) {
ocrIdentifyDetail.setStatus("1");
}else{
ocrIdentifyDetail.setStatus("0");
ocrIdentifyDetail.setMessage(rMessage.toString());
}
ocrIdentifyDetail.setDataStructured(JSONArray.toJSONString(ocrResultList));//数据结构化
ocrIdentifyDetailService.updateById(ocrIdentifyDetail);
}
//=========================
}
@Async
@Override
public void postSemantic(String id, String identifyUrl) {
}
public static void mapPutIfTrue(Map map,String key,Boolean flag){
if (map.containsKey(key) && (Boolean) map.get(key).equals(true)) {
}else{
map.put(key,flag);
}
}
/**
* OcrResult, ocrResultList
* @param ocrResultList
* @param field
* @param inputText
* @param ocrText
* @param ocrPrecisionRate
* @param imgPath
* @param failureReason
*/
public static void ocrResultAdd(List<OcrResult> ocrResultList,String field,String inputText,String ocrText,Double ocrPrecisionRate,String imgPath,String failureReason,Boolean ruleValidation){
OcrResult ocrResult = new OcrResult();
ocrResult.setTag(field);
ocrResult.setOcrText(ocrText);
ocrResult.setInputText(inputText);
ocrResult.setOcrPrecisionRate(ocrPrecisionRate==null?0d:ocrPrecisionRate);
if (StringUtils.isNotBlank(imgPath)) {
SourceImage sourceImage = new SourceImage();
sourceImage.setPath(imgPath);
int i = imgPath.lastIndexOf("/");
sourceImage.setFileName(imgPath.substring(i,imgPath.length()));
ocrResult.setSourceImage(sourceImage);
}
ocrResult.setFailureReason(failureReason);
if (ocrResultList==null) {
ocrResultList=new ArrayList<>();
}
ocrResult.setRuleValidation(ruleValidation);
ocrResultList.add(ocrResult);
}
/**
* checkSemanticModelMap
* @param configRuleMap 1/0 map
* @param fieldMap map
* @return
*/
public Map<String,CheckSemanticModel> getCheckSemanticModelMap(Map<String,String> configRuleMap,Map<String,String> fieldMap, List<JSONObject> sourceJsonObjects){
Map<String,CheckSemanticModel> checkSemanticModelMap=new LinkedHashMap<>();
//校验正确的值
Map<String,String> inputMap=new LinkedHashMap<>();
if (sourceJsonObjects!=null&&sourceJsonObjects.size()>0) {
String tag,inputText;
for (JSONObject sourceJsonObject : sourceJsonObjects) {
tag = sourceJsonObject.getString("tag");
if (StringUtils.isBlank(tag)) {
continue;
}
inputText = sourceJsonObject.getString("inputText");
inputMap.put(tag,inputText);
}
}
CheckSemanticModel copyEntity=null;
String configRule=null,fieldName=null,inputText;
if (fieldMap!=null) {
for (String field : fieldMap.keySet()) {
copyEntity=new CheckSemanticModel();
copyEntity.setField(field);
//1/0
if (configRuleMap!=null) {
configRule = configRuleMap.get(field);
copyEntity.setRuleInfo(configRule);
}
//端字段含义
fieldName = fieldMap.get(field);
//检查数据
inputText = inputMap.get(field);
copyEntity.setFieldName(fieldName);
copyEntity.setInputText(inputText);
checkSemanticModelMap.put(field,copyEntity);
}
}
return checkSemanticModelMap;
}
public static void main(String[] args) {
Map<String,Boolean> fieldRightMap = new LinkedHashMap<>();
fieldRightMap.put("zhangsan",false);
fieldRightMap.put("zhangsan4",true);
mapPutIfTrue(fieldRightMap,"zhangsan",true);
mapPutIfTrue(fieldRightMap,"zhangsan2",false);
mapPutIfTrue(fieldRightMap,"zhangsan3",true);
mapPutIfTrue(fieldRightMap,"zhangsan4",false);
for (String s : fieldRightMap.keySet()) {
System.out.println(s+":"+fieldRightMap.get(s));
}
String ss="[{\"tag\":\"hospitalName\",\"inputText\":\"仁和医院\"},{\"tag\":\"departmentName\",\"inputText\":\"耳鼻喉科\"},{\"tag\":\"doctorName\",\"inputText\":\"张三\"}]";
JSONArray jsonArray = JSONObject.parseArray(ss);
//System.out.println(jsonArray);
}
}

@ -127,9 +127,14 @@ public class OcrMetadataConfigServiceImpl extends ServiceImpl<OcrMetadataConfigM
}
@Override
public List<OcrMetadataConfigVo> allList() {
public List<OcrMetadataConfigVo> allList(List<String> idList) {
List<OcrMetadataConfigVo> allList=new ArrayList<>();
List<OcrMetadataConfig> list = this.list();
List<OcrMetadataConfig> list = new ArrayList<>();
if (idList!=null && idList.size()>0) {
list=this.listByIds(idList);
}else{
list=this.list();
}
OcrMetadataConfigVo copyEntityVo=null;
List<OcrMetadataConfigDetail> metadataConfigDetails = ocrMetadataConfigDetailService.list();
List<OcrMetadataConfigDetail> arrayTestList=new ArrayList<>();

@ -1,10 +1,14 @@
package org.jeecg.modules.ocr.vo;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.jeecg.modules.ocr.entity.OcrIdentify;
import org.jeecg.modules.ocr.entity.OcrRuleCheck;
import java.util.List;
import java.util.Map;
/**
* @Description ocr
* @Author ZhouWenTao
@ -16,5 +20,5 @@ public class OcrIdentifyVo extends OcrIdentify {
public OcrRuleCheckVo ocrRuleCheckVo;
/**校验数据源 json对象*/
public JSONObject sourceJsonObject;
public List<JSONObject> sourceJsonObjects;
}

Loading…
Cancel
Save