Przeglądaj źródła

证件识别调整

lzm 2 lat temu
rodzic
commit
dae5cbfcaf

+ 13 - 4
business-system/filing-system/src/main/java/com/pj/api/WxController.java

@@ -209,21 +209,30 @@ public class WxController {
     }
 
     /**
-     * 识别边民证
+     * 识别边民证,弃用
      */
     @RequestMapping("ocrIcCard")
-    public AjaxJson ocrIcCard(String icCardUrl) throws Exception {
+    public AjaxJson ocrIcCard(String icCardUrl) {
         OcrResult ocrResult = ocrService.ocrIcCard(icCardUrl);
         return AjaxJson.getSuccessData(ocrResult);
     }
 
     /**
-     * 识别身份证
+     * 识别身份证,弃用
      */
     @RequestMapping("ocrIdCard")
-    public AjaxJson ocrIdCard(String idCardUrl) throws Exception {
+    public AjaxJson ocrIdCard(String idCardUrl){
         OcrResult ocrResult = ocrService.ocrIdCard(idCardUrl);
         return AjaxJson.getSuccessData(ocrResult);
     }
 
+    /**
+     * 识别身份证或边民证
+     */
+    @RequestMapping("ocrIdOrIcCard")
+    public AjaxJson ocrIdOrIcCard(String cardUrl) {
+        OcrResult ocrResult = ocrService.ocrIdOrIc(cardUrl);
+        return AjaxJson.getSuccessData(ocrResult);
+    }
+
 }

+ 46 - 6
sp-core/src/main/java/com/pj/project/ocr/OcrService.java

@@ -11,6 +11,7 @@ import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
 import com.pj.current.config.MyConfig;
 import com.pj.current.config.OcrConfig;
+import com.pj.current.global.BusinessException;
 import com.pj.project.ocr.bo.OcrResult;
 import com.pj.project.ocr.bo.WordsResult;
 import com.pj.project4sp.uploadfile.UploadUtil;
@@ -20,7 +21,9 @@ import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
 import java.io.File;
+import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 import java.util.regex.Pattern;
@@ -38,7 +41,35 @@ public class OcrService {
     @Resource
     MyConfig myConfig;
 
-    public OcrResult ocrIdCard(String idCardPath) throws Exception{
+    /**
+     * 识别边民证或身份证
+     * */
+    public OcrResult ocrIdOrIc(String cardPath){
+        List<String> wordsList = ocrImage(cardPath);
+        OcrResult ocrResult = new OcrResult();
+        if(wordsList.contains("公民身份号码")){
+            String words = wordsList.stream().map(String::valueOf).collect(Collectors.joining(""));
+            String name = getIdName(words);
+            String IdCard = getIdCard(words);
+            ocrResult.setName(name).setIdCard(IdCard).setType(1);
+        }else if(wordsList.contains("本证有效期")){
+            String words = sortIcWords(wordsList);
+            String icNo = getIcNo(words);
+            String name = getIcName(words);
+            String idCard = getIcIdCard(words);
+            String startTime = getIcStartTime(words);
+            String endTime = getIcEndTime(words);
+            ocrResult.setIcNo(icNo).setName(name).setIdCard(idCard).setStartTime(startTime).setEndTime(endTime).setType(2);
+        }else{
+            throw new BusinessException("识别身份信息失败,请检查证件是否上传正确");
+        }
+        return ocrResult;
+    }
+
+    /**
+     * 识别身份证
+     * */
+    public OcrResult ocrIdCard(String idCardPath){
         List<String> wordsList = ocrImage(idCardPath);
         String words = wordsList.stream().map(String::valueOf).collect(Collectors.joining(""));
         //String words = "SINGOMINGZ姓名李卓明SINGQBIEDMINZCUZ性别男民族汉SEN的NIENZ NYIEDHAUH出生1997年12月9日DIEGYOUQ住址广西北流市新圩镇西宁街245号GUNGHMINZSINHFWN HAUMAJ公民身份号码450981199712091118";
@@ -49,8 +80,10 @@ public class OcrService {
         return ocrResult;
     }
 
-
-    public OcrResult ocrIcCard(String icCardPath) throws Exception{
+    /**
+     * 识别边民证
+     * */
+    public OcrResult ocrIcCard(String icCardPath){
         List<String> wordsList = ocrImage(icCardPath);
         String words = sortIcWords(wordsList);
         //String words = "0024745姓名:黄恩华性别:男身份证号码:450681197409172411本证有效期自2021年01月01日起至2022年12月31日止发证单位:东兴市商务和口岸管理局";
@@ -64,7 +97,7 @@ public class OcrService {
         return ocrResult;
     }
 
-    public List<String> ocrImage(String icCardPath) throws Exception{
+    public List<String> ocrImage(String icCardPath){
         //String imagePath = "http://127.0.0.1:8099/pro/upload/image/2022/07-04/bm2.jpg";
         String separator = File.separator;
         String rootPath = UploadUtil.uploadConfig.rootFolder + separator + UploadUtil.uploadConfig.httpPrefix;
@@ -79,7 +112,12 @@ public class OcrService {
         String accessToken = tokenResult.getStr("access_token");
         String accurateUrl = ocrConfig.getAccurateUrl().replaceAll("ACCESS_TOKEN", accessToken);
 
-        String params = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(base64Str, "UTF-8");
+        String params = null;
+        try {
+            params = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(base64Str, "UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            log.info("ocr识别,图片转UTF-8编码失败");
+        }
         String accurateResp = HttpUtil.createPost(accurateUrl).header("Content-Type", "application/x-www-form-urlencoded").body(params)
                 .execute().body();
         log.info("accurateResp:", accurateResp);
@@ -114,7 +152,9 @@ public class OcrService {
 
 
     private String getIcNo(String words) {
-        String icNo = StrUtil.sub(words, 0, 7);
+        int noBeginIndex = StrUtil.indexOf(words, "00", 0, false);
+        String icNo = StrUtil.sub(words, noBeginIndex, noBeginIndex+7);
+        //String icNo = StrUtil.sub(words, 0, 7);
         String regFormat = "00[\\d]{5}";
         boolean matches = Pattern.matches(regFormat, icNo);
         if(!matches){

+ 2 - 0
sp-core/src/main/java/com/pj/project/ocr/bo/OcrResult.java

@@ -19,5 +19,7 @@ public class OcrResult {
     private String idCard;
     private String startTime;
     private String endTime;
+    //证件类型(1=身份证,2=边民证)
+    private Integer type;
 
 }

+ 4 - 0
sp-core/src/main/java/com/pj/project/tb_person_filing/TbPersonFiling.java

@@ -141,6 +141,10 @@ public class TbPersonFiling extends Model<TbPersonFiling> implements Serializabl
      */
     private String icCardUrl;
     /**
+     * 边民证或身份证图片url
+     */
+    private String cardUrl;
+    /**
      * 边民证有效期,起始日期
      */
     @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")

+ 3 - 3
sp-start/src/main/resources/application-dev.yml

@@ -7,9 +7,9 @@ spring:
     # 数据源配置
     datasource: 
         type: com.alibaba.druid.pool.DruidDataSource
-        url: jdbc:mysql://127.0.0.1:3306/smart_face?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
+        url: jdbc:mysql://127.0.0.1:3307/smart_face?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
         username: root
-        password: 123456
+        password: 1234
         # 是否打开sql监控台  (生产环境请务必关闭此选项) 
         druid: 
             stat-view-servlet:
@@ -46,7 +46,7 @@ spring:
     # 项目自定义配置
     myconfig:
         # 本项目部署到的服务器域名(文件上传等等模块  要用到)
-        domain: http://127.0.0.1:8099
+        domain: http://127.0.0.1:8099/pro
         ip: 192.168.3.27
         net-ip: 192.168.3.27
         heart-port: 9999