Idcard.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 java.net.URLEncoder;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. /**
  12. * 身份证识别
  13. */
  14. @Slf4j
  15. public class Idcard {
  16. private Idcard() {
  17. throw new IllegalStateException("Idcard class");
  18. }
  19. /**
  20. * 重要提示代码中所需工具类
  21. * FileUtil,Base64Util,HttpUtil,GsonUtils请从
  22. * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
  23. * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
  24. * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
  25. * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
  26. * 下载
  27. */
  28. public static HttpResult<Map<String, Object>> getIdCard(String imgs) {
  29. //// 从redis中获取信息,redis保存时间是20天。
  30. String accessToken = AuthService.getAuth();
  31. Map<String, Object> ma = new HashMap<>();
  32. // 请求url
  33. String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
  34. try {
  35. String imgParam = URLEncoder.encode(imgs, "UTF-8");
  36. String param = "id_card_side=" + "front" + "&image=" + imgParam + "&detect_photo=true";
  37. String result = HttpUtil.post(url, accessToken, param);
  38. JSONObject ja = JSON.parseObject(result);
  39. log.info(ja.toString());
  40. String wordsResult = ja.getString("wordsResult");
  41. CustomerInfoVO customerInfo = new CustomerInfoVO();
  42. JSONObject jb = JSON.parseObject(wordsResult);
  43. customerInfo.setName(JSON.parseObject(jb.getString("姓名")).getString("words"));
  44. customerInfo.setIdentifyNumber(JSON.parseObject(jb.getString("公民身份号码")).getString("words"));
  45. customerInfo.setAddr(JSON.parseObject(jb.getString("住址")).getString("words"));
  46. customerInfo.setIdentifyType("01");
  47. customerInfo.setIdentifyValidDate(JSON.parseObject(jb.getString("出生")).getString("words"));
  48. customerInfo.setIdentifyIssuedCom(JSON.parseObject(jb.getString("性别")).getString("words"));
  49. ma.put("customerInfo", customerInfo);
  50. return HttpResult.ok("200", ma);
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. }
  54. return HttpResult.ok("201", ma);
  55. }
  56. public static JSONObject request(String image, String idCardSide) {
  57. //// 从redis中获取信息,redis保存时间是20天。
  58. String accessToken = AuthService.getAuth();
  59. // 请求url
  60. String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
  61. try {
  62. String imgParam = URLEncoder.encode(image, "UTF-8");
  63. String param = "idCardSide=" + idCardSide + "&image=" + imgParam + "&detect_photo=true";
  64. String result = HttpUtil.post(url, accessToken, param);
  65. JSONObject ja = JSON.parseObject(result);
  66. log.info(ja.toString());
  67. String wordsResult = ja.getString("words_result");
  68. return JSON.parseObject(wordsResult);
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. }
  72. return new JSONObject();
  73. }
  74. /**
  75. * 识别身份证正面
  76. *
  77. * @param images
  78. * @return
  79. */
  80. public static void getIdCardFront(String images, CustomerInfoVO customerInfo) {
  81. if (StringUtils.isEmpty(images)) {
  82. return;
  83. }
  84. try {
  85. JSONObject jb = request(images, "front");
  86. customerInfo.setName(JSON.parseObject(jb.getString("姓名")).getString("words"));
  87. customerInfo.setIdentifyNumber(JSON.parseObject(jb.getString("公民身份号码")).getString("words"));
  88. customerInfo.setAddr(JSON.parseObject(jb.getString("住址")).getString("words"));
  89. customerInfo.setIdentifyType("01");
  90. customerInfo.setIdentifyIssuedCom(JSON.parseObject(jb.getString("性别")).getString("words"));
  91. } catch (Exception e) {
  92. e.printStackTrace();
  93. }
  94. }
  95. public static void getIdCardBack(String images, CustomerInfoVO customerInfo) {
  96. if (StringUtils.isEmpty(images)) {
  97. return;
  98. }
  99. try {
  100. JSONObject jb = request(images, "back");
  101. customerInfo.setIdentifyValidDate(JSON.parseObject(jb.getString("签发日期")).getString("words"));
  102. customerInfo.setIdentifyValidEndDate(JSON.parseObject(jb.getString("失效日期")).getString("words"));
  103. } catch (Exception e) {
  104. e.printStackTrace();
  105. }
  106. }
  107. }