|
@@ -11,6 +11,7 @@ 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.current.global.BusinessException;
|
|
|
import com.pj.project.ocr.bo.OcrResult;
|
|
|
import com.pj.project.ocr.bo.WordsResult;
|
|
|
import com.pj.project4sp.uploadfile.UploadUtil;
|
|
@@ -20,7 +21,9 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.File;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
import java.net.URLEncoder;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.regex.Pattern;
|
|
@@ -38,7 +41,35 @@ public class OcrService {
|
|
|
@Resource
|
|
|
MyConfig myConfig;
|
|
|
|
|
|
- public OcrResult ocrIdCard(String idCardPath) throws Exception{
|
|
|
+ /**
|
|
|
+ * 识别边民证或身份证
|
|
|
+ * */
|
|
|
+ public OcrResult ocrIdOrIc(String cardPath){
|
|
|
+ List<String> wordsList = ocrImage(cardPath);
|
|
|
+ OcrResult ocrResult = new OcrResult();
|
|
|
+ if(wordsList.contains("公民身份号码")){
|
|
|
+ String words = wordsList.stream().map(String::valueOf).collect(Collectors.joining(""));
|
|
|
+ String name = getIdName(words);
|
|
|
+ String IdCard = getIdCard(words);
|
|
|
+ ocrResult.setName(name).setIdCard(IdCard).setType(1);
|
|
|
+ }else if(wordsList.contains("本证有效期")){
|
|
|
+ String words = sortIcWords(wordsList);
|
|
|
+ String icNo = getIcNo(words);
|
|
|
+ String name = getIcName(words);
|
|
|
+ String idCard = getIcIdCard(words);
|
|
|
+ String startTime = getIcStartTime(words);
|
|
|
+ String endTime = getIcEndTime(words);
|
|
|
+ ocrResult.setIcNo(icNo).setName(name).setIdCard(idCard).setStartTime(startTime).setEndTime(endTime).setType(2);
|
|
|
+ }else{
|
|
|
+ throw new BusinessException("识别身份信息失败,请检查证件是否上传正确");
|
|
|
+ }
|
|
|
+ return ocrResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 识别身份证
|
|
|
+ * */
|
|
|
+ public OcrResult ocrIdCard(String idCardPath){
|
|
|
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";
|
|
@@ -49,8 +80,10 @@ public class OcrService {
|
|
|
return ocrResult;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- public OcrResult ocrIcCard(String icCardPath) throws Exception{
|
|
|
+ /**
|
|
|
+ * 识别边民证
|
|
|
+ * */
|
|
|
+ public OcrResult ocrIcCard(String icCardPath){
|
|
|
List<String> wordsList = ocrImage(icCardPath);
|
|
|
String words = sortIcWords(wordsList);
|
|
|
//String words = "0024745姓名:黄恩华性别:男身份证号码:450681197409172411本证有效期自2021年01月01日起至2022年12月31日止发证单位:东兴市商务和口岸管理局";
|
|
@@ -64,7 +97,7 @@ public class OcrService {
|
|
|
return ocrResult;
|
|
|
}
|
|
|
|
|
|
- public List<String> ocrImage(String icCardPath) throws Exception{
|
|
|
+ public List<String> ocrImage(String icCardPath){
|
|
|
//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;
|
|
@@ -79,7 +112,12 @@ public class OcrService {
|
|
|
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 params = null;
|
|
|
+ try {
|
|
|
+ params = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(base64Str, "UTF-8");
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ log.info("ocr识别,图片转UTF-8编码失败");
|
|
|
+ }
|
|
|
String accurateResp = HttpUtil.createPost(accurateUrl).header("Content-Type", "application/x-www-form-urlencoded").body(params)
|
|
|
.execute().body();
|
|
|
log.info("accurateResp:", accurateResp);
|
|
@@ -114,7 +152,9 @@ public class OcrService {
|
|
|
|
|
|
|
|
|
private String getIcNo(String words) {
|
|
|
- String icNo = StrUtil.sub(words, 0, 7);
|
|
|
+ int noBeginIndex = StrUtil.indexOf(words, "00", 0, false);
|
|
|
+ String icNo = StrUtil.sub(words, noBeginIndex, noBeginIndex+7);
|
|
|
+ //String icNo = StrUtil.sub(words, 0, 7);
|
|
|
String regFormat = "00[\\d]{5}";
|
|
|
boolean matches = Pattern.matches(regFormat, icNo);
|
|
|
if(!matches){
|