百度云ocr是国内用得比较多的第三方OCR识别API了。速度快,识别率高,支持旋转后的图片等功能深受广大码农的喜爱。今天咱们要说的是百度云OCR中的一个比较常用的识别源码:
java版的百度云ocr源码参考:
/** * 身份证ocr * @param file 身份证正面照片文件 * @return map */ public Map<String,String> idCardOcr(MultipartFile file) throws Exception { Map result = new HashMap(); JSONObject jsonObject = null; String filePath, fileImg = ""; //本地图片转为base64编码 fileImg = FileUtils.multipartFileToBASE64(file); Map<String,Object> params = new HashMap<>(); params.put("image", fileImg); params.put("id_card_side", "front"); Map<String, String> headers = new HashMap<>(); headers.put("Content-Type", "application/x-www-form-urlencoded"); String s = ""; try { s = HttpUtils.httpClientPost(idCardUrlPath + getAuth(), params, "UTF-8", headers); } catch (Exception e){ e.printStackTrace(); result.put("status","false"); result.put("result",s); return result; } System.err.println("百度云身份证识别结果->" + s); jsonObject = JSON.parseObject(s); if(!"normal".equals(jsonObject.getString("image_status"))){ result.put("status","false"); result.put("result",jsonObject.getString("image_status")); return result; } JSONObject words_result = jsonObject.getJSONObject("words_result"); String address = words_result.getJSONObject("住址").getString("words"); String id_card = words_result.getJSONObject("公民身份号码").getString("words"); String name = words_result.getJSONObject("姓名").getString("words"); result.put("status","true"); result.put("address",address); result.put("id_card",id_card); result.put("name",name); return result; }