本文實例為大家分享了java實現百度云文字識別的接口具體代碼,供大家參考,具體內容如下
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
|
public class images { public static string getresult() { string otherhost = "https://aip.baidubce.com/rest/2.0/ocr/v1/general" ; // 本地圖片路徑 string str= "你的本地圖片路徑" string filepath = "str" ; try { byte [] imgdata = fileutil.readfilebybytes(filepath); string imgstr = base64util.encode(imgdata); string params = urlencoder.encode( "image" , "utf-8" ) + "=" + urlencoder.encode(imgstr, "utf-8" ); /** * access_token有過期時間, 客戶端可自行緩存,過期后重新獲取。 */ string accesstoken = getauth( "申請的api key" , "申請的secret key" ); //system.out.println("wwwwwwwwwwwwww"); string result = httputil.post(otherhost, accesstoken, params); //system.out.println("sssssssssssssssssss"); return result; //system.out.println(result); } catch (exception e) { e.printstacktrace(); return null ; } } public 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(); // 獲取所有響應頭字段 map<string, list<string>> map = connection.getheaderfields(); // 遍歷所有的響應頭字段 for (string key : map.keyset()) { system.err.println(key + "--->" + map.get(key)); } // 定義 bufferedreader輸入流來讀取url的響應 bufferedreader in = new bufferedreader( new inputstreamreader(connection.getinputstream())); string result = "" ; string line; while ((line = in.readline()) != null ) { result += line; } /** * 返回結果示例 */ system.out.println( "result:" + result); jsonobject jsonobject = new jsonobject(result); string access_token = jsonobject.getstring( "access_token" ); return access_token; } catch (exception e) { system.err.printf( "獲取token失敗!" ); 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
|
public static void main(string[] args) { string otherhost = "https://aip.baidubce.com/rest/2.0/ocr/v1/general" ; // 本地圖片路徑 string filepath = "本地圖片路徑" ; try { byte [] imgdata = fileutil.readfilebybytes(filepath); string imgstr = base64util.encode(imgdata); string params = urlencoder.encode( "image" , "utf-8" ) + "=" + urlencoder.encode(imgstr, "utf-8" ); * //** * 線上環境access_token有過期時間, 客戶端可自行緩存,過期后重新獲取。 * //* string accesstoken = getauth( "api key" , "secret key" ); //system.out.println("wwwwwwwwwwwwww"); string result = httputil.post(otherhost, accesstoken, params); //system.out.println("sssssssssssssssssss"); system.out.println(result); } catch (exception e) { e.printstacktrace(); } } |
小編再另分享一份網上找到的代碼,百度云ocr文字識別功能,作者是:笑釋一切。
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
|
import java.util.hashmap; import java.util.iterator; import org.json.jsonarray; import org.json.jsonobject; import com.baidu.aip.ocr.aipocr; /** * 測試百度云ocr的文字識別功能 <br> * 打開百度云ai的官網: <br> * https://console.bce.baidu.com/ai/?_=1517288853048#/ai/ocr/overview/index <br> */ public class testocr { //設置app id/ak/sk public static final string app_id = "10736110" ; public static final string api_key = "4nguig7odphzfhdfnz2abvhx" ; public static final string secret_key = "8gnuzj19h0nie5noc7hsgsh2vigju9vl" ; public static void main(string[] args) { // 初始化一個aipocr aipocr client = new aipocr(app_id, api_key, secret_key); // 傳入可選參數調用接口 hashmap<string, string> options = new hashmap<string, string>(); // 是否定位單字符位置,big:不定位單字符位置,默認值;small:定位單字符位置 options.put( "recognize_granularity" , "big" ); // 識別語言類型,默認為chn_eng。可選值包括: // chn_eng:中英文混合; // eng:英文; // por:葡萄牙語; // fre:法語; // ger:德語; // ita:意大利語; // spa:西班牙語; // rus:俄語; // jap:日語; // kor:韓語; options.put( "language_type" , "chn_eng" ); // 是否檢測圖像朝向,默認不檢測,即:false。朝向是指輸入圖像是正常方向、逆時針旋轉90/180/270度。 options.put( "detect_direction" , "true" ); // 是否檢測語言,默認不檢測。當前支持(中文、英語、日語、韓語) options.put( "detect_language" , "true" ); // 是否返回文字外接多邊形頂點位置,不支持單字位置。默認為false options.put( "vertexes_location" , "false" ); // 是否返回識別結果中每一行的置信度 options.put( "probability" , "false" ); // 可選:設置網絡連接參數 client.setconnectiontimeoutinmillis( 2000 ); client.setsockettimeoutinmillis( 60000 ); // 調用接口 string path = "d:\\qq截圖20180130134257.png" ; jsonobject res = client.accurategeneral(path, options); jsonarray myjson = res.getjsonarray( "words_result" ); iterator<object> iterator = myjson.iterator(); while (iterator.hasnext()){ object value = iterator.next(); jsonobject obj = new jsonobject(value.tostring()); system.out.println(obj.get( "words" )); } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/syy363250763/article/details/80843480