| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- 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 java.net.URLEncoder;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * 身份证识别
- */
- @Slf4j
- public class Idcard {
- private Idcard() {
- throw new IllegalStateException("Idcard class");
- }
- /**
- * 重要提示代码中所需工具类
- * 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 = "https://aip.baidubce.com/rest/2.0/ocr/v1/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 = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
- try {
- String imgParam = URLEncoder.encode(image, "UTF-8");
- String param = "idCardSide=" + 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 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();
- }
- }
- }
|