2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 保姆级——Java调用百度OCR实现身份证识别

保姆级——Java调用百度OCR实现身份证识别

时间:2019-04-26 00:34:22

相关推荐

保姆级——Java调用百度OCR实现身份证识别

具体实现功能和参数,可以看百度的API开发文档:/ai-doc/OCR/rk3h7xzck

其实我是看不懂API文档的

文章中的请求参数

调用百度的OCR需要做的准备工作

先注册一个百度智能云账号:然后在首页,选择创建应用

创建完成之后在应用列表就可以找到你刚创建的应用,就可以获取到你需要用到的APPID、API_KEY 、SECRET_KEY

然后你可以在首页免费领取你需要用到的资源

接下来就是实现

有两种实现

1.通用OCR文字识别

这种OCR只能按照识别图片中的文字,且是按照行识别返回结果,精度较低

有点类似于word文档的导入

首先引入需要的依赖包

<dependency><groupId>com.baidu.aip</groupId><artifactId>java-sdk</artifactId><version>4.6.0</version></dependency>

实现工具类

import java.util.HashMap;import com.baidu.aip.ocr.AipOcr;import org.json.JSONObject;/*** @author sun 通用OCR文字识别* @date -11-11 20:10* @Decsription: com.ocr.util* @version: 1.0*/public class OcrApi {private static final String APP_ID = "";private static final String API_KEY = "";private static final String SECRET_KEY = "";private static AipOcr getAipClient() {return getAipClient(API_KEY, SECRET_KEY);}public static AipOcr getAipClient(String apiKey, String secretKey) {AipOcr client = new AipOcr(APP_ID, apiKey, secretKey);// 可选:设置网络连接参数client.setConnectionTimeoutInMillis(2000);client.setSocketTimeoutInMillis(60000);return client;}public static String result(AipOcr client) {// 传入可选参数调用接口HashMap<String, String> options = new HashMap<>();options.put("language_type", "CHN_ENG");options.put("detect_direction", "true");options.put("detect_language", "true");options.put("probability", "true");JSONObject res = client.basicGeneralUrl("图片路径", options);return res.toString(2);}public static void main(String[] args) {System.out.println(result(getAipClient()));}}

2.高精度OCR识别身份证信息

这种就比较高精度,且按照分类显示,返回数据更友好,高可用。

1、准备pom文件

<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.5</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.28</version></dependency>

2.获取Access_token

由于Access_token会过期,为了更好的实现需求,我们每次都获取最新的Access_token,获取的方式也非常的简单。

import java.io.BufferedReader;import java.io.InputStreamReader;import .HttpURLConnection;import .URL;import java.util.List;import java.util.Map;import org.json.JSONObject;public class AccessTokenUtils {private static String APIKEY = "";private static String SecretKEY = "";// 获取Token路径private static String PATH = "/oauth/2.0/token?";public static String getAuth() {// 获取token地址String getAccessTokenUrl = PATH// 1. grant_type为固定参数+ "grant_type=client_credentials"// 2. 官网获取的 API Key+ "&client_id=" + APIKEY// 3. 官网获取的 Secret Key+ "&client_secret=" + SecretKEY;try {URL realUrl = new URL(getAccessTokenUrl);// 打开和URL之间的连接HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();connection.setRequestMethod("GET");connection.connect();// 获取所有响应头字段Map<String, List<String>> map = connection.getHeaderFields();// 定义 BufferedReader输入流来读取URL的响应BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));String result = "";String line;while ((line = in.readLine()) != null) {result += line;}JSONObject jsonObject = new 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;}}

3.本地上传需要将图片转为Base64码,Url图片可以直接传网络地址

该方法只能传本地图片,oss的图片路径无法使用

import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import sun.misc.BASE64Encoder;public class BaseImg64Utils {/*** 将一张本地图片转化成Base64字符串* @param imgPath 本地图片地址* @return 图片转化base64后再UrlEncode结果*/public static String getImageStrFromPath(String imgPath) {InputStream in = null;byte[] data = null;// 读取图片字节数组try {in = new FileInputStream(imgPath);data = new byte[in.available()];in.read(data);in.close();} catch (IOException e) {e.printStackTrace();}// 对字节数组Base64编码BASE64Encoder encoder = new BASE64Encoder();// 返回Base64编码过的字节数组字符串return encoder.encode(data).replaceAll("\r\n", "").replaceAll("\\+", "%2B");}}

4.调用API接口的方法,获取识别结果

import java.io.File;import java.io.IOException;import .URI;import .URISyntaxException;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.HttpClientBuilder;import org.apache.http.util.EntityUtils;public class XszOcrUtils {private static final String POST_URL = "/rest/2.0/ocr/v1/idcard?access_token="+ AccessTokenUtils.getAuth();//必传参数:id_card_side=frontfront:身份证含照片的一面 -back:身份证带国徽的一面 自动检测身份证正反面,如果传参指定方向与图片相反,支持正常识别,返回参数image_status字段为"reversed_side"/*** 识别本地图片的文字** @param path 本地图片地址* @return 识别结果,为json格式* @throws URISyntaxException URI打开异常* @throws IOException io流异常*/public static String checkFile(String path) throws URISyntaxException, IOException {File file = new File(path);if (!file.exists()) {throw new NullPointerException("图片不存在");}String image = BaseImg64Utils.getImageStrFromPath(path);String param = "image=" + image + "&id_card_side=front";return post(param);}/*** @param url 图片url* @return 识别结果,为json格式*/public static String checkUrl(String url) throws IOException, URISyntaxException {String param = "url=" + url;return post(param);}/*** 通过传递参数:url和image进行文字识别** @param param 区分是url还是image识别* @return 识别结果* @throws URISyntaxException URI打开异常* @throws IOException IO流异常*/private static String post(String param) throws URISyntaxException, IOException {// 开始搭建post请求HttpClient httpClient = HttpClientBuilder.create().build();HttpPost post = new HttpPost();URI url = new URI(POST_URL);post.setURI(url);// 设置请求头,请求头必须为application/x-www-form-urlencoded,因为是传递一个很长的字符串,不能分段发送post.setHeader("Content-Type", "application/x-www-form-urlencoded");StringEntity entity = new StringEntity(param);post.setEntity(entity);HttpResponse response = httpClient.execute(post);if (response.getStatusLine().getStatusCode() == 200) {String str;try {/* 读取服务器返回过来的json字符串数据 */str = EntityUtils.toString(response.getEntity());return str;} catch (Exception e) {e.printStackTrace();return null;}}return null;}public static void main(String[] args) throws URISyntaxException, IOException {String checkFile = checkFile("E:\\tmp_40d75be6049049f841cbbee213743430d699596c8c08ae47ac76a60f36de6189.jpeg");System.out.println("========" + checkFile);}

5.识别结果(正面)

{"log_id": 2648325511,"direction": 0,"image_status": "normal","photo": "/9j/4AAQSkZJRgABA......","photo_location": {"width": 1189,"top": 638,"left": 2248,"height": 1483},"card_image": "/9j/4AAQSkZJRgABA......","card_location": {"top": 328,"left": 275,"width": 1329,"height": 571},"words_result": {"住址": {"location": {"left": 267,"top": 453,"width": 459,"height": 99},"words": "南京市江宁区弘景大道3889号"},"公民身份号码": {"location": {"left": 443,"top": 681,"width": 589,"height": 45},"words": "330881199904173914"},"出生": {"location": {"left": 270,"top": 355,"width": 357,"height": 45},"words": "19990417"},"姓名": {"location": {"left": 267,"top": 176,"width": 152,"height": 50},"words": "伍云龙"},"性别": {"location": {"left": 269,"top": 262,"width": 33,"height": 52},"words": "男"},"民族": {"location": {"left": 492,"top": 279,"width": 30,"height": 37},"words": "汉"}},"words_result_num": 6}

反面

{"words_result": {"失效日期": {"words": "20390711","location": {"top": 445,"left": 523,"width": 153,"height": 38}},"签发机关": {"words": "陆丰市公安局","location": {"top": 377,"left": 339,"width": 195,"height": 38}},"签发日期": {"words": "0606","location": {"top": 445,"left": 343,"width": 152,"height": 38}}},"log_id": "1559208562721579328","words_result_num": 3,"error_code": 0,"image_status": "normal"}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。