# java调用百度AI实现图文识别功能
# 1 创建百度应用
1、在浏览器输入网址https://login.bce.baidu.com/或者百度搜索‘百度ai’点击第一个。点击主页的产品服务,看到文字识别。如下图所示:
2、点击创建应用
创建完应用后,才能获取百度AI的授权
3、创建完成后可查看相应API key和Secret Key
# 2 查看sdk文档
点击文字识别下面的技术文档https://cloud.baidu.com/doc/OCR/index.html,如图
可以根据官方提供的文档进行图文识别,下面博主为大家提供了java的工具类
# 3 java调用图文识别的工具类
# 3.1 获取access_token
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 获取token类
* https://github.com/ourlang
* @author 福小林
*/
public class AuthService {
/**
* 获取权限token
* @return 返回示例:
* {
* "access_token": "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567",
* "expires_in": 2592000
* }
*/
public static String getAuth() {
// 官网获取的API Key 更新为你注册的
String clientId = "sZoHMGl0jo1pSXQ1SeWiG8kv";
// 官网获取的 Secret Key 更新为你注册的
String clientSecret = "FPRCv0tbhoEXjDD6Gj7XWLhg64EqAelo";
return getAuth(clientId, clientSecret);
}
/**
* 获取API访问token
* 该token有一定的有效期,需要自行管理,当失效时需重新获取.
* @param ak - 百度云官网获取的 API Key
* @param sk - 百度云官网获取的 Securet Key
* @return assess_token 示例:
* "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567"
*/
private static String getAuth(String ak, String sk) {
// 获取token地址
String authHost = "https://aip.baidubce.com/oauth/2.0/token?";
String getAccessTokenUrl = authHost
// 1. grant_type为固定参数
+ "grant_type=client_credentials"
// 2. 官网获取的 API Key
+ "&client_id=" + ak
// 3. 官网获取的 Secret Key
+ "&client_secret=" + sk;
try {
URL realUrl = new URL(getAccessTokenUrl);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
connection.setRequestMethod("GET");
connection.connect();
// 定义 BufferedReader输入流来读取URL的响应
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder result = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
JSONObject jsonObject = new JSONObject(result.toString());
return jsonObject.getString("access_token");
} catch (Exception e) {
//打印错误日志
e.printStackTrace(System.err);
}
return null;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 3.2 调用服务测试
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.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
/**
* 图像文字识别公用接口类
* https://github.com/ourlang
* @author 福小林
*/
public class ImageTextUtil {
/**
* 调用百度的图文识别接口
*/
private static final String POST_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + AuthService.getAuth();
private static final String BASE64_START_MARK = "data:image/png;base64,";
/**
* 根据图片的base64编码实现图文识别功能
*
* @param imgStr base64图片字符串
* @return 识别结果,为json格式
* @throws URISyntaxException URI打开异常
* @throws IOException io流异常
*/
public static String getTextByBase64(String imgStr) throws URISyntaxException, IOException {
StringBuilder sb = new StringBuilder();
if (imgStr.startsWith(BASE64_START_MARK)){
imgStr=imgStr.substring(22);
}
String[] strings = splitStringByLength(imgStr, 1000);
for (String tempStr : strings) {
sb.append(tempStr);
}
String s = encodeUriComponent(sb.toString());
String param = "image=" + s;
return post(param);
}
/**
* @param url 图片url http地址
* @return 识别结果,为json格式
*/
public static String getTextByUrl(String url) throws IOException, URISyntaxException {
String param = "url=" + url;
return post(param);
}
/***
* 将字符串按固定长度切割成字符子串
* @param src 需要切割的字符串
* @param length 字符子串的长度
* @return 字符子串数组
*/
public static String[] splitStringByLength(String src, int length) {
//检查参数是否合法
if (null == src || src.isEmpty()) {
System.out.println("the string is null");
return null;
}
if (length <= 0) {
System.out.println("the length < 0");
return null;
}
//获取整个字符串可以被切割成字符子串的个数
int n = (src.length() + length - 1) / length;
String[] split = new String[n];
for (int i = 0; i < n; i++) {
if (i < (n - 1)) {
split[i] = src.substring(i * length, (i + 1) * length);
} else {
split[i] = src.substring(i * length);
}
}
return split;
}
/**
* 通过传递参数: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 = new DefaultHttpClient();
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;
}
/**
* 返回编码后的字符串
*
* @param s 要编码的字符串
* @return 编码好的字符串
*/
public static String encodeUriComponent(String s) {
String result;
try {
result = URLEncoder.encode(s, "UTF-8")
.replaceAll("\\+", "%20")
.replaceAll("\\%21", "!")
.replaceAll("\\%27", "'")
.replaceAll("\\%28", "(")
.replaceAll("\\%29", ")")
.replaceAll("\\%7E", "~");
}
// This exception should never occur.
catch (UnsupportedEncodingException e) {
result = s;
}
return result;
}
public static void main(String[] args) {
try {
String textByUrl = getTextByUrl("http://lsdcloud.com/img/goImage/file.png");
System.out.println(textByUrl);
} catch (URISyntaxException | IOException e) {
e.printStackTrace();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# 3.3.输出结果如下
← 后台服务一键启动bat 跨域的本质 →