实现代码如下:
private boolean getIdcard(Driver driver, String face, String accessToken) throws Exception { String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard"; String imgParam = null; if(driver.getIdcareId().startsWith("http")){ String imgStr = ImageKit.image2Base64(driver.getIdcareId()); imgParam = URLEncoder.encode(imgStr, "UTF-8"); }else { byte[] imgData = FileUtil.readFileByBytes(driver.getIdcareId()); String imgStr = Base64Util.encode(imgData); imgParam = URLEncoder.encode(imgStr, "UTF-8"); } String param = "id_card_side=" + face + "&image=" + imgParam; String result = HttpUtil.post(url, accessToken, param); // System.out.println(result); Map map=new ObjectMapper().readValue(result,Map.class); Map words_result= (Map) map.get("words_result"); if(!result.contains("住址")){ return true; } if (face.equals("front")){ String addr= ((Map) words_result.get("住址")).get("words")+""; String birthday= ((Map) words_result.get("出生")).get("words")+""; birthday = birthday.replaceAll("(.{4})(.{2})(.{2})","$1-$2-$2"); String name= ((Map) words_result.get("姓名")).get("words")+""; String sex= ((Map) words_result.get("性别")).get("words")+""; String nation= ((Map) words_result.get("民族")).get("words")+""; driver.setDriveraddress(addr); driver.setDriverbirthday(DateUtils.parseDate(birthday, "yyyy-MM-dd")); driver.setDrivername(name); driver.setDrivergender(sex); driver.setDrivernation(nation); }else { } return false; }