|
@@ -0,0 +1,148 @@
|
|
|
+package cn.hmsoft.ai.idcard.baidu;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.util.HashMap;
|
|
|
+
|
|
|
+import com.baidu.aip.ocr.AipOcr;
|
|
|
+import com.google.gson.JsonObject;
|
|
|
+
|
|
|
+import cn.hmsoft.ai.idcard.IdCard;
|
|
|
+import cn.hmsoft.ai.idcard.IdCardBack;
|
|
|
+import cn.hmsoft.ai.idcard.IdCardOcr;
|
|
|
+import cn.hmsoft.ai.util.AiParamUtil;
|
|
|
+import cn.hmsoft.helper.CollectionHelper;
|
|
|
+import cn.hmsoft.helper.LocalDateHelper;
|
|
|
+
|
|
|
+/**************************
|
|
|
+ * 使用百度api实现身份证识别
|
|
|
+ *
|
|
|
+ * @author zxq
|
|
|
+ * @create 2019-08-23 16:52:28
|
|
|
+ * @version 2.0.0
|
|
|
+ * @email: revisit@126.com
|
|
|
+ * @Company: www.hmsoft.cn
|
|
|
+ *
|
|
|
+ * { "log_id": 1140563900576588182, "words_result": { "姓名": { "words":
|
|
|
+ * "张小庆", "location": { "top": 435, "left": 843, "width": 460,
|
|
|
+ * "height": 159 } }, "民族": { "words": "汉", "location": { "top": 750,
|
|
|
+ * "left": 1553, "width": 92, "height": 112 } }, "住址": { "words":
|
|
|
+ * "杭州市上城区大学路新村6幢14号601室", "location": { "top": 1298, "left": 853,
|
|
|
+ * "width": 1408, "height": 291 } }, "公民身份号码": { "words":
|
|
|
+ * "352622198007091515", "location": { "top": 1974, "left": 1408,
|
|
|
+ * "width": 1812, "height": 126 } }, "出生": { "words": "19800709",
|
|
|
+ * "location": { "top": 1015, "left": 852, "width": 1133, "height":
|
|
|
+ * 119 } }, "性别": { "words": "男", "location": { "top": 754, "left":
|
|
|
+ * 864, "width": 92, "height": 117 } } }, "words_result_num": 6,
|
|
|
+ * "image_status": "normal", "direction": 0 }
|
|
|
+ */
|
|
|
+public class BaiduIdCardOcr extends IdCardOcr {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IdCard ocrIdCardByFile(File idcard_image_file) {
|
|
|
+ // 每次访问一次实例,防止token串的问题
|
|
|
+ String appId = AiParamUtil.ParamMap.get(AiParamUtil.ParamName_BaiduOcrAppId).trim();
|
|
|
+ String appKey = AiParamUtil.ParamMap.get(AiParamUtil.ParamName_BaiduOcrAppKey).trim();
|
|
|
+ String secretKey = AiParamUtil.ParamMap.get(AiParamUtil.ParamName_BaiduOcrSecretKey).trim();
|
|
|
+ // System.out.println(appId);
|
|
|
+ // System.out.println(appKey);
|
|
|
+ // System.out.println(secretKey);
|
|
|
+ AipOcr client = new AipOcr(appId, appKey, secretKey);
|
|
|
+ // 设置参数
|
|
|
+ // 网络超时,默认5秒
|
|
|
+ client.setConnectionTimeoutInMillis(CollectionHelper.getParamValue(AiParamUtil.ParamMap,
|
|
|
+ AiParamUtil.ParamName_BaiduOcrConnectionTimeoutInMillis, 5) * 1000);
|
|
|
+
|
|
|
+ // 传入可选参数调用接口
|
|
|
+ HashMap<String, String> options = new HashMap<String, String>();
|
|
|
+ options.put("detect_direction", "true");
|
|
|
+ options.put("detect_risk", "true");
|
|
|
+ // detect_photo 图片信息
|
|
|
+ options.put("detect_photo", "true");
|
|
|
+ String idCardSide = "front";
|
|
|
+
|
|
|
+ // 参数为本地路径
|
|
|
+ JsonObject res = client.idcard(idcard_image_file.getPath(), idCardSide, options);
|
|
|
+ IdCard card = new IdCard();
|
|
|
+ try {
|
|
|
+ card.setDirection(res.get("direction").getAsInt());
|
|
|
+ if (card.getDirection() == 1) {
|
|
|
+ card.setDirection(90);
|
|
|
+ } else if (card.getDirection() == 2) {
|
|
|
+ card.setDirection(180);
|
|
|
+ } else if (card.getDirection() == 3) {
|
|
|
+ card.setDirection(270);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ card.setPhoto(res.get("photo").getAsString());
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ if (res.get("image_status").getAsString().equals("normal")) {
|
|
|
+ // 成功识别
|
|
|
+ card.setPerson_name(
|
|
|
+ res.get("words_result").getAsJsonObject().get("姓名").getAsJsonObject().get("words").getAsString());
|
|
|
+ card.setPerson_nation(
|
|
|
+ res.get("words_result").getAsJsonObject().get("民族").getAsJsonObject().get("words").getAsString());
|
|
|
+ card.setPerson_addr(
|
|
|
+ res.get("words_result").getAsJsonObject().get("住址").getAsJsonObject().get("words").getAsString());
|
|
|
+ card.setPerson_code(res.get("words_result").getAsJsonObject().get("公民身份号码").getAsJsonObject().get("words")
|
|
|
+ .getAsString());
|
|
|
+ card.setBirthday(LocalDateHelper.parseDateNoSplit(
|
|
|
+ res.get("words_result").getAsJsonObject().get("出生").getAsJsonObject().get("words").getAsString()));
|
|
|
+ card.setPerson_sex(
|
|
|
+ res.get("words_result").getAsJsonObject().get("性别").getAsJsonObject().get("words").getAsString());
|
|
|
+ card.setOcr_success(true);
|
|
|
+ } else
|
|
|
+ card.setError_msg(res.get("image_status").getAsString());
|
|
|
+ return card;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Description: 身份证反面识别
|
|
|
+ * @date 2023-11-09 04:13:27
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public IdCardBack ocrIdCardBackByFile(File idcard_image_file) {
|
|
|
+ String appId = AiParamUtil.ParamMap.get(AiParamUtil.ParamName_BaiduOcrAppId).trim();
|
|
|
+ String appKey = AiParamUtil.ParamMap.get(AiParamUtil.ParamName_BaiduOcrAppKey).trim();
|
|
|
+ String secretKey = AiParamUtil.ParamMap.get(AiParamUtil.ParamName_BaiduOcrSecretKey).trim();
|
|
|
+ AipOcr client = new AipOcr(appId, appKey, secretKey);
|
|
|
+ client.setConnectionTimeoutInMillis(CollectionHelper.getParamValue(AiParamUtil.ParamMap,
|
|
|
+ AiParamUtil.ParamName_BaiduOcrConnectionTimeoutInMillis, 5) * 1000);
|
|
|
+ HashMap<String, String> options = new HashMap<String, String>();
|
|
|
+ options.put("detect_direction", "true");
|
|
|
+ options.put("detect_risk", "true");
|
|
|
+ // detect_photo 图片信息
|
|
|
+ options.put("detect_photo", "true");
|
|
|
+ String idCardSide = "back";
|
|
|
+ // 参数为本地路径
|
|
|
+ JsonObject res = client.idcard(idcard_image_file.getPath(), idCardSide, options);
|
|
|
+ IdCardBack back = new IdCardBack();
|
|
|
+ try {
|
|
|
+ back.setDirection(res.get("direction").getAsInt());
|
|
|
+ if (back.getDirection() == 1) {
|
|
|
+ back.setDirection(90);
|
|
|
+ } else if (back.getDirection() == 2) {
|
|
|
+ back.setDirection(180);
|
|
|
+ } else if (back.getDirection() == 3) {
|
|
|
+ back.setDirection(270);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ if (res.get("image_status").getAsString().equals("normal")) {
|
|
|
+ back.setSign_organization(res.get("words_result").getAsJsonObject().get("签发机关").getAsJsonObject().get("words").getAsString());
|
|
|
+ back.setIssue_date(res.get("words_result").getAsJsonObject().get("签发日期").getAsJsonObject().get("words").getAsString());
|
|
|
+ back.setExpire_date(res.get("words_result").getAsJsonObject().get("失效日期").getAsJsonObject().get("words").getAsString());
|
|
|
+ back.setOcr_success(true);
|
|
|
+ } else {
|
|
|
+ back.setError_msg(res.get("image_status").getAsString());
|
|
|
+ }
|
|
|
+ return back;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|