|
@@ -0,0 +1,86 @@
|
|
|
+package com.citu.framework.baiduaip.core;
|
|
|
+
|
|
|
+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.common.util.date.DateUtils;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.util.HashMap;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 百度OCR识别客户端
|
|
|
+ *
|
|
|
+ * @author rayson
|
|
|
+ **/
|
|
|
+@Slf4j
|
|
|
+public class AipOcrClient {
|
|
|
+
|
|
|
+ private final BaiduAipProperties properties;
|
|
|
+ private final AipOcr client;
|
|
|
+
|
|
|
+ public AipOcrClient(BaiduAipProperties properties) {
|
|
|
+ this.properties = properties;
|
|
|
+ this.client = new AipOcr(properties.getAppId(), properties.getApiKey(), properties.getSecretKey());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据营业执照图片解析识别营业执照
|
|
|
+ *
|
|
|
+ * @param imageUrl 图片地址
|
|
|
+ * @return BusinessLicenseOcr 营业执照对象
|
|
|
+ **/
|
|
|
+ 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) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取words_result字段中的属性值
|
|
|
+ *
|
|
|
+ * @param key 属性名
|
|
|
+ * @param wordsResult words_result字段
|
|
|
+ **/
|
|
|
+ private String getWordsResultAttribute(String key, JSONObject wordsResult) {
|
|
|
+ return wordsResult.getJSONObject(key).getString("words");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|