java通过百度AI开发平台提取身份证图片中的文字信息

Posted 五柳先生柳三变

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java通过百度AI开发平台提取身份证图片中的文字信息相关的知识,希望对你有一定的参考价值。

废话不多说,直接上代码。。。

  IdCardDemo.java

  1 package com.wulss.baidubce;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.InputStreamReader;
  5 import java.net.HttpURLConnection;
  6 import java.net.URL;
  7 import java.net.URLEncoder;
  8 import java.util.Map;
  9 
 10 import com.wulss.utils.Base64Util;
 11 import com.wulss.utils.FileUtil;
 12 import com.wulss.utils.HttpUtil;
 13 
 14 /**
 15  * 
 16  * @Descript TODO (身份证图片识别 案例)
 17  * @author yeting
 18  * @date 2019年4月18日
 19  *
 20  */
 21 public class IdCardDemo {
 22 
 23     private static final String URL_IDCARD = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";//身份证识别地址
 24     private static final String URL_ACCESSTOKEN = "https://aip.baidubce.com/oauth/2.0/token?"; // 百度AI开发平台 获取token的地址
 25     private static final String API_KEY = "afdH343CAt342YFT7F";    // 百度AI开发平台 获取的 API Key 更新为你注册的
 26     private static final String SECRET_KEY = "js45sdfqRFF65gOd667sd1R7sdr"; // 百度AI开发平台 获取的 Secret Key 更新为你注册的
 27     
 28      /**
 29      * 获取API访问token
 30      * 该token有一定的有效期,需要自行管理,当失效时需重新获取.
 31      * @param ak - 百度云官网获取的 API Key
 32      * @param sk - 百度云官网获取的 Securet Key
 33      * @return assess_token 示例:
 34      * "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567"
 35      */
 36     public static String getAccessToken() {
 37         String getAccessTokenUrl = URL_ACCESSTOKEN
 38                 + "grant_type = client_credentials" // 1. grant_type为固定参数 
 39                 + "&client_id = " + API_KEY // 2. 官网获取的 API Key
 40                 + "&client_secret = " + SECRET_KEY; // 3. 官网获取的 Secret Key
 41         String accessToken = "";
 42         try {
 43             URL realUrl = new URL(getAccessTokenUrl);
 44             
 45             // 打开和URL之间的连接
 46             HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
 47             connection.setRequestMethod("GET");
 48             connection.connect();
 49             
 50             // 获取所有响应头字段
 51 //            Map<String, List<String>> map = connection.getHeaderFields();   
 52             // 遍历所有的响应头字段
 53 //            for (String key : map.keySet()) {
 54 //                System.err.println(key + "--->" + map.get(key));
 55 //            }
 56             
 57             // 定义 BufferedReader输入流来读取URL的响应
 58             BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
 59             String result = "";
 60             String line;
 61             while ((line = in.readLine()) != null) {
 62                 result += line;
 63             }
 64             
 65             System.err.println("result:" + result);
 66             
 67             org.json.JSONObject jsonObject = new org.json.JSONObject(result);
 68             accessToken = jsonObject.getString("access_token");        
 69         } catch (Exception e) {
 70             System.err.printf("获取token失败!");
 71             e.printStackTrace(System.err);
 72         }
 73         return accessToken;
 74     }
 75     
 76     /**
 77      * 身份证识别请求
 78      * @param side 识别身份证正面 front;识别身份证背面 back;
 79      * @param filePath 图片路径
 80      * @param accessToken 线上环境有过期时间, 客户端可自行缓存,过期后重新获取。
 81      * @return 返回身份证号码
 82      */
 83     public static String requestIdCard(String side,String filePath,String accessToken) {
 84         String result = "";
 85 
 86         try {            
 87             //1.请求获取结果
 88             String requestParams = "id_card_side = " + side 
 89                     + "&" + URLEncoder.encode("image", "UTF-8") 
 90                     + "=" + URLEncoder.encode(Base64Util.encode(FileUtil.readFileByBytes(filePath)), "UTF-8");
 91             
 92             result = HttpUtil.post(URL_IDCARD, accessToken, requestParams);//返回json格式的结果
 93             System.out.println(result);
 94             
 95             // 请求返回结果eg:
 96             // String result = 
 97             //    "[{"log_id": 3812339812321238679, "words_result_num": 6,"direction": 2, "image_status": "normal",
 98             //         "words_result": {
 99             //             "住址":{"location": {"width": 123, "top": 123, "height": 4423, "left":1232}, "words": "湖北省咸宁市茶叶巷"},
100             //             "出生": {"location":{"width": 333, "top": 339, "height": 2333, "left": 3333}, "words": "19191010"}, 
101             //             "姓名": {"location": {"width": 133, "top": 309, "height": 303, "left": 2205}, "words": "张三"},
102             //             "公民身份号码":{"location": {"width": 111, "top": 3333, "height": 3335, "left":333}, "words": "430124191910101234"},
103             //             "性别": {"location": {"width":222, "top": 521, "height": 304, "left": 2333}, "words": "男"},
104             //             "民族": {"location": {"width": 111, "top": 3333, "height": 22,"left": 1222}, "words": "汉"}
105             //         }
106             //     }]";
107 
108 //            <!-- json转换工具 依赖jar包-->
109 //            <dependency>
110 //                <groupId>net.sf.json-lib</groupId>
111 //                <artifactId>json-lib</artifactId>
112 //                <version>2.4</version>
113 //                <classifier>jdk15</classifier>
114 //            </dependency>
115             
116             //2.解析结果
117             Map<String,String> resultMap = (Map<String,String>)net.sf.json.JSONObject
118                                 .toBean(net.sf.json.JSONObject.fromObject(result),Map.class);
119             
120             if(resultMap.get("error_code").equals("110")) {
121                 return requestIdCard(side,filePath,getAccessToken()) ;//重新请求
122             }else {
123                 String words = "";
124                 if(resultMap.get("image_status") != null && resultMap.get("image_status").equals("normal")) {//    正常
125                     String wordsResults = resultMap.get("words_result");
126                     Map<String,String> wordsResultMap = (Map<String,String>)net.sf.json.JSONObject
127                             .toBean(net.sf.json.JSONObject.fromObject(wordsResults),Map.class);
128                     
129                     String idCardNums = wordsResultMap.get("公民身份号码");
130                     Map<String,String> idCardNumMap = (Map<String,String>)net.sf.json.JSONObject
131                             .toBean(net.sf.json.JSONObject.fromObject(idCardNums),Map.class);
132                     words = idCardNumMap.get("words");
133                 }
134                 return words;
135             }
136         } catch (Exception e) {
137             e.printStackTrace();
138             result = e.getMessage();
139         }
140         
141         return result;
142     }
143     
144     
145 }

  FileUtil.java

 1 package com.wulss.utils;
 2 import java.io.*;
 3 
 4 /**
 5  * 文件读取工具类
 6  */
 7 public class FileUtil {
 8 
 9     /**
10      * 读取文件内容,作为字符串返回
11      */
12     public static String readFileAsString(String filePath) throws IOException {
13         File file = new File(filePath);
14         if (!file.exists()) {
15             throw new FileNotFoundException(filePath);
16         } 
17 
18         if (file.length() > 1024 * 1024 * 1024) {
19             throw new IOException("File is too large");
20         } 
21 
22         StringBuilder sb = new StringBuilder((int) (file.length()));
23         // 创建字节输入流  
24         FileInputStream fis = new FileInputStream(filePath);  
25         // 创建一个长度为10240的Buffer
26         byte[] bbuf = new byte[10240];  
27         // 用于保存实际读取的字节数  
28         int hasRead = 0;  
29         while ( (hasRead = fis.read(bbuf)) > 0 ) {  
30             sb.append(new String(bbuf, 0, hasRead));  
31         }  
32         fis.close();  
33         return sb.toString();
34     }
35 
36     /**
37      * 根据文件路径读取byte[] 数组
38      */
39     public static byte[] readFileByBytes(String filePath) throws IOException {
40         File file = new File(filePath);
41         if (!file.exists()) {
42             throw new FileNotFoundException(filePath);
43         } else {
44             ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
45             BufferedInputStream in = null;
46 
47             try {
48                 in = new BufferedInputStream(new FileInputStream(file));
49                 short bufSize = 1024;
50                 byte[] buffer = new byte[bufSize];
51                 int len1;
52                 while (-1 != (len1 = in.read(buffer, 0, bufSize))) {
53                     bos.write(buffer, 0, len1);
54                 }
55 
56                 byte[] var7 = bos.toByteArray();
57                 return var7;
58             } finally {
59                 try {
60                     if (in != null) {
61                         in.close();
62                     }
63                 } catch (IOException var14) {
64                     var14.printStackTrace();
65                 }
66 
67                 bos.close();
68             }
69         }
70     }
71 }

  Base64Util.java

  1 package com.wulss.utils;
  2 
  3 
  4 
  5 /**
  6  * Base64 工具类
  7  */
  8 public class Base64Util {
  9     private static final char[] ALPHABET;
 10     private static final char last2byte;
 11     private static final char last4byte;
 12     private static final char last6byte;
 13     private static final char lead6byte;
 14     private static final char lead4byte;
 15     private static final char lead2byte;
 16     private static final char[] encodeTable;
 17     private static int[] toInt;
 18     
 19     static {
 20         ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
 21         last2byte = (char)Integer.parseInt("00000011", 2);
 22         last4byte = (char)Integer.parseInt("00001111", 2);
 23         last6byte = (char)Integer.parseInt("00111111", 2);
 24         lead6byte = (char)Integer.parseInt("11111100", 2);
 25         lead4byte = (char)Integer.parseInt("11110000", 2);
 26         lead2byte = (char)Integer.parseInt("11000000", 2);
 27         encodeTable = new char[] { ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘i‘, ‘j‘, ‘k‘, ‘l‘, ‘m‘, ‘n‘, ‘o‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘, ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘+‘, ‘/‘ };
 28         Base64Util.toInt = new int[128];
 29         for (int i = 0; i < Base64Util.ALPHABET.length; ++i) {
 30             Base64Util.toInt[Base64Util.ALPHABET[i]] = i;
 31         }
 32     }
 33     public Base64Util() {
 34     }
 35 
 36     public static String encode(byte[] from) {
 37         StringBuilder to = new StringBuilder((int) ((double) from.length * 1.34D) + 3);
 38         int num = 0;
 39         char currentByte = 0;
 40 
 41         int i;
 42         for (i = 0; i < from.length; ++i) {
 43             for (num %= 8; num < 8; num += 6) {
 44                 switch (num) {
 45                     case 0:
 46                         currentByte = (char) (from[i] & lead6byte);
 47                         currentByte = (char) (currentByte >>> 2);
 48                     case 1:
 49                     case 3:
 50                     case 5:
 51                     default:
 52                         break;
 53                     case 2:
 54                         currentByte = (char) (from[i] & last6byte);
 55                         break;
 56                     case 4:
 57                         currentByte = (char) (from[i] & last4byte);
 58                         currentByte = (char) (currentByte << 2);
 59                         if (i + 1 < from.length) {
 60                             currentByte = (char) (currentByte | (from[i + 1] & lead2byte) >>> 6);
 61                         }
 62                         break;
 63                     case 6:
 64                         currentByte = (char) (from[i] & last2byte);
 65                         currentByte = (char) (currentByte << 4);
 66                         if (i + 1 < from.length) {
 67                             currentByte = (char) (currentByte | (from[i + 1] & lead4byte) >>> 4);
 68                         }
 69                 }
 70 
 71                 to.append(encodeTable[currentByte]);
 72             }
 73         }
 74 
 75         if (to.length() % 4 != 0) {
 76             for (i = 4 - to.length() % 4; i > 0; --i) {
 77                 to.append("=");
 78             }
 79         }
 80 
 81         return to.toString();
 82     }
 83     
 84     public static byte[] decode(final String s) {
 85         final int delta = s.endsWith("==") ? 2 : (s.endsWith("=") ? 1 : 0);
 86         final byte[] buffer = new byte[s.length() * 3 / 4 - delta];
 87         final int mask = 255;
 88         int index = 0;
 89         for (int i = 0; i < s.length(); i += 4) {
 90             final int c0 = Base64Util.toInt[s.charAt(i)];
 91             final int c2 = Base64Util.toInt[s.charAt(i + 1)];
 92             buffer[index++] = (byte)((c0 << 2 | c2 >> 4) & mask);
 93             if (index >= buffer.length) {
 94                 return buffer;
 95             }
 96             final int c3 = Base64Util.toInt[s.charAt(i + 2)];
 97             buffer[index++] = (byte)((c2 << 4 | c3 >> 2) & mask);
 98             if (index >= buffer.length) {
 99                 return buffer;
100             }
101             final int c4 = Base64Util.toInt[s.charAt(i + 3)];
102             buffer[index++] = (byte)((c3 << 6 | c4) & mask);
103         }
104         return buffer;
105     }
106 }

  HttpUtil.java

 1 package com.wulss.utils;
 2 
 3 
 4 import java.io.BufferedReader;
 5 import java.io.DataOutputStream;
 6 import java.io.InputStreamReader;
 7 import java.net.HttpURLConnection;
 8 import java.net.URL;
 9 import java.util.List;
10 import java.util.Map;
11 
12 /**
13  * http 工具类
14  */
15 public class HttpUtil {
16 
17     public static String post(String requestUrl, String accessToken, String params)
18             throws Exception {
19         String contentType = "application/x-www-form-urlencoded";
20         return HttpUtil.post(requestUrl, accessToken, contentType, params);
21     }
22 
23     public static String post(String requestUrl, String accessToken, String contentType, String params)
24             throws Exception {
25         String encoding = "UTF-8";
26         if (requestUrl.contains("nlp")) {
27             encoding = "GBK";
28         }
29         return HttpUtil.post(requestUrl, accessToken, contentType, params, encoding);
30     }
31 
32     public static String post(String requestUrl, String accessToken, String contentType, String params, String encoding)
33             throws Exception {
34         String url = requestUrl + "?access_token=" + accessToken;
35         return HttpUtil.postGeneralUrl(url, contentType, params, encoding);
36     }
37 
38     public static String postGeneralUrl(String generalUrl, String contentType, String params, String encoding)
39             throws Exception {
40         URL url = new URL(generalUrl);
41         // 打开和URL之间的连接
42         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
43         connection.setRequestMethod("POST");
44         // 设置通用的请求属性
45         connection.setRequestProperty("Content-Type", contentType);
46         connection.setRequestProperty("Connection", "Keep-Alive");
47         connection.setUseCaches(false);
48         connection.setDoOutput(true);
49         connection.setDoInput(true);
50 
51         // 得到请求的输出流对象
52         DataOutputStream out = new DataOutputStream(connection.getOutputStream());
53         out.write(params.getBytes(encoding));
54         out.flush();
55         out.close();
56 
57         // 建立实际的连接
58         connection.connect();
59         // 获取所有响应头字段
60         Map<String, List<String>> headers = connection.getHeaderFields();
61         // 遍历所有的响应头字段
62         for (String key : headers.keySet()) {
63             System.err.println(key + "--->" + headers.get(key));
64         }
65         // 定义 BufferedReader输入流来读取URL的响应
66         BufferedReader in = null;
67         in = new BufferedReader(
68                 new InputStreamReader(connection.getInputStream(), encoding));
69         String result = "";
70         String getLine;
71         while ((getLine = in.readLine()) != null) {
72             result += getLine;
73         }
74         in.close();
75         System.err.println("result:" + result);
76         return result;
77     }
78 }

 

以上是关于java通过百度AI开发平台提取身份证图片中的文字信息的主要内容,如果未能解决你的问题,请参考以下文章

Qt+百度AI文字识别OCR小工具

ai如何提取图片中的字体

Java调用百度OCR接口实现文字识别

Java调用百度OCR接口实现文字识别

借助百度OCR,实现一键识别图片中文字,就是这么酷!

APP开发-使用Vue3+vant+html5+ 实现相机拍照,选取相册图片,裁剪图片以及提取图片中的文字等功能