package com.bonus.core.aip; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import com.bonus.core.aip.AuthService; import com.bonus.core.aip.Base64Util; import com.gexin.fastjson.JSON; import com.gexin.fastjson.JSONObject; public class IDCardIdentification { public static void test1() { // 身份证识别url String idcardIdentificate = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard"; // 本地图片路径 String filePath = "C:\\Users\\Administrator\\Desktop\\正面.jpg"; try { 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 = AuthService.getAuth(); String result = HttpUtil.post(idcardIdentificate, accessToken, params); JSONObject obj = JSON.parseObject(result); String name = obj.getJSONObject("words_result").getJSONObject("姓名").getString("words"); String age = obj.getJSONObject("words_result").getJSONObject("姓名").getString("words"); String number = obj.getJSONObject("words_result").getJSONObject("公民身份号码").getString("words"); String gender = obj.getJSONObject("words_result").getJSONObject("性别").getString("words"); int leh = number.length(); String dates=""; if (leh == 18) { int se = Integer.valueOf(number.substring(leh - 1)) % 2; dates = number.substring(6, 10); SimpleDateFormat df = new SimpleDateFormat("yyyy"); String year=df.format(new Date()); int u=Integer.parseInt(year)-Integer.parseInt(dates); age = u+""; }else{ dates = number.substring(6, 8); age = ""+Integer.parseInt(dates); } System.out.println("name:"+name+",age:"+age+",number:"+number+",gender:"+gender); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } public static Map getIDCardInfo(String path) { Map map = new HashMap(); // 身份证识别url String idcardIdentificate = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard"; // 本地图片路径 String filePath = "C:\\Users\\Administrator\\Desktop\\正面.jpg"; try { 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 = AuthService.getAuth(); String result = HttpUtil.post(idcardIdentificate, accessToken, params); JSONObject obj = JSON.parseObject(result); String name = obj.getJSONObject("words_result").getJSONObject("姓名").getString("words"); String age = obj.getJSONObject("words_result").getJSONObject("姓名").getString("words"); String number = obj.getJSONObject("words_result").getJSONObject("公民身份号码").getString("words"); String gender = obj.getJSONObject("words_result").getJSONObject("性别").getString("words"); int leh = number.length(); String dates=""; if (leh == 18) { int se = Integer.valueOf(number.substring(leh - 1)) % 2; dates = number.substring(6, 10); SimpleDateFormat df = new SimpleDateFormat("yyyy"); String year=df.format(new Date()); int u=Integer.parseInt(year)-Integer.parseInt(dates); age = u+""; }else{ dates = number.substring(6, 8); age = ""+Integer.parseInt(dates); } map.put("name", name); map.put("idNum",number); map.put("age",age); map.put("gender",gender); } catch (Exception e) { e.printStackTrace(); } return map; } }