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