|
@@ -4,6 +4,7 @@ import cn.hutool.http.HttpUtil;
|
|
|
import com.baidu.aip.ocr.AipOcr;
|
|
|
import com.citu.framework.baiduaip.config.BaiduAipProperties;
|
|
|
import com.citu.framework.baiduaip.core.ocr.BusinessLicenseOcr;
|
|
|
+import com.citu.framework.baiduaip.core.ocr.IDCardInfo;
|
|
|
import com.citu.framework.common.util.date.DateUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.json.JSONObject;
|
|
@@ -27,6 +28,15 @@ public class AipOcrClient {
|
|
|
this.client = new AipOcr(properties.getAppId(), properties.getApiKey(), properties.getSecretKey());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取words_result字段中的属性值
|
|
|
+ *
|
|
|
+ * @param key 属性名
|
|
|
+ * @param wordsResult words_result字段
|
|
|
+ **/
|
|
|
+ private String getWordsResultAttribute(String key, JSONObject wordsResult) {
|
|
|
+ return wordsResult.getJSONObject(key).getString("words");
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 根据营业执照图片解析识别营业执照
|
|
@@ -36,50 +46,92 @@ public class AipOcrClient {
|
|
|
**/
|
|
|
public BusinessLicenseOcr businessLicense(String imageUrl) {
|
|
|
try {
|
|
|
- byte[] image = HttpUtil.downloadBytes(imageUrl);
|
|
|
- JSONObject res = client.businessLicense(image, new HashMap<String, String>());
|
|
|
- log.info("营业执照识别: {}", res);
|
|
|
- // 获取words_result字段
|
|
|
- JSONObject wordsResult = res.getJSONObject("words_result");
|
|
|
- BusinessLicenseOcr businessLicenseOcr = new BusinessLicenseOcr();
|
|
|
- // 接口返回的就是对象里面中文
|
|
|
- businessLicenseOcr.setCode(getWordsResultAttribute("社会信用代码", wordsResult));
|
|
|
- businessLicenseOcr.setName(getWordsResultAttribute("单位名称", wordsResult));
|
|
|
- businessLicenseOcr.setType(getWordsResultAttribute("类型", wordsResult));
|
|
|
- businessLicenseOcr.setAddress(getWordsResultAttribute("地址", wordsResult));
|
|
|
- businessLicenseOcr.setRepresentative(getWordsResultAttribute("法人", wordsResult));
|
|
|
- LocalDate date = DateUtils.of(getWordsResultAttribute("成立日期", wordsResult), DateUtils.FORMAT_YEAR_MONTH_DAY_CHINESE);
|
|
|
- businessLicenseOcr.setEstablishmentTime(date.atStartOfDay());
|
|
|
- businessLicenseOcr.setRegisteredCapital(getWordsResultAttribute("注册资本", wordsResult));
|
|
|
- businessLicenseOcr.setApprovalTime(getWordsResultAttribute("核准日期", wordsResult));
|
|
|
-
|
|
|
- businessLicenseOcr.setRegistrationAuthority(getWordsResultAttribute("登记机关", wordsResult));
|
|
|
- businessLicenseOcr.setBusinessTerm(getWordsResultAttribute("有效期起始日期", wordsResult) + " 至 " + getWordsResultAttribute("有效期", wordsResult));
|
|
|
- businessLicenseOcr.setBusinessScope(getWordsResultAttribute("经营范围", wordsResult));
|
|
|
-
|
|
|
-
|
|
|
- // 其他属性
|
|
|
- getWordsResultAttribute("组成形式", wordsResult);
|
|
|
- getWordsResultAttribute("证件编号", wordsResult);
|
|
|
- getWordsResultAttribute("实收资本", wordsResult);
|
|
|
- getWordsResultAttribute("税务登记号", wordsResult);
|
|
|
-
|
|
|
-
|
|
|
- return businessLicenseOcr;
|
|
|
- }catch (Exception ex) {
|
|
|
+ byte[] image = HttpUtil.downloadBytes(imageUrl);
|
|
|
+ JSONObject res = client.businessLicense(image, new HashMap<String, String>());
|
|
|
+ log.info("营业执照识别: {}", res);
|
|
|
+ // 获取words_result字段
|
|
|
+ JSONObject wordsResult = res.getJSONObject("words_result");
|
|
|
+ BusinessLicenseOcr businessLicenseOcr = new BusinessLicenseOcr();
|
|
|
+ // 接口返回的就是对象里面中文
|
|
|
+ businessLicenseOcr.setCode(getWordsResultAttribute("社会信用代码", wordsResult));
|
|
|
+ businessLicenseOcr.setName(getWordsResultAttribute("单位名称", wordsResult));
|
|
|
+ businessLicenseOcr.setType(getWordsResultAttribute("类型", wordsResult));
|
|
|
+ businessLicenseOcr.setAddress(getWordsResultAttribute("地址", wordsResult));
|
|
|
+ businessLicenseOcr.setRepresentative(getWordsResultAttribute("法人", wordsResult));
|
|
|
+ LocalDate date = DateUtils.of(getWordsResultAttribute("成立日期", wordsResult), DateUtils.FORMAT_YEAR_MONTH_DAY_CHINESE);
|
|
|
+ businessLicenseOcr.setEstablishmentTime(date.atStartOfDay());
|
|
|
+ businessLicenseOcr.setRegisteredCapital(getWordsResultAttribute("注册资本", wordsResult));
|
|
|
+ businessLicenseOcr.setApprovalTime(getWordsResultAttribute("核准日期", wordsResult));
|
|
|
+
|
|
|
+ businessLicenseOcr.setRegistrationAuthority(getWordsResultAttribute("登记机关", wordsResult));
|
|
|
+ businessLicenseOcr.setBusinessTerm(getWordsResultAttribute("有效期起始日期", wordsResult) + " 至 " + getWordsResultAttribute("有效期", wordsResult));
|
|
|
+ businessLicenseOcr.setBusinessScope(getWordsResultAttribute("经营范围", wordsResult));
|
|
|
+
|
|
|
+
|
|
|
+ // 其他属性
|
|
|
+ getWordsResultAttribute("组成形式", wordsResult);
|
|
|
+ getWordsResultAttribute("证件编号", wordsResult);
|
|
|
+ getWordsResultAttribute("实收资本", wordsResult);
|
|
|
+ getWordsResultAttribute("税务登记号", wordsResult);
|
|
|
+
|
|
|
+
|
|
|
+ return businessLicenseOcr;
|
|
|
+ } catch (Exception ex) {
|
|
|
ex.printStackTrace();
|
|
|
- return null;
|
|
|
}
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取words_result字段中的属性值
|
|
|
+ * 根据身份证图片解析识别身份证信息
|
|
|
*
|
|
|
- * @param key 属性名
|
|
|
- * @param wordsResult words_result字段
|
|
|
+ * @param frontUrl 身份证含照片的一面
|
|
|
+ * @param backUrl 身份证带国徽的一面
|
|
|
+ * @return IDCardInfo 身份证对象
|
|
|
**/
|
|
|
- private String getWordsResultAttribute(String key, JSONObject wordsResult) {
|
|
|
- return wordsResult.getJSONObject(key).getString("words");
|
|
|
+ public IDCardInfo idcard(String frontUrl, String backUrl) {
|
|
|
+ try {
|
|
|
+ byte[] front = HttpUtil.downloadBytes(frontUrl);
|
|
|
+ byte[] back = HttpUtil.downloadBytes(backUrl);
|
|
|
+ JSONObject frontRes = client.idcard(front, "front", new HashMap<String, String>());
|
|
|
+ JSONObject backRes = client.idcard(back, "back", new HashMap<String, String>());
|
|
|
+ log.info("身份证识别 - 头像面: {} \r\n 国徽面", frontRes, backRes);
|
|
|
+ IDCardInfo idCardInfo = new IDCardInfo();
|
|
|
+ // image_status
|
|
|
+ // normal-识别正常
|
|
|
+ // reversed_side-身份证正反面颠倒
|
|
|
+ // non_idcard-上传的图片中不包含身份证
|
|
|
+ // blurred-身份证模糊
|
|
|
+ // other_type_card-其他类型证照
|
|
|
+ // over_exposure-身份证关键字段反光或过曝
|
|
|
+ // over_dark-身份证欠曝(亮度过低)
|
|
|
+ // unknown-未知状态
|
|
|
+ if ("normal".equals(frontRes.getString("image_status"))) {
|
|
|
+ // 获取words_result字段
|
|
|
+ JSONObject wordsResult = frontRes.getJSONObject("words_result");
|
|
|
+ idCardInfo.setName(getWordsResultAttribute("姓名", wordsResult));
|
|
|
+ idCardInfo.setGender(getWordsResultAttribute("性别", wordsResult));
|
|
|
+ idCardInfo.setBirthDate(DateUtils.of(getWordsResultAttribute("出生", wordsResult),
|
|
|
+ DateUtils.FORMAT_YEAR_MONTH_DAY_CHINESE));
|
|
|
+ idCardInfo.setAddress(getWordsResultAttribute("住址", wordsResult));
|
|
|
+ idCardInfo.setIdNumber(getWordsResultAttribute("公民身份号码", wordsResult));
|
|
|
+ idCardInfo.setEthnicity(getWordsResultAttribute("民族", wordsResult));
|
|
|
+
|
|
|
+ }
|
|
|
+ if ("normal".equals(backRes.getString("image_status"))) {
|
|
|
+ JSONObject wordsResult = backRes.getJSONObject("words_result");
|
|
|
+ idCardInfo.setIssuingAuthority(getWordsResultAttribute("签发机关", wordsResult));
|
|
|
+ idCardInfo.setIssueDate(DateUtils.of(getWordsResultAttribute("签发日期", wordsResult),
|
|
|
+ DateUtils.FORMAT_YEAR_MONTH_DAY_CHINESE));
|
|
|
+ idCardInfo.setExpiryDate(DateUtils.of(getWordsResultAttribute("失效日期", wordsResult),
|
|
|
+ DateUtils.FORMAT_YEAR_MONTH_DAY_CHINESE));
|
|
|
+
|
|
|
+ }
|
|
|
+ return idCardInfo;
|
|
|
+ } catch (Exception ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
|