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> getIdCard(String imgs) { //// 从redis中获取信息,redis保存时间是20天。 String accessToken = AuthService.getAuth(); Map 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 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(); } } }