浏览代码

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

lzm 3 年之前
父节点
当前提交
27ac551ffe

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

@@ -39,9 +39,9 @@ public class OcrService {
     MyConfig myConfig;
 
     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";
+        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();
@@ -49,39 +49,11 @@ public class OcrService {
         return ocrResult;
     }
 
-    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;
-
-    }
-
-    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{
-        //List<String> wordsList = ocrImage(icCardPath);
-        //String words = sortIcWords(wordsList);
-        String words = "0024745姓名:黄恩华性别:男身份证号码:450681197409172411本证有效期自2021年01月01日起至2022年12月31日止发证单位:东兴市商务和口岸管理局";
+        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);
@@ -198,5 +170,34 @@ public class OcrService {
     }
 
 
+    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;
+
+    }
+
+    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;
+    }
+
+
 
 }

+ 0 - 59
sp-core/src/main/java/com/pj/project/ocr/util/OcrHttpUtil.java

@@ -1,59 +0,0 @@
-package com.pj.project.ocr.util;
-
-import java.io.BufferedReader;
-import java.io.DataOutputStream;
-import java.io.InputStreamReader;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.util.List;
-import java.util.Map;
-
-/**
- * @Auther: lzm
-
- */
-public class OcrHttpUtil {
-
-    public static String postGeneralUrl(String generalUrl, String contentType, String params, String encoding)
-            throws Exception {
-        URL url = new URL(generalUrl);
-        // 打开和URL之间的连接
-        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
-        connection.setRequestMethod("POST");
-        // 设置通用的请求属性
-        connection.setRequestProperty("Content-Type", contentType);
-        connection.setRequestProperty("Connection", "Keep-Alive");
-        connection.setUseCaches(false);
-        connection.setDoOutput(true);
-        connection.setDoInput(true);
-
-        // 得到请求的输出流对象
-        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
-        out.write(params.getBytes(encoding));
-        out.flush();
-        out.close();
-
-        // 建立实际的连接
-        connection.connect();
-        // 获取所有响应头字段
-        Map<String, List<String>> headers = connection.getHeaderFields();
-        // 遍历所有的响应头字段
-        for (String key : headers.keySet()) {
-            System.err.println(key + "--->" + headers.get(key));
-        }
-        // 定义 BufferedReader输入流来读取URL的响应
-        BufferedReader in = null;
-        in = new BufferedReader(
-                new InputStreamReader(connection.getInputStream(), encoding));
-        String result = "";
-        String getLine;
-        while ((getLine = in.readLine()) != null) {
-            result += getLine;
-        }
-        in.close();
-        System.err.println("result:" + result);
-        return result;
-    }
-
-
-}