|
@@ -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;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|