| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- package com.ydtech.utils.baidu;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.ydtech.core.page.HttpResult;
- import com.ydtech.modules.ins.model.CustomerInfoVO;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.core.env.Environment;
- import org.springframework.stereotype.Component;
- import javax.annotation.PostConstruct;
- import java.net.URLEncoder;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * 身份证识别
- */
- @Slf4j
- @Component
- public class Idcard {
- @Autowired
- private Environment environment;
- @Autowired
- private static Environment staticEnvironment;
- @PostConstruct
- public void init() {
- staticEnvironment = environment;
- }
- /**
- * 重要提示代码中所需工具类
- * FileUtil,Base64Util,HttpUtil,GsonUtils请从
- * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
- * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
- * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
- * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
- * 下载
- */
- public static HttpResult<Map<String, Object>> getIdCard(String imgs) {
- //// 从redis中获取信息,redis保存时间是20天。
- String accessToken = AuthService.getAuth();
- Map<String, Object> ma = new HashMap<>();
- // 请求url
- String url = staticEnvironment.getProperty("baidu.idcard");
- try {
- String imgParam = URLEncoder.encode(imgs, "UTF-8");
- String param = "id_card_side=" + "front" + "&image=" + imgParam + "&detect_photo=true";
- String result = HttpUtil.post(url, accessToken, param);
- JSONObject ja = JSON.parseObject(result);
- log.info(ja.toString());
- String wordsResult = ja.getString("wordsResult");
- CustomerInfoVO customerInfo = new CustomerInfoVO();
- JSONObject jb = JSON.parseObject(wordsResult);
- customerInfo.setName(JSON.parseObject(jb.getString("姓名")).getString("words"));
- customerInfo.setIdentifyNumber(JSON.parseObject(jb.getString("公民身份号码")).getString("words"));
- customerInfo.setAddr(JSON.parseObject(jb.getString("住址")).getString("words"));
- customerInfo.setIdentifyType("01");
- customerInfo.setIdentifyValidDate(JSON.parseObject(jb.getString("出生")).getString("words"));
- customerInfo.setIdentifyIssuedCom(JSON.parseObject(jb.getString("性别")).getString("words"));
- ma.put("customerInfo", customerInfo);
- return HttpResult.ok("200", ma);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return HttpResult.ok("201", ma);
- }
- public static JSONObject request(String image, String idCardSide) {
- //// 从redis中获取信息,redis保存时间是20天。
- String accessToken = AuthService.getAuth();
- // 请求url
- String url = staticEnvironment.getProperty("baidu.idcard");
- try {
- String imgParam = URLEncoder.encode(image, "UTF-8");
- String param = "id_card_side=" + idCardSide + "&image=" + imgParam + "&detect_photo=true";
- String result = HttpUtil.post(url, accessToken, param);
- JSONObject ja = JSON.parseObject(result);
- log.info(ja.toString());
- String wordsResult = ja.getString("words_result");
- return JSON.parseObject(wordsResult);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return new JSONObject();
- }
- /**
- * 识别身份证正面
- *
- * @param images
- * @return
- */
- public static void getIdCardFront(String images, CustomerInfoVO customerInfo) {
- if (StringUtils.isEmpty(images)) {
- return;
- }
- try {
- JSONObject jb = request(images, "front");
- customerInfo.setName(JSON.parseObject(jb.getString("姓名")).getString("words"));
- customerInfo.setIdentifyNumber(JSON.parseObject(jb.getString("公民身份号码")).getString("words"));
- customerInfo.setAddr(JSON.parseObject(jb.getString("住址")).getString("words"));
- customerInfo.setIdentifyType("01");
- customerInfo.setIdentifyIssuedCom(JSON.parseObject(jb.getString("性别")).getString("words"));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void conversionFront(CustomerInfoVO customerInfo, CustomerInfoVO customerInfoNew) {
- customerInfo.setName(customerInfoNew.getName());
- customerInfo.setIdentifyNumber(customerInfoNew.getIdentifyNumber());
- customerInfo.setAddr(customerInfoNew.getAddr());
- customerInfo.setIdentifyType(customerInfoNew.getIdentifyType());
- customerInfo.setIdentifyIssuedCom(customerInfoNew.getIdentifyIssuedCom());
- }
- public static void getIdCardBack(String images, CustomerInfoVO customerInfo) {
- if (StringUtils.isEmpty(images)) {
- return;
- }
- try {
- JSONObject jb = request(images, "back");
- customerInfo.setIdentifyValidDate(JSON.parseObject(jb.getString("签发日期")).getString("words"));
- customerInfo.setIdentifyValidEndDate(JSON.parseObject(jb.getString("失效日期")).getString("words"));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void conversionBack(CustomerInfoVO customerInfo, CustomerInfoVO customerInfoNew) {
- customerInfo.setIdentifyValidDate(customerInfoNew.getName());
- customerInfo.setIdentifyValidEndDate(customerInfoNew.getIdentifyValidEndDate());
- }
- }
|