赞
踩
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>${version}</version>
</dependency>
@ApiOperation("用户实名认证") @PostMapping("/authentication") public ResObject authentication(@RequestBody DdcRealNameUser ddcRealNameUser)throws Exception { Map maps = new HashMap(); ResObject resObject = null; String idcardIdentificate = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard"; // 本地图片路径 String path = ddcRealNameUser.getRealnameauthfront(); String filePath = path;//#####本地文件路径##### byte[] imgData = FileUtil.readFileByBytes(filePath); String imgStr = Base64Util.encode(imgData); // 识别身份证正面id_card_side=front;识别身份证背面id_card_side=back; String params = "id_card_side=front&" + URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(imgStr, "UTF-8"); /** * 线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。 */ String accessToken = OcrUtil.getAuth();//#####调用鉴权接口获取的token##### String results = HttpUtil.post(idcardIdentificate, accessToken, params); Map<String, String> map = new HashMap<>(); JSONObject jsonObject = JSONObject.parseObject(results); JSONObject words_result = jsonObject.getJSONObject("words_result"); if (words_result == null || words_result.isEmpty()) { // throw new MyException("请提供身份证"+side+"图片"); } for (String key : words_result.keySet()) { JSONObject result = words_result.getJSONObject(key); String info = result.getString("words"); switch (key) { case "姓名": map.put("name", info); break; case "性别": map.put("sex", info); break; case "民族": map.put("nation", info); break; case "出生": map.put("birthday", info); break; case "住址": map.put("address", info); break; case "公民身份号码": map.put("idNumber", info); break; case "签发机关": map.put("issuedOrganization", info); break; case "签发日期": map.put("issuedAt", info); break; case "失效日期": map.put("expiredAt", info); break; } int count = 1; for (int i = 0; i < results.length(); i++) { System.out.print(results.charAt(i)); count++; if (count % 120 == 0) { System.out.print("\n"); } } } resObject = new ResObject(0, "success", map); return resObject; }```
// Access_Token获取 private static final String ACCESS_TOKEN_HOST = "https://aip.baidubce.com/oauth/2.0/token?"; // 身份证识别请求URL private static final String OCR_HOST = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?"; // apiKey,secretKey private static final String API_KEY =""; private static final String SECRET_KEY = ""; public static String getAuth() { // 官网获取的 API Key 更新为你注册的 String clientId = "";//百度云应用的AK // 官网获取的 Secret Key 更新为你注册的 String clientSecret = "";//百度云应用的SK return getAuth(clientId, clientSecret); } public static String getAuth(String ak, String sk) { // 获取token地址 String authHost = "https://aip.baidubce.com/oauth/2.0/token?"; String getAccessTokenUrl = authHost // 1. grant_type为固定参数 + "grant_type=client_credentials" // 2. 官网获取的 API Key + "&client_id=" + ak // 3. 官网获取的 Secret Key + "&client_secret=" + sk; try { URL realUrl = new URL(getAccessTokenUrl); // 打开和URL之间的连接 HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection(); connection.setRequestMethod("GET"); connection.connect(); // 获取所有响应头字段 Map<String, List<String>> map = connection.getHeaderFields(); // 遍历所有的响应头字段 for (String key : map.keySet()) { System.err.println(key + "--->" + map.get(key)); } // 定义 BufferedReader输入流来读取URL的响应 BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String result = ""; String line; while ((line = in.readLine()) != null) { result += line; } /** * 返回结果示例 */ System.err.println("result:" + result); org.json.JSONObject jsonObject = new org.json.JSONObject(result); String access_token = jsonObject.getString("access_token"); return access_token; } catch (Exception e) { System.err.printf("获取token失败!"); e.printStackTrace(System.err); } return null; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。