lzm 3 سال پیش
والد
کامیت
a087fbac53

+ 18 - 0
business-system/filing-system/src/main/java/com/pj/api/WxController.java

@@ -5,6 +5,8 @@ import cn.dev33.satoken.annotation.SaCheckPermission;
 import cn.dev33.satoken.stp.StpUtil;
 import cn.hutool.core.util.StrUtil;
 import com.pj.current.satoken.StpUserUtil;
+import com.pj.project.ocr.OcrService;
+import com.pj.project.ocr.bo.OcrResult;
 import com.pj.project.re_mini_user_car_filing.ReMiniUserCarFiling;
 import com.pj.project.re_mini_user_car_filing.ReMiniUserCarFilingService;
 import com.pj.project.re_mini_user_person_filing.ReMiniUserPersonFiling;
@@ -24,6 +26,7 @@ import com.pj.utils.so.SoMap;
 import lombok.extern.slf4j.Slf4j;
 import org.aspectj.weaver.loadtime.Aj;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
 import java.util.List;
@@ -51,6 +54,9 @@ public class WxController {
     @Resource
     private SpAdminService spAdminService;
 
+    @Resource
+    private OcrService ocrService;
+
 
     @GetMapping(value = "getRedirectUrl")
     public AjaxJson getRedirectUrl(String path, @RequestParam(required = false) String state) {
@@ -194,4 +200,16 @@ public class WxController {
     public AjaxJson bind(String openid, String key, String password, Integer type, String name) {
         return spAdminService.bindUser(openid, key, password, type, name);
     }
+
+    @RequestMapping("ocrIcCard")
+    public AjaxJson uploadAndOcrIcCard(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;
+    }
 }

+ 22 - 0
sp-core/src/main/java/com/pj/current/config/OcrConfig.java

@@ -0,0 +1,22 @@
+package com.pj.current.config;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+/**
+ * @Auther: lzm
+
+ */
+@ConfigurationProperties(prefix = "ocr-config")
+@Component
+@Data
+public class OcrConfig {
+
+    private String appId;
+    private String appKey;
+    private String secretKey;
+    private String  accessTokenUrl;
+    private String accurateUrl;
+
+}

+ 155 - 0
sp-core/src/main/java/com/pj/project/ocr/OcrService.java

@@ -0,0 +1,155 @@
+package com.pj.project.ocr;
+
+import cn.hutool.core.codec.Base64;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.http.HttpUtil;
+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.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.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * @Auther: lzm
+ */
+@Service
+@Transactional(rollbackFor = Exception.class)
+@Slf4j
+public class OcrService {
+    @Resource
+    OcrConfig ocrConfig;
+    @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);
+
+        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);
+
+        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;
+
+    }
+
+    public OcrResult ocrIcCard(String icCardPath) throws Exception{
+        String words = this.ocrImage(icCardPath);
+        String icId = getIcId(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);
+        return ocrResult;
+    }
+
+    public 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;
+        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);
+        JSONObject tokenResult = JSONUtil.parseObj(tokenResp);
+        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 accurateResp = HttpUtil.createPost(accurateUrl).header("Content-Type", "application/x-www-form-urlencoded").body(params)
+                .execute().body();
+        log.info("accurateResp:", accurateResp);
+        JSONObject resultJson = JSONUtil.parseObj(accurateResp);
+        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());
+        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(""));
+        return words;
+    }
+
+
+    private String getIcId(String words) {
+        String icId = StrUtil.sub(words, 0, 7);
+        return icId;
+    }
+    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);
+        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);
+        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);
+        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);
+        return endTime;
+    }
+
+
+
+}

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

@@ -0,0 +1,23 @@
+package com.pj.project.ocr.bo;
+
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+/**
+ * Created with IntelliJ IDEA.
+ *
+ * @Auther: lzm
+ * @Date: 2022/08/29/16:54
+ * @Description:
+ */
+@Data
+@Accessors(chain = true)
+public class OcrResult {
+
+    private String icId;
+    private String name;
+    private String idCard;
+    private String startTime;
+    private String endTime;
+
+}

+ 20 - 0
sp-core/src/main/java/com/pj/project/ocr/bo/WordsResult.java

@@ -0,0 +1,20 @@
+package com.pj.project.ocr.bo;
+
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+/**
+ * Created with IntelliJ IDEA.
+ *
+ * @Auther: lzm
+ * @Date: 2022/08/26/16:20
+ * @Description:
+ */
+@Data
+@Accessors(chain = true)
+public class WordsResult {
+
+    private String words;
+    private String location;
+
+}

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

@@ -0,0 +1,153 @@
+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);
+    }
+
+}

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

@@ -0,0 +1,59 @@
+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;
+    }
+
+
+}

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

@@ -131,6 +131,8 @@ public class TbPersonFiling extends Model<TbPersonFiling> implements Serializabl
      */
     private String icCard;
 
+    private String icCardUrl;
+
     @TableField(exist = false)
     private String deptName;
 

+ 7 - 1
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.22
         heart-port: 9999
@@ -74,3 +74,9 @@ wx:
 #    health-status-template: IEzVk2HCvctwXZzaGaElWQpRskolYlZSahTZXbJhuVM
     temperature-error-template: 3DBqZsC6x4IMmkzZNkSCh47slBUhuoyMlpC0wgW_9-w
     health-status-template: 3DBqZsC6x4IMmkzZNkSCh47slBUhuoyMlpC0wgW_9-w
+ocr-config:
+    app-id: 26598295
+    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