Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit be4f2ad

Browse files
author
suqin
committed
zhuan
1 parent 4b35b4e commit be4f2ad

2 files changed

Lines changed: 93 additions & 1 deletion

File tree

‎B_Web/src/ocr/F.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
public class F {
1818
public static void main(String[] args) {
1919
//获取本地的绝对路径图片
20-
File file = new File("C:\\Users\\Administrator\\Desktop\\12343.jpg");
20+
File file = new File("C:\\Users\\Administrator\\Desktop\\1234.png");
2121
//进行BASE64位编码
2222
String imageBase = BASE64.encodeImgageToBase64(file);
2323
imageBase = imageBase.replaceAll("\r\n","");

‎B_Web/src/ocr/text/AuthService.java‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package ocr.text;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
import java.net.HttpURLConnection;
6+
import java.net.URL;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
import org.json.JSONObject;
11+
12+
public class AuthService {
13+
14+
/**
15+
* 获取权限token
16+
* @return 返回示例:
17+
* {
18+
* "access_token": "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567",
19+
* "expires_in": 2592000
20+
* }
21+
*/
22+
public static String getAuth() {
23+
// 官网获取的 API Key 更新为你注册的
24+
String clientId = "**";
25+
// 官网获取的 Secret Key 更新为你注册的
26+
String clientSecret = "**";
27+
return getAuth(clientId, clientSecret);
28+
}
29+
30+
/**
31+
* 获取API访问token
32+
* 该token有一定的有效期,需要自行管理,当失效时需重新获取.
33+
* @param ak - 百度云官网获取的 API Key
34+
* @param sk - 百度云官网获取的 Securet Key
35+
* @return assess_token 示例:
36+
* "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567"
37+
*/
38+
private static String getAuth(String ak, String sk) {
39+
// 获取token地址
40+
String authHost = "https://aip.baidubce.com/oauth/2.0/token?";
41+
String getAccessTokenUrl = authHost
42+
// 1. grant_type为固定参数
43+
+ "grant_type=client_credentials"
44+
// 2. 官网获取的 API Key
45+
+ "&client_id=" + "NnA1zs4neW1VzuWtvi2Ykn0c"
46+
// 3. 官网获取的 Secret Key
47+
+ "&client_secret=" + "aeGoBSGtoNlGquwVHj2jAHDXGGxx0HB2";
48+
49+
50+
//String clientId = "NnA1zs4neW1VzuWtvi2Ykn0c";
51+
// 官网获取的 Secret Key
52+
// String clientSecret = "aeGoBSGtoNlGquwVHj2jAHDXGGxx0HB2";
53+
54+
55+
try {
56+
URL realUrl = new URL(getAccessTokenUrl);
57+
// 打开和URL之间的连接
58+
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
59+
connection.setRequestMethod("GET");
60+
connection.connect();
61+
// 获取所有响应头字段
62+
Map<String, List<String>> map = connection.getHeaderFields();
63+
// 遍历所有的响应头字段
64+
for (String key : map.keySet()) {
65+
System.err.println(key + "--->" + map.get(key));
66+
}
67+
// 定义 BufferedReader输入流来读取URL的响应
68+
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
69+
StringBuilder result = new StringBuilder();
70+
String line;
71+
while ((line = in.readLine()) != null) {
72+
result.append(line);
73+
}
74+
/**
75+
* 返回结果示例
76+
*/
77+
System.err.println("result:" + result);
78+
JSONObject jsonObject = new JSONObject(result.toString());
79+
return jsonObject.getString("access_token");
80+
} catch (Exception e) {
81+
System.err.printf("获取token失败!");
82+
e.printStackTrace(System.err);
83+
}
84+
return null;
85+
}
86+
87+
public static void main(String[] args) {
88+
getAuth();
89+
}
90+
91+
}
92+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /