印刷文字识别-身份证

Posted gynbk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了印刷文字识别-身份证相关的知识,希望对你有一定的参考价值。

公司要求作身份证识别,一开始在网上搜索java方面有Tesseract-OCR的开源技术识别率可以达到99.99%。但真正接触下来,是说这个技术识别率可以达到99.99%,但需要大量的训练数据,也就是人工智能那一套。搞了半天根本达不到理想要求。那有没有其他办法那,查找了下中国的3大巨头公司阿里,腾讯、百度都有提供身份证识别接口。那就走调用第三方的方法吧。我选择了阿里。

前提:

1、需要申请一个阿里账号,有淘宝号的用淘宝号登录也可以。

2、然后在云市场搜索“印刷文字识别-身份证”。

3、阿里免费提供500次识别。需要购买该产品。

4、产品购买成功后,获取appcode值,这个要放到header中去的。

5、就可调用用了。

需要的jar包:

Maven项目直接配置依赖就可以,非maven项目就需要导入jar包:

Commons-lang-2.4.jar、fastjson-1.2.7.jar、httpclient-4.5.6.jar、httpcode-4.4.10.jar、servlet-api.jar基本这几个就够用了。

 

官方文档:https://market.aliyun.com/products/57124001/cmapi010401.html?spm=5176.730005.productlist.d_cmapi010401.stMjsY#sku=yuncode440100000

其他的不说调用代码如下:

  1 import java.io.UnsupportedEncodingException;
  2 import java.net.URLEncoder;
  3 import java.security.KeyManagementException;
  4 import java.security.NoSuchAlgorithmException;
  5 import java.security.cert.X509Certificate;
  6 import java.util.ArrayList;
  7 import java.util.List;
  8 import java.util.Map;
  9 
 10 import javax.net.ssl.SSLContext;
 11 import javax.net.ssl.TrustManager;
 12 import javax.net.ssl.X509TrustManager;
 13 
 14 import org.apache.commons.lang.StringUtils;
 15 import org.apache.http.HttpResponse;
 16 import org.apache.http.NameValuePair;
 17 import org.apache.http.client.HttpClient;
 18 import org.apache.http.client.entity.UrlEncodedFormEntity;
 19 import org.apache.http.client.methods.HttpDelete;
 20 import org.apache.http.client.methods.HttpGet;
 21 import org.apache.http.client.methods.HttpPost;
 22 import org.apache.http.client.methods.HttpPut;
 23 import org.apache.http.conn.ClientConnectionManager;
 24 import org.apache.http.conn.scheme.Scheme;
 25 import org.apache.http.conn.scheme.SchemeRegistry;
 26 import org.apache.http.conn.ssl.SSLSocketFactory;
 27 import org.apache.http.entity.ByteArrayEntity;
 28 import org.apache.http.entity.StringEntity;
 29 import org.apache.http.impl.client.DefaultHttpClient;
 30 import org.apache.http.message.BasicNameValuePair;
 31 
 32 public class HttpUtils {
 33 
 34     /**
 35      * get
 36      *
 37      * @param host
 38      * @param path
 39      * @param method
 40      * @param headers
 41      * @param querys
 42      * @return
 43      * @throws Exception
 44      */
 45     public static HttpResponse doGet(String host, String path, String method,
 46                                      Map<String, String> headers,
 47                                      Map<String, String> querys)
 48             throws Exception {
 49         HttpClient httpClient = wrapClient(host);
 50 
 51         HttpGet request = new HttpGet(buildUrl(host, path, querys));
 52         for (Map.Entry<String, String> e : headers.entrySet()) {
 53             request.addHeader(e.getKey(), e.getValue());
 54         }
 55 
 56         return httpClient.execute(request);
 57     }
 58 
 59     /**
 60      * post form
 61      *
 62      * @param host
 63      * @param path
 64      * @param method
 65      * @param headers
 66      * @param querys
 67      * @param bodys
 68      * @return
 69      * @throws Exception
 70      */
 71     public static HttpResponse doPost(String host, String path, String method,
 72                                       Map<String, String> headers,
 73                                       Map<String, String> querys,
 74                                       Map<String, String> bodys)
 75             throws Exception {
 76         HttpClient httpClient = wrapClient(host);
 77 
 78         HttpPost request = new HttpPost(buildUrl(host, path, querys));
 79         for (Map.Entry<String, String> e : headers.entrySet()) {
 80             request.addHeader(e.getKey(), e.getValue());
 81         }
 82 
 83         if (bodys != null) {
 84             List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
 85 
 86             for (String key : bodys.keySet()) {
 87                 nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
 88             }
 89             UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
 90             formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
 91             request.setEntity(formEntity);
 92         }
 93 
 94         return httpClient.execute(request);
 95     }
 96 
 97     /**
 98      * Post String
 99      *
100      * @param host
101      * @param path
102      * @param method
103      * @param headers
104      * @param querys
105      * @param body
106      * @return
107      * @throws Exception
108      */
109     public static HttpResponse doPost(String host, String path, String method,
110                                       Map<String, String> headers,
111                                       Map<String, String> querys,
112                                       String body)
113             throws Exception {
114         HttpClient httpClient = wrapClient(host);
115 
116         HttpPost request = new HttpPost(buildUrl(host, path, querys));
117         for (Map.Entry<String, String> e : headers.entrySet()) {
118             request.addHeader(e.getKey(), e.getValue());
119         }
120 
121         if (StringUtils.isNotBlank(body)) {
122             request.setEntity(new StringEntity(body, "utf-8"));
123         }
124 
125         return httpClient.execute(request);
126     }
127 
128     /**
129      * Post stream
130      *
131      * @param host
132      * @param path
133      * @param method
134      * @param headers
135      * @param querys
136      * @param body
137      * @return
138      * @throws Exception
139      */
140     public static HttpResponse doPost(String host, String path, String method,
141                                       Map<String, String> headers,
142                                       Map<String, String> querys,
143                                       byte[] body)
144             throws Exception {
145         HttpClient httpClient = wrapClient(host);
146 
147         HttpPost request = new HttpPost(buildUrl(host, path, querys));
148         for (Map.Entry<String, String> e : headers.entrySet()) {
149             request.addHeader(e.getKey(), e.getValue());
150         }
151 
152         if (body != null) {
153             request.setEntity(new ByteArrayEntity(body));
154         }
155 
156         return httpClient.execute(request);
157     }
158 
159     /**
160      * Put String
161      * @param host
162      * @param path
163      * @param method
164      * @param headers
165      * @param querys
166      * @param body
167      * @return
168      * @throws Exception
169      */
170     public static HttpResponse doPut(String host, String path, String method,
171                                      Map<String, String> headers,
172                                      Map<String, String> querys,
173                                      String body)
174             throws Exception {
175         HttpClient httpClient = wrapClient(host);
176 
177         HttpPut request = new HttpPut(buildUrl(host, path, querys));
178         for (Map.Entry<String, String> e : headers.entrySet()) {
179             request.addHeader(e.getKey(), e.getValue());
180         }
181 
182         if (StringUtils.isNotBlank(body)) {
183             request.setEntity(new StringEntity(body, "utf-8"));
184         }
185 
186         return httpClient.execute(request);
187     }
188 
189     /**
190      * Put stream
191      * @param host
192      * @param path
193      * @param method
194      * @param headers
195      * @param querys
196      * @param body
197      * @return
198      * @throws Exception
199      */
200     public static HttpResponse doPut(String host, String path, String method,
201                                      Map<String, String> headers,
202                                      Map<String, String> querys,
203                                      byte[] body)
204             throws Exception {
205         HttpClient httpClient = wrapClient(host);
206 
207         HttpPut request = new HttpPut(buildUrl(host, path, querys));
208         for (Map.Entry<String, String> e : headers.entrySet()) {
209             request.addHeader(e.getKey(), e.getValue());
210         }
211 
212         if (body != null) {
213             request.setEntity(new ByteArrayEntity(body));
214         }
215 
216         return httpClient.execute(request);
217     }
218 
219     /**
220      * Delete
221      *
222      * @param host
223      * @param path
224      * @param method
225      * @param headers
226      * @param querys
227      * @return
228      * @throws Exception
229      */
230     public static HttpResponse doDelete(String host, String path, String method,
231                                         Map<String, String> headers,
232                                         Map<String, String> querys)
233             throws Exception {
234         HttpClient httpClient = wrapClient(host);
235 
236         HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
237         for (Map.Entry<String, String> e : headers.entrySet()) {
238             request.addHeader(e.getKey(), e.getValue());
239         }
240 
241         return httpClient.execute(request);
242     }
243 
244     private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
245         StringBuilder sbUrl = new StringBuilder();
246         sbUrl.append(host);
247         if (!StringUtils.isBlank(path)) {
248             sbUrl.append(path);
249         }
250         if (null != querys) {
251             StringBuilder sbQuery = new StringBuilder();
252             for (Map.Entry<String, String> query : querys.entrySet()) {
253                 if (0 < sbQuery.length()) {
254                     sbQuery.append("&");
255                 }
256                 if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
257                     sbQuery.append(query.getValue());
258                 }
259                 if (!StringUtils.isBlank(query.getKey())) {
260                     sbQuery.append(query.getKey());
261                     if (!StringUtils.isBlank(query.getValue())) {
262                         sbQuery.append("=");
263                         sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
264                     }
265                 }
266             }
267             if (0 < sbQuery.length()) {
268                 sbUrl.append("?").append(sbQuery);
269             }
270         }
271 
272         return sbUrl.toString();
273     }
274 
275     private static HttpClient wrapClient(String host) {
276         HttpClient httpClient = new DefaultHttpClient();
277         if (host.startsWith("https://")) {
278             sslClient(httpClient);
279         }
280 
281         return httpClient;
282     }
283 
284     private static void sslClient(HttpClient httpClient) {
285         try {
286             SSLContext ctx = SSLContext.getInstance("TLS");
287             X509TrustManager tm = new X509TrustManager() {
288                 public X509Certificate[] getAcceptedIssuers() {
289                     return null;
290                 }
291                 public void checkClientTrusted(X509Certificate[] xcs, String str) {
292 
293                 }
294                 public void checkServerTrusted(X509Certificate[] xcs, String str) {
295 
296                 }
297             };
298             ctx.init(null, new TrustManager[] { tm }, null);
299             SSLSocketFactory ssf = new SSLSocketFactory(ctx);
300             ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
301             ClientConnectionManager ccm = httpClient.getConnectionManager();
302             SchemeRegistry registry = ccm.getSchemeRegistry();
303             registry.register(new Scheme("https", 443, ssf));
304         } catch (KeyManagementException ex) {
305             throw new RuntimeException(ex);
306         } catch (NoSuchAlgorithmException ex) {
307             throw new RuntimeException(ex);
308         }
309     }
310 }

调用的工具累HttpUtils:

  1 import java.io.UnsupportedEncodingException;
  2 import java.net.URLEncoder;
  3 import java.security.KeyManagementException;
  4 import java.security.NoSuchAlgorithmException;
  5 import java.security.cert.X509Certificate;
  6 import java.util.ArrayList;
  7 import java.util.List;
  8 import java.util.Map;
  9 
 10 import javax.net.ssl.SSLContext;
 11 import javax.net.ssl.TrustManager;
 12 import javax.net.ssl.X509TrustManager;
 13 
 14 import org.apache.commons.lang.StringUtils;
 15 import org.apache.http.HttpResponse;
 16 import org.apache.http.NameValuePair;
 17 import org.apache.http.client.HttpClient;
 18 import org.apache.http.client.entity.UrlEncodedFormEntity;
 19 import org.apache.http.client.methods.HttpDelete;
 20 import org.apache.http.client.methods.HttpGet;
 21 import org.apache.http.client.methods.HttpPost;
 22 import org.apache.http.client.methods.HttpPut;
 23 import org.apache.http.conn.ClientConnectionManager;
 24 import org.apache.http.conn.scheme.Scheme;
 25 import org.apache.http.conn.scheme.SchemeRegistry;
 26 import org.apache.http.conn.ssl.SSLSocketFactory;
 27 import org.apache.http.entity.ByteArrayEntity;
 28 import org.apache.http.entity.StringEntity;
 29 import org.apache.http.impl.client.DefaultHttpClient;
 30 import org.apache.http.message.BasicNameValuePair;
 31 
 32 public class HttpUtils {
 33 
 34     /**
 35      * get
 36      *
 37      * @param host
 38      * @param path
 39      * @param method
 40      * @param headers
 41      * @param querys
 42      * @return
 43      * @throws Exception
 44      */
 45     public static HttpResponse doGet(String host, String path, String method,
 46                                      Map<String, String> headers,
 47                                      Map<String, String> querys)
 48             throws Exception {
 49         HttpClient httpClient = wrapClient(host);
 50 
 51         HttpGet request = new HttpGet(buildUrl(host, path, querys));
 52         for (Map.Entry<String, String> e : headers.entrySet()) {
 53             request.addHeader(e.getKey(), e.getValue());
 54         }
 55 
 56         return httpClient.execute(request);
 57     }
 58 
 59     /**
 60      * post form
 61      *
 62      * @param host
 63      * @param path
 64      * @param method
 65      * @param headers
 66      * @param querys
 67      * @param bodys
 68      * @return
 69      * @throws Exception
 70      */
 71     public static HttpResponse doPost(String host, String path, String method,
 72                                       Map<String, String> headers,
 73                                       Map<String, String> querys,
 74                                       Map<String, String> bodys)
 75             throws Exception {
 76         HttpClient httpClient = wrapClient(host);
 77 
 78         HttpPost request = new HttpPost(buildUrl(host, path, querys));
 79         for (Map.Entry<String, String> e : headers.entrySet()) {
 80             request.addHeader(e.getKey(), e.getValue());
 81         }
 82 
 83         if (bodys != null) {
 84             List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
 85 
 86             for (String key : bodys.keySet()) {
 87                 nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
 88             }
 89             UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
 90             formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
 91             request.setEntity(formEntity);
 92         }
 93 
 94         return httpClient.execute(request);
 95     }
 96 
 97     /**
 98      * Post String
 99      *
100      * @param host
101      * @param path
102      * @param method
103      * @param headers
104      * @param querys
105      * @param body
106      * @return
107      * @throws Exception
108      */
109     public static HttpResponse doPost(String host, String path, String method,
110                                       Map<String, String> headers,
111                                       Map<String, String> querys,
112                                       String body)
113             throws Exception {
114         HttpClient httpClient = wrapClient(host);
115 
116         HttpPost request = new HttpPost(buildUrl(host, path, querys));
117         for (Map.Entry<String, String> e : headers.entrySet()) {
118             request.addHeader(e.getKey(), e.getValue());
119         }
120 
121         if (StringUtils.isNotBlank(body)) {
122             request.setEntity(new StringEntity(body, "utf-8"));
123         }
124 
125         return httpClient.execute(request);
126     }
127 
128     /**
129      * Post stream
130      *
131      * @param host
132      * @param path
133      * @param method
134      * @param headers
135      * @param querys
136      * @param body
137      * @return
138      * @throws Exception
139      */
140     public static HttpResponse doPost(String host, String path, String method,
141                                       Map<String, String> headers,
142                                       Map<String, String> querys,
143                                       byte[] body)
144             throws Exception {
145         HttpClient httpClient = wrapClient(host);
146 
147         HttpPost request = new HttpPost(buildUrl(host, path, querys));
148         for (Map.Entry<String, String> e : headers.entrySet()) {
149             request.addHeader(e.getKey(), e.getValue());
150         }
151 
152         if (body != null) {
153             request.setEntity(new ByteArrayEntity(body));
154         }
155 
156         return httpClient.execute(request);
157     }
158 
159     /**
160      * Put String
161      * @param host
162      * @param path
163      * @param method
164      * @param headers
165      * @param querys
166      * @param body
167      * @return
168      * @throws Exception
169      */
170     public static HttpResponse doPut(String host, String path, String method,
171                                      Map<String, String> headers,
172                                      Map<String, String> querys,
173                                      String body)
174             throws Exception {
175         HttpClient httpClient = wrapClient(host);
176 
177         HttpPut request = new HttpPut(buildUrl(host, path, querys));
178         for (Map.Entry<String, String> e : headers.entrySet()) {
179             request.addHeader(e.getKey(), e.getValue());
180         }
181 
182         if (StringUtils.isNotBlank(body)) {
183             request.setEntity(new StringEntity(body, "utf-8"));
184         }
185 
186         return httpClient.execute(request);
187     }
188 
189     /**
190      * Put stream
191      * @param host
192      * @param path
193      * @param method
194      * @param headers
195      * @param querys
196      * @param body
197      * @return
198      * @throws Exception
199      */
200     public static HttpResponse doPut(String host, String path, String method,
201                                      Map<String, String> headers,
202                                      Map<String, String> querys,
203                                      byte[] body)
204             throws Exception {
205         HttpClient httpClient = wrapClient(host);
206 
207         HttpPut request = new HttpPut(buildUrl(host, path, querys));
208         for (Map.Entry<String, String> e : headers.entrySet()) {
209             request.addHeader(e.getKey(), e.getValue());
210         }
211 
212         if (body != null) {
213             request.setEntity(new ByteArrayEntity(body));
214         }
215 
216         return httpClient.execute(request);
217     }
218 
219     /**
220      * Delete
221      *
222      * @param host
223      * @param path
224      * @param method
225      * @param headers
226      * @param querys
227      * @return
228      * @throws Exception
229      */
230     public static HttpResponse doDelete(String host, String path, String method,
231                                         Map<String, String> headers,
232                                         Map<String, String> querys)
233             throws Exception {
234         HttpClient httpClient = wrapClient(host);
235 
236         HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
237         for (Map.Entry<String, String> e : headers.entrySet()) {
238             request.addHeader(e.getKey(), e.getValue());
239         }
240 
241         return httpClient.execute(request);
242     }
243 
244     private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
245         StringBuilder sbUrl = new StringBuilder();
246         sbUrl.append(host);
247         if (!StringUtils.isBlank(path)) {
248             sbUrl.append(path);
249         }
250         if (null != querys) {
251             StringBuilder sbQuery = new StringBuilder();
252             for (Map.Entry<String, String> query : querys.entrySet()) {
253                 if (0 < sbQuery.length()) {
254                     sbQuery.append("&");
255                 }
256                 if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
257                     sbQuery.append(query.getValue());
258                 }
259                 if (!StringUtils.isBlank(query.getKey())) {
260                     sbQuery.append(query.getKey());
261                     if (!StringUtils.isBlank(query.getValue())) {
262                         sbQuery.append("=");
263                         sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
264                     }
265                 }
266             }
267             if (0 < sbQuery.length()) {
268                 sbUrl.append("?").append(sbQuery);
269             }
270         }
271 
272         return sbUrl.toString();
273     }
274 
275     private static HttpClient wrapClient(String host) {
276         HttpClient httpClient = new DefaultHttpClient();
277         if (host.startsWith("https://")) {
278             sslClient(httpClient);
279         }
280 
281         return httpClient;
282     }
283 
284     private static void sslClient(HttpClient httpClient) {
285         try {
286             SSLContext ctx = SSLContext.getInstance("TLS");
287             X509TrustManager tm = new X509TrustManager() {
288                 public X509Certificate[] getAcceptedIssuers() {
289                     return null;
290                 }
291                 public void checkClientTrusted(X509Certificate[] xcs, String str) {
292 
293                 }
294                 public void checkServerTrusted(X509Certificate[] xcs, String str) {
295 
296                 }
297             };
298             ctx.init(null, new TrustManager[] { tm }, null);
299             SSLSocketFactory ssf = new SSLSocketFactory(ctx);
300             ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
301             ClientConnectionManager ccm = httpClient.getConnectionManager();
302             SchemeRegistry registry = ccm.getSchemeRegistry();
303             registry.register(new Scheme("https", 443, ssf));
304         } catch (KeyManagementException ex) {
305             throw new RuntimeException(ex);
306         } catch (NoSuchAlgorithmException ex) {
307             throw new RuntimeException(ex);
308         }
309     }
310 }

识别结果正确率已经达到可用。

{"sex":"男","num":"410527**********","birth":"1990**","request_id":"20180801150222_a325b5b8ea94c5c3ab2b7c98310252ef","nationality":"汉","address":"河南省******","name":"***","config_str":"{"side":"face"}","success":true,"face_rect":{"center":{"y":488,"x":1412},"angle":-90,"size":{"height":240,"width":208}}}

以上是关于印刷文字识别-身份证的主要内容,如果未能解决你的问题,请参考以下文章

iOS身份证号码识别

印刷字符识别基于matlab OCR印刷字母+数字识别含Matlab源码 1861期

印刷字符识别基于matlab OCR印刷字母+数字识别含Matlab源码 287期

文档地址整理

基于深度学习和语言模型的印刷文字OCR系统

OCR场景文本识别:文字检测+文字识别