Idcard.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package com.ydtech.utils.baidu;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.ydtech.core.page.HttpResult;
  5. import com.ydtech.modules.ins.model.CustomerInfoVO;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.apache.commons.lang3.StringUtils;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.core.env.Environment;
  10. import org.springframework.stereotype.Component;
  11. import javax.annotation.PostConstruct;
  12. import java.net.URLEncoder;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. /**
  16. * 身份证识别
  17. */
  18. @Slf4j
  19. @Component
  20. public class Idcard {
  21. @Autowired
  22. private Environment environment;
  23. @Autowired
  24. private static Environment staticEnvironment;
  25. @PostConstruct
  26. public void init() {
  27. staticEnvironment = environment;
  28. }
  29. /**
  30. * 重要提示代码中所需工具类
  31. * FileUtil,Base64Util,HttpUtil,GsonUtils请从
  32. * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
  33. * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
  34. * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
  35. * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
  36. * 下载
  37. */
  38. public static HttpResult<Map<String, Object>> getIdCard(String imgs) {
  39. //// 从redis中获取信息,redis保存时间是20天。
  40. String accessToken = AuthService.getAuth();
  41. Map<String, Object> ma = new HashMap<>();
  42. // 请求url
  43. String url = staticEnvironment.getProperty("baidu.idcard");
  44. try {
  45. String imgParam = URLEncoder.encode(imgs, "UTF-8");
  46. String param = "id_card_side=" + "front" + "&image=" + imgParam + "&detect_photo=true";
  47. String result = HttpUtil.post(url, accessToken, param);
  48. JSONObject ja = JSON.parseObject(result);
  49. log.info(ja.toString());
  50. String wordsResult = ja.getString("wordsResult");
  51. CustomerInfoVO customerInfo = new CustomerInfoVO();
  52. JSONObject jb = JSON.parseObject(wordsResult);
  53. customerInfo.setName(JSON.parseObject(jb.getString("姓名")).getString("words"));
  54. customerInfo.setIdentifyNumber(JSON.parseObject(jb.getString("公民身份号码")).getString("words"));
  55. customerInfo.setAddr(JSON.parseObject(jb.getString("住址")).getString("words"));
  56. customerInfo.setIdentifyType("01");
  57. customerInfo.setIdentifyValidDate(JSON.parseObject(jb.getString("出生")).getString("words"));
  58. customerInfo.setIdentifyIssuedCom(JSON.parseObject(jb.getString("性别")).getString("words"));
  59. ma.put("customerInfo", customerInfo);
  60. return HttpResult.ok("200", ma);
  61. } catch (Exception e) {
  62. e.printStackTrace();
  63. }
  64. return HttpResult.ok("201", ma);
  65. }
  66. public static JSONObject request(String image, String idCardSide) {
  67. //// 从redis中获取信息,redis保存时间是20天。
  68. String accessToken = AuthService.getAuth();
  69. // 请求url
  70. String url = staticEnvironment.getProperty("baidu.idcard");
  71. try {
  72. String imgParam = URLEncoder.encode(image, "UTF-8");
  73. String param = "id_card_side=" + idCardSide + "&image=" + imgParam + "&detect_photo=true";
  74. String result = HttpUtil.post(url, accessToken, param);
  75. JSONObject ja = JSON.parseObject(result);
  76. log.info(ja.toString());
  77. String wordsResult = ja.getString("words_result");
  78. return JSON.parseObject(wordsResult);
  79. } catch (Exception e) {
  80. e.printStackTrace();
  81. }
  82. return new JSONObject();
  83. }
  84. /**
  85. * 识别身份证正面
  86. *
  87. * @param images
  88. * @return
  89. */
  90. public static void getIdCardFront(String images, CustomerInfoVO customerInfo) {
  91. if (StringUtils.isEmpty(images)) {
  92. return;
  93. }
  94. try {
  95. JSONObject jb = request(images, "front");
  96. customerInfo.setName(JSON.parseObject(jb.getString("姓名")).getString("words"));
  97. customerInfo.setIdentifyNumber(JSON.parseObject(jb.getString("公民身份号码")).getString("words"));
  98. customerInfo.setAddr(JSON.parseObject(jb.getString("住址")).getString("words"));
  99. customerInfo.setIdentifyType("01");
  100. customerInfo.setIdentifyIssuedCom(JSON.parseObject(jb.getString("性别")).getString("words"));
  101. } catch (Exception e) {
  102. e.printStackTrace();
  103. }
  104. }
  105. public static void conversionFront(CustomerInfoVO customerInfo, CustomerInfoVO customerInfoNew) {
  106. customerInfo.setName(customerInfoNew.getName());
  107. customerInfo.setIdentifyNumber(customerInfoNew.getIdentifyNumber());
  108. customerInfo.setAddr(customerInfoNew.getAddr());
  109. customerInfo.setIdentifyType(customerInfoNew.getIdentifyType());
  110. customerInfo.setIdentifyIssuedCom(customerInfoNew.getIdentifyIssuedCom());
  111. }
  112. public static void getIdCardBack(String images, CustomerInfoVO customerInfo) {
  113. if (StringUtils.isEmpty(images)) {
  114. return;
  115. }
  116. try {
  117. JSONObject jb = request(images, "back");
  118. customerInfo.setIdentifyValidDate(JSON.parseObject(jb.getString("签发日期")).getString("words"));
  119. customerInfo.setIdentifyValidEndDate(JSON.parseObject(jb.getString("失效日期")).getString("words"));
  120. } catch (Exception e) {
  121. e.printStackTrace();
  122. }
  123. }
  124. public static void conversionBack(CustomerInfoVO customerInfo, CustomerInfoVO customerInfoNew) {
  125. customerInfo.setIdentifyValidDate(customerInfoNew.getName());
  126. customerInfo.setIdentifyValidEndDate(customerInfoNew.getIdentifyValidEndDate());
  127. }
  128. }