Browse Source

人员备案-身份证、边民证识别

lzm 2 years ago
parent
commit
3149ddaeb2

+ 12 - 6
business-system/filing-system/src/main/java/com/pj/api/WxController.java

@@ -207,16 +207,22 @@ public class WxController {
         return tbMiniUserService.login(openid,key, password);
     }
 
-
+    /**
+     * 识别边民证
+     */
     @RequestMapping("ocrIcCard")
-    public AjaxJson uploadAndOcrIcCard(String icCardUrl) throws Exception {
+    public AjaxJson ocrIcCard(String icCardUrl) throws Exception {
         OcrResult ocrResult = ocrService.ocrIcCard(icCardUrl);
         return AjaxJson.getSuccessData(ocrResult);
     }
 
-    @RequestMapping("test")
-    public String test() throws Exception{
-        String result = ocrService.test();
-        return result;
+    /**
+     * 识别身份证
+     */
+    @RequestMapping("ocrIdCard")
+    public AjaxJson ocrIdCard(String idCardUrl) throws Exception {
+        OcrResult ocrResult = ocrService.ocrIdCard(idCardUrl);
+        return AjaxJson.getSuccessData(ocrResult);
     }
+
 }

+ 82 - 35
sp-core/src/main/java/com/pj/project/ocr/OcrService.java

@@ -1,6 +1,10 @@
 package com.pj.project.ocr;
 
 import cn.hutool.core.codec.Base64;
+import cn.hutool.core.date.DateException;
+import cn.hutool.core.date.DateTime;
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.IdcardUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.http.HttpUtil;
 import cn.hutool.json.JSONObject;
@@ -9,22 +13,17 @@ import com.pj.current.config.MyConfig;
 import com.pj.current.config.OcrConfig;
 import com.pj.project.ocr.bo.OcrResult;
 import com.pj.project.ocr.bo.WordsResult;
-import com.pj.project.ocr.util.OcrHttpUtil;
 import com.pj.project4sp.uploadfile.UploadUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
-import sun.misc.BASE64Encoder;
 
 import javax.annotation.Resource;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
 import java.net.URLEncoder;
-import java.util.HashMap;
+import java.util.Date;
 import java.util.List;
-import java.util.Map;
+import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
 /**
@@ -39,51 +38,68 @@ public class OcrService {
     @Resource
     MyConfig myConfig;
 
-    public String test() throws Exception{
-        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;
-        String prefix = myConfig.getDomain() + "/pro" + UploadUtil.uploadConfig.httpPrefix;
-        imagePath = imagePath.replaceAll(prefix, rootPath);
-        File imageFile = new File(imagePath);
-        String base64Str = Base64.encode(imageFile);
+    public OcrResult ocrIdCard(String idCardPath) throws Exception{
+//        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";
+        String name = getIdName(words);
+        String IdCard = getIdCard(words);
+        OcrResult ocrResult = new OcrResult();
+        ocrResult.setName(name).setIdCard(IdCard);
+        return ocrResult;
+    }
 
-        String tokenUrl = ocrConfig.getAccessTokenUrl();
-        String resp = HttpUtil.get(tokenUrl);
-        JSONObject tokenResult = JSONUtil.parseObj(resp);
-        String accessToken = tokenResult.getStr("access_token");
-        String accurateUrl = ocrConfig.getAccurateUrl().replaceAll("ACCESS_TOKEN", accessToken);
+    private String getIdCard(String words) {
+        int idBeginIndex = StrUtil.indexOf(words, "公民身份号码", 0, false);
+        String idCard = StrUtil.sub(words, idBeginIndex+6, idBeginIndex+24);
+        if(!IdcardUtil.isValidCard(idCard)){
+            idCard = "";
+        }
+        return idCard;
 
-        String params = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(base64Str, "UTF-8");
-        String result = HttpUtil.createPost(accurateUrl).header("Content-Type", "application/x-www-form-urlencoded").body(params)
-        .execute().body();
-        log.info("result:", result);
-        return result;
+    }
 
+    private String getIdName(String words) {
+        int nameBeginIndex = StrUtil.indexOf(words, "姓名", 0, false);
+        int nameEndIndex = nameBeginIndex+3;
+        for(int i=nameBeginIndex; i<words.length(); i++){
+            if(words.charAt(i)>='a' && words.charAt(i)<='z' || words.charAt(i)>='A' && words.charAt(i)<='Z'){
+                nameEndIndex = i;
+                break;
+            }
+        }
+        String name = StrUtil.sub(words, nameBeginIndex+2, nameEndIndex);
+        String regFormat = "[\\u4e00-\\u9fa5]{2,4}";
+        boolean matches = Pattern.matches(regFormat, name);
+        if(!matches){
+            name = "";
+        }
+        return  name;
     }
 
+
     public OcrResult ocrIcCard(String icCardPath) throws Exception{
-        String words = this.ocrImage(icCardPath);
-        String icId = getIcId(words);
+        //List<String> wordsList = ocrImage(icCardPath);
+        //String words = sortIcWords(wordsList);
+        String words = "0024745姓名:黄恩华性别:男身份证号码:450681197409172411本证有效期自2021年01月01日起至2022年12月31日止发证单位:东兴市商务和口岸管理局";
+        String icNo = getIcNo(words);
         String name = getIcName(words);
         String idCard = getIcIdCard(words);
         String startTime = getIcStartTime(words);
         String endTime = getIcEndTime(words);
         OcrResult ocrResult = new OcrResult();
-        ocrResult.setIcId(icId).setName(name).setIdCard(idCard).setStartTime(startTime).setEndTime(endTime);
+        ocrResult.setIcNo(icNo).setName(name).setIdCard(idCard).setStartTime(startTime).setEndTime(endTime);
         return ocrResult;
     }
 
-    public String ocrImage(String icCardPath) throws Exception{
+    public List<String> ocrImage(String icCardPath) throws Exception{
         //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;
-        String prefix = myConfig.getDomain() + "/pro" + UploadUtil.uploadConfig.httpPrefix;
+        String prefix = myConfig.getDomain() + UploadUtil.uploadConfig.httpPrefix;
         icCardPath = icCardPath.replaceAll(prefix, rootPath);
         File imageFile = new File(icCardPath);
         String base64Str = Base64.encode(imageFile);
-        //String base64Str = getBase64Str(imagePath);
-
 
         String tokenUrl = ocrConfig.getAccessTokenUrl();
         String tokenResp = HttpUtil.get(tokenUrl);
@@ -99,6 +115,10 @@ public class OcrService {
         String wordsResultJson = resultJson.getStr("words_result");
         List<WordsResult> wordsResultList = JSONUtil.toList(wordsResultJson, WordsResult.class);
         List<String> wordsList = wordsResultList.stream().map(w -> w.getWords()).collect(Collectors.toList());
+        return wordsList;
+    }
+
+    private String sortIcWords(List<String> wordsList) {
         int sexIndex  = 0;
         int nameIndex = 0;
         for (int i = 0; i < wordsList.size(); i++) {
@@ -121,32 +141,59 @@ public class OcrService {
     }
 
 
-    private String getIcId(String words) {
-        String icId = StrUtil.sub(words, 0, 7);
-        return icId;
+    private String getIcNo(String words) {
+        String icNo = StrUtil.sub(words, 0, 7);
+        String regFormat = "00[\\d]{5}";
+        boolean matches = Pattern.matches(regFormat, icNo);
+        if(!matches){
+            icNo = "";
+        }
+        return icNo;
     }
     private String getIcName(String words) {
         int nameBeginIndex = StrUtil.indexOf(words, "姓名", 0, false);
         int nameEndIndex = StrUtil.indexOf(words, "性别", 0, false);
         String name = StrUtil.sub(words, nameBeginIndex+3, nameEndIndex);
+        String regFormat = "[\\u4e00-\\u9fa5]{2,4}";
+        boolean matches = Pattern.matches(regFormat, name);
+        if(!matches){
+            name = "";
+        }
         return  name;
     }
     private String getIcIdCard(String words) {
         int idBeginIndex = StrUtil.indexOf(words, "身份证号码", 0, false);
         int idEndIndex = StrUtil.indexOf(words, "本证有效期自", 0, false);
         String idCard = StrUtil.sub(words, idBeginIndex+6, idEndIndex);
+        if(!IdcardUtil.isValidCard(idCard)){
+            idCard = "";
+        }
         return idCard;
     }
     private String getIcStartTime(String words) {
         int startTimeBeginIndex = StrUtil.indexOf(words, "本证有效期自", 0, false);
         int startTimeEndIndex = StrUtil.indexOf(words, "起至", 0, false);
         String startTime = StrUtil.sub(words, startTimeBeginIndex+6, startTimeEndIndex);
+        Date date;
+        try {
+            date = DateUtil.parse(startTime, "yyyy年MM月dd日");
+            startTime = DateUtil.format(date, "yyyy-MM-dd");
+        }catch (DateException e) {
+            startTime = null;
+        }
         return startTime;
     }
     private String getIcEndTime(String words) {
         int endTimeBeginIndex = StrUtil.indexOf(words, "起至", 0, false);
         int endTimeEndIndex = StrUtil.indexOf(words, "止发证单位", 0, false);
         String endTime = StrUtil.sub(words, endTimeBeginIndex+2, endTimeEndIndex);
+        Date date;
+        try {
+            date = DateUtil.parse(endTime, "yyyy年MM月dd日");
+            endTime = DateUtil.format(date, "yyyy-MM-dd");
+        }catch (DateException e) {
+            endTime = null;
+        }
         return endTime;
     }
 

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

@@ -14,7 +14,7 @@ import lombok.experimental.Accessors;
 @Accessors(chain = true)
 public class OcrResult {
 
-    private String icId;
+    private String icNo;
     private String name;
     private String idCard;
     private String startTime;

+ 0 - 153
sp-core/src/main/java/com/pj/project/ocr/etsete.java

@@ -1,153 +0,0 @@
-package com.pj.project.ocr;
-
-import cn.hutool.core.util.StrUtil;
-import cn.hutool.json.JSONArray;
-import cn.hutool.json.JSONObject;
-import cn.hutool.json.JSONUtil;
-import com.pj.project.ocr.bo.WordsResult;
-
-import java.util.List;
-import java.util.stream.Collectors;
-
-/**
- * Created with IntelliJ IDEA.
- *
- * @Auther: lzm
- * @Date: 2022/08/26/15:32
- * @Description:
- */
-public class etsete {
-
-    public static void main(String[] args) {
-        String resp = "{\n" +
-                "    \"words_result\": [\n" +
-                "        {\n" +
-                "            \"words\": \"0024754\",\n" +
-                "            \"location\": {\n" +
-                "                \"top\": 364,\n" +
-                "                \"left\": 251,\n" +
-                "                \"width\": 297,\n" +
-                "                \"height\": 64\n" +
-                "            }\n" +
-                "        },\n" +
-                "        {\n" +
-                "            \"words\": \"姓名:\",\n" +
-                "            \"location\": {\n" +
-                "                \"top\": 520,\n" +
-                "                \"left\": 120,\n" +
-                "                \"width\": 144,\n" +
-                "                \"height\": 70\n" +
-                "            }\n" +
-                "        },\n" +
-                "        {\n" +
-                "            \"words\": \"宋永峰\",\n" +
-                "            \"location\": {\n" +
-                "                \"top\": 511,\n" +
-                "                \"left\": 338,\n" +
-                "                \"width\": 190,\n" +
-                "                \"height\": 63\n" +
-                "            }\n" +
-                "        },\n" +
-                "        {\n" +
-                "            \"words\": \"男\",\n" +
-                "            \"location\": {\n" +
-                "                \"top\": 609,\n" +
-                "                \"left\": 422,\n" +
-                "                \"width\": 63,\n" +
-                "                \"height\": 69\n" +
-                "            }\n" +
-                "        },\n" +
-                "        {\n" +
-                "            \"words\": \"性别:\",\n" +
-                "            \"location\": {\n" +
-                "                \"top\": 676,\n" +
-                "                \"left\": 120,\n" +
-                "                \"width\": 146,\n" +
-                "                \"height\": 68\n" +
-                "            }\n" +
-                "        },\n" +
-                "        {\n" +
-                "            \"words\": \"身份证号码:450681198004042410\",\n" +
-                "            \"location\": {\n" +
-                "                \"top\": 823,\n" +
-                "                \"left\": 127,\n" +
-                "                \"width\": 981,\n" +
-                "                \"height\": 69\n" +
-                "            }\n" +
-                "        },\n" +
-                "        {\n" +
-                "            \"words\": \"本证有效期\",\n" +
-                "            \"location\": {\n" +
-                "                \"top\": 1046,\n" +
-                "                \"left\": 126,\n" +
-                "                \"width\": 297,\n" +
-                "                \"height\": 61\n" +
-                "            }\n" +
-                "        },\n" +
-                "        {\n" +
-                "            \"words\": \"自2021年01月01日起至2022年12月31日止\",\n" +
-                "            \"location\": {\n" +
-                "                \"top\": 1207,\n" +
-                "                \"left\": 139,\n" +
-                "                \"width\": 865,\n" +
-                "                \"height\": 41\n" +
-                "            }\n" +
-                "        },\n" +
-                "        {\n" +
-                "            \"words\": \"发证单位:东兴市商务和口岸管理局\",\n" +
-                "            \"location\": {\n" +
-                "                \"top\": 1347,\n" +
-                "                \"left\": 128,\n" +
-                "                \"width\": 957,\n" +
-                "                \"height\": 61\n" +
-                "            }\n" +
-                "        }\n" +
-                "    ],\n" +
-                "    \"words_result_num\": 9,\n" +
-                "    \"log_id\": 1563055170558893628\n" +
-                "}";
-        JSONObject result = JSONUtil.parseObj(resp);
-        String wordsResultJson = result.getStr("words_result");
-        List<WordsResult> wordsResultList = JSONUtil.toList(wordsResultJson, WordsResult.class);
-        List<String> wordsList = wordsResultList.stream().map(w -> w.getWords()).collect(Collectors.toList());
-
-        int sexIndex  = 0;
-        int nameIndex = 0;
-        for (int i = 0; i < wordsList.size(); i++) {
-            String currWord = wordsList.get(i);
-            if(StrUtil.equals("男", currWord) || StrUtil.equals("女", currWord)){
-                sexIndex = i;
-            }
-            if(StrUtil.contains(currWord, "性别")){
-                nameIndex = i;
-            }
-        }
-        if(nameIndex > sexIndex){
-            String sex = wordsList.get(sexIndex);
-            String name = wordsList.get(nameIndex);
-            wordsList.set(nameIndex, sex);
-            wordsList.set(sexIndex, name);
-        }
-        String words = wordsList.stream().map(String::valueOf).collect(Collectors.joining(""));
-        System.out.println(words);
-        String icId = StrUtil.sub(words, 0, 7);
-        System.out.println(icId);
-        int nameBeginIndex = StrUtil.indexOf(words, "姓名", 0, false);
-        int nameEndIndex = StrUtil.indexOf(words, "性别", 0, false);
-        String name = StrUtil.sub(words, nameBeginIndex+3, nameEndIndex);
-        System.out.println(name);
-        int idBeginIndex = StrUtil.indexOf(words, "身份证号码", 0, false);
-        int idEndIndex = StrUtil.indexOf(words, "本证有效期自", 0, false);
-        String idCard = StrUtil.sub(words, idBeginIndex+6, idEndIndex);
-        System.out.println(idCard);
-        int startTimeBeginIndex = StrUtil.indexOf(words, "本证有效期自", 0, false);
-        int startTimeEndIndex = StrUtil.indexOf(words, "起至", 0, false);
-        String startTime = StrUtil.sub(words, startTimeBeginIndex+6, startTimeEndIndex);
-        System.out.println(startTime);
-        int endTimeBeginIndex = StrUtil.indexOf(words, "起至", 0, false);
-        int endTimeEndIndex = StrUtil.indexOf(words, "止发证单位", 0, false);
-        String endTime = StrUtil.sub(words, endTimeBeginIndex+2, endTimeEndIndex);
-        System.out.println(endTime);
-    }
-
-}

+ 16 - 2
sp-core/src/main/java/com/pj/project/tb_person_filing/TbPersonFiling.java

@@ -74,6 +74,10 @@ public class TbPersonFiling extends Model<TbPersonFiling> implements Serializabl
      * 身份证
      */
     private String idCard;
+    /**
+     * 身份证图片url
+     */
+    private String idCardUrl;
 
     /**
      * 来访事由
@@ -127,11 +131,21 @@ public class TbPersonFiling extends Model<TbPersonFiling> implements Serializabl
     private String judgeBy;
     private String openid;
     /**
-     * 边民证
+     * 边民证
      */
     private String icCard;
-
+    /**
+     * 边民证图片url
+     */
     private String icCardUrl;
+    /**
+     * 边民证有效期,起始日期
+     */
+    private Date icCardStartTime;
+    /**
+     * 边民证有效期,到期时间
+     */
+    private Date icCardEndTime;
 
     @TableField(exist = false)
     private String deptName;

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

@@ -46,7 +46,7 @@ spring:
     # 项目自定义配置
     myconfig:
         # 本项目部署到的服务器域名(文件上传等等模块  要用到)
-        domain: http://192.168.1.3:8099
+        domain: http://127.0.0.1:8099
         ip: 192.168.3.27
         net-ip: 192.168.3.27
         heart-port: 9999
@@ -57,8 +57,10 @@ spring:
         limit-user-count: 45000
 wx:
     enable: true
-    app-id: wxd40a34141872bf0c
-    app-secret: 2e6a69fab1fbab60369ebd21b0882f3e
+#    app-id: wxd40a34141872bf0c
+#    app-secret: 2e6a69fab1fbab60369ebd21b0882f3e
+    app-id: wx4979e287cbed0d86
+    app-secret: 0f2c93b93615b768c6423651ab536ddb
     access-token-key: wechat:base_token_key
     js-api-ticket-key: wechat:jsapi_ticket_key
     access-token-url: https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${wx.app-id}&secret=${wx.app-secret}
@@ -75,4 +77,4 @@ ocr-config:
     app-key: n42E1NMa44BAZDdCxGC6tp7Q
     secret-key: 0BSS01iVb3GWROCnHzRpLdMEGpMqb3vS
     access-token-url: https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=${ocr-config.app-key}&client_secret=${ocr-config.secret-key}
-    accurateUrl: https://aip.baidubce.com/rest/2.0/ocr/v1/accurate?access_token=ACCESS_TOKEN
+    accurateUrl: https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=ACCESS_TOKEN