一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - java實(shí)現(xiàn)百度云文字識(shí)別接口代碼

java實(shí)現(xiàn)百度云文字識(shí)別接口代碼

2021-06-17 11:25syy363250763 Java教程

這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)百度云文字識(shí)別的接口代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java實(shí)現(xiàn)百度云文字識(shí)別的接口具體代碼,供大家參考,具體內(nèi)容如下

?
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有過期時(shí)間, 客戶端可自行緩存,過期后重新獲取。
    */
    string accesstoken = getauth("申請(qǐng)的api key", "申請(qǐng)的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為固定參數(shù)
     + "grant_type=client_credentials"
     // 2. 官網(wǎng)獲取的 api key
     + "&client_id=" + ak
     // 3. 官網(wǎng)獲取的 secret key
     + "&client_secret=" + sk;
   try {
    url realurl = new url(getaccesstokenurl);
    // 打開和url之間的連接
    httpurlconnection connection = (httpurlconnection) realurl.openconnection();
    connection.setrequestmethod("get");
    connection.connect();
    // 獲取所有響應(yīng)頭字段
    map<string, list<string>> map = connection.getheaderfields();
    // 遍歷所有的響應(yīng)頭字段
    for (string key : map.keyset()) {
     system.err.println(key + "--->" + map.get(key));
    }
    // 定義 bufferedreader輸入流來讀取url的響應(yīng)
    bufferedreader in = new bufferedreader(new inputstreamreader(connection.getinputstream()));
    string result = "";
    string line;
    while ((line = in.readline()) != null) {
     result += line;
    }
    /**
     * 返回結(jié)果示例
     */
    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;
  }
}

測(cè)試:

?
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");
    *//**
     * 線上環(huán)境access_token有過期時(shí)間, 客戶端可自行緩存,過期后重新獲取。
     *//*
    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();
   }
 }

小編再另分享一份網(wǎng)上找到的代碼,百度云ocr文字識(shí)別功能,作者是:笑釋一切。

?
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;
 
/**
 * 測(cè)試百度云ocr的文字識(shí)別功能 <br>
 * 打開百度云ai的官網(wǎng): <br>
 * https://console.bce.baidu.com/ai/?_=1517288853048#/ai/ocr/overview/index <br>
 */
public class testocr {
 
 //設(shè)置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) {
 
  // 初始化一個(gè)aipocr
  aipocr client = new aipocr(app_id, api_key, secret_key);
 
  // 傳入可選參數(shù)調(diào)用接口
  hashmap<string, string> options = new hashmap<string, string>();
  // 是否定位單字符位置,big:不定位單字符位置,默認(rèn)值;small:定位單字符位置
  options.put("recognize_granularity", "big");
  // 識(shí)別語言類型,默認(rèn)為chn_eng。可選值包括:
  // chn_eng:中英文混合;
  // eng:英文;
  // por:葡萄牙語;
  // fre:法語;
  // ger:德語;
  // ita:意大利語;
  // spa:西班牙語;
  // rus:俄語;
  // jap:日語;
  // kor:韓語;
  options.put("language_type", "chn_eng");
  // 是否檢測(cè)圖像朝向,默認(rèn)不檢測(cè),即:false。朝向是指輸入圖像是正常方向、逆時(shí)針旋轉(zhuǎn)90/180/270度。
  options.put("detect_direction", "true");
  // 是否檢測(cè)語言,默認(rèn)不檢測(cè)。當(dāng)前支持(中文、英語、日語、韓語)
  options.put("detect_language", "true");
  // 是否返回文字外接多邊形頂點(diǎn)位置,不支持單字位置。默認(rèn)為false
  options.put("vertexes_location", "false");
  // 是否返回識(shí)別結(jié)果中每一行的置信度
  options.put("probability", "false");
 
  // 可選:設(shè)置網(wǎng)絡(luò)連接參數(shù)
  client.setconnectiontimeoutinmillis(2000);
  client.setsockettimeoutinmillis(60000);
 
  // 調(diào)用接口
  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"));
  }
 
 }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/syy363250763/article/details/80843480

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久不射电影网 | 久久这里只有精品视频9 | 爱操综合网 | 91制片厂(果冻传媒)原档破解 | 全肉一女n男np高h乳 | 99热视频| 九九九国产在线 | 91这里只有精品 | 青青青国产在线观看 | 日本 片 成人 在线 日b视频免费 | 青青热久免费精品视频网站 | h在线动漫 | 亚洲国产成人久久99精品 | 国产精品免费一级在线观看 | free性俄罗斯护士 | 日韩在线二区全免费 | 精品国产一区二区三区久久久狼 | 色中文字幕 | 12-14娇小videos | 狐媚小说 | www青青草原| 热99精品只有里视频最新 | 99精品国产自在现线观看 | 久久亚洲高清观看 | 免费人成在线观看69式小视频 | 色帽子影院| 日韩一卡2卡3卡新区网站 | 教师系列 大桥未久在线 | 亚洲精品国产成人99久久 | 女人把扒开给男人爽 | 污污美女| 午夜神器老司机高清无码 | 国产成人免费片在线视频观看 | 金莲一级淫片aaaaaa | 无遮挡免费h肉动漫在线观看 | 国产日韩欧美精品在线 | 校园全黄h全肉细节文 | 国产成人综合久久精品红 | 美女的让男人桶爽30分钟的 | 丫鬟粗大狠狠贯穿h | 香蕉tv亚洲专区在线观看 |