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

五柳先生柳三变      2022-05-03     726

关键词:

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

  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 }

 

基于百度ocr提取图像中的文本(代码片段)

...服务及提高识别率就需要注册且付费。本文主要讲述如何通过百度OCR服务来识别图片中的文本。开通百度OCR服务登录百度的AI开放平台,在开放功能的文本识别模块选择通用文字识别,开通服务(下面图片是开通服务后的页面)... 查看详情

基于百度ocr提取图像中的文本(代码片段)

...服务及提高识别率就需要注册且付费。本文主要讲述如何通过百度OCR服务来识别图片中的文本。开通百度OCR服务登录百度的AI开放平台,在开放功能的文本识别模块选择通用文字识别,开通服务(下面图片是开通服务后的页面)... 查看详情

app开发-使用vue3+vant+html5+实现相机拍照,选取相册图片,裁剪图片以及提取图片中的文字等功能(代码片段)

...Html5+一、提取图片中的文字对于文字识别的技术,百度OCR技术可以说是非常厉害了,不仅可以满足我们文字识别的功能,还有其它各种各样的识别功能。以下是从官网的部分截图,文档也是比较齐全的,所... 查看详情

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

...成了对简单带有背景的文字图片识别,后续会逐步增加对身份证、银行卡、驾驶证、车牌号的识别,所 查看详情

app开发-使用vue3+vant+html5+实现相机拍照,选取相册图片,裁剪图片以及提取图片中的文字等功能(代码片段)

...Html5+一、提取图片中的文字对于文字识别的技术,百度OCR技术可以说是非常厉害了,不仅可以满足我们文字识别的功能,还有其它各种各样的识别功能。以下是从官网的部分截图,文档也是比较齐全的,所... 查看详情

app开发-使用vue3+vant+html5+实现相机拍照,选取相册图片,裁剪图片以及提取图片中的文字等功能(代码片段)

...Html5+一、提取图片中的文字对于文字识别的技术,百度OCR技术可以说是非常厉害了,不仅可以满足我们文字识别的功能,还有其它各种各样的识别功能。以下是从官网的部分截图,文档也是比较齐全的,所... 查看详情

python调用百度ai对颜值评分(代码片段)

上一篇文章介绍了应用百度AI的文字识别功能对身份证进行识别。本文介绍应用百度AI的人脸识别功能对年龄、性别、颜值等进行识别,感兴趣的朋友一起来看看效果吧。由于安装baidu-aip模块和获取百度AI接口密钥在之前的文... 查看详情

java调用百度ocr接口实现文字识别(代码片段)

...字识别功能,由于之前有过使用百度云平台接口进行身份证识别的经历,因此这次也是自然而然的再次选择了百度AI平台,首先需要开通百度通用文字识别功能。然后我们需要创建一个应用:然后我们就可以进行... 查看详情

java调用百度ocr接口实现文字识别(代码片段)

...字识别功能,由于之前有过使用百度云平台接口进行身份证识别的经历,因此这次也是自然而然的再次选择了百度AI平台,首先需要开通百度通用文字识别功能。然后我们需要创建一个应用:然后我们就可以进行... 查看详情

ai提取图片里包含的文字信息-解决文字无法复制的痛点(代码片段)

1.前言平时工作中编写开发技术文档,或者学生在编写论文时,经常会上网搜索一些参考文献、文档。比如:上网搜索相似的内容参考一下或者引用别人的一段文字,有时候看到一篇较好的内容想要保存等等。这个过程... 查看详情

android图片文字识别demo(基于百度ocr)

...方式将纸质文档中的文字转换成为黑白点阵的图像文件,通过识别软件将图像中的文字转换成文本格式,供文字处理软件进一步编辑加工的技术(好吧,这是我查来的)。简单的来说,OCR技术就是可以把图片上的文字识别出来,... 查看详情

人口普查分析:利用python+百度文字识别提取图片中的表格数据(代码片段)

今天发布了最新的人口普查结果,笔者拿到的文件是pdf格式(网上应该有)。之前就一直想实现从pdf提取表格数据,输出为excel。正好这次有公开数据,因此打算用来练个手。尝试了两种方法:1.python的pdfpl... 查看详情

人工智能初识(百度ai)(代码片段)

...频识别:抖音内容审核,视频社交APP的审核机制文字识别:从身份证照片提取身份证号码,扫一扫翻译语义理解:智能问答机器人,也包含小米的小爱同学,苹果的siri,微软的Cortana我们身边的人工智能银行办卡刷脸就行车辆违章有牌儿就... 查看详情

人口普查分析:利用python+百度文字识别提取图片中的表格数据(代码片段)

...智能云的文字识别:需要把pdf先转换成图片,再通过图片识别完成(其实。。。感觉有些多此一举。。。),不过对于边框有缺失的表格感觉效果一般。 以下是用python+百度智能云的文字识别抽取表格信息... 查看详情

ai怎么把图片文字画出路径提取出来

参考技术A1、首先我们打开AI。2、然后我们如图在路径上有一些文字。3、这时我们在单机点击选择工具。4、这时我们在选择文字,出现如图的垂线。5、移动垂线即可移动文字。6、然后将垂线拖到内部。7、如图所示文字就到了... 查看详情

将图片局部文字提取出来的小技巧

...  步骤一、为了实现文字局部识别功能,我们需要借助百度搜索下载相关OCR文字识别工具,小编在这里使用到的是迅捷OCR文字识别软件,可通过搜索迅捷办公找到。  步骤二、打 查看详情

python案例ocr提取图片中的文字(代码片段)

很多软件内置了OCR功能,即图片提取文字功能。有些是免费提供给大家使用,但有些是收费的。不管是免费的还是收费的,终究逃离不了隐私问题。用别人的OCR,总得把图片传到对方的服务器。今天我们使用Python... 查看详情

python案例ocr提取图片中的文字(代码片段)

很多软件内置了OCR功能,即图片提取文字功能。有些是免费提供给大家使用,但有些是收费的。不管是免费的还是收费的,终究逃离不了隐私问题。用别人的OCR,总得把图片传到对方的服务器。今天我们使用Python... 查看详情