java實(shí)現(xiàn)百度云文字識(shí)別接口代碼
本文實(shí)例為大家分享了java實(shí)現(xiàn)百度云文字識(shí)別的接口具體代碼,供大家參考,具體內(nèi)容如下
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有過(guò)期時(shí)間, 客戶端可自行緩存,過(guò)期后重新獲取。
*/
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);
// 打開(kāi)和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輸入流來(lái)讀取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è)試:
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有過(guò)期時(shí)間, 客戶端可自行緩存,過(guò)期后重新獲取。
*//*
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í)別功能,作者是:笑釋一切。
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>
* 打開(kāi)百度云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í)別語(yǔ)言類型,默認(rèn)為CHN_ENG??蛇x值包括:
// CHN_ENG:中英文混合;
// ENG:英文;
// POR:葡萄牙語(yǔ);
// FRE:法語(yǔ);
// GER:德語(yǔ);
// ITA:意大利語(yǔ);
// SPA:西班牙語(yǔ);
// RUS:俄語(yǔ);
// JAP:日語(yǔ);
// KOR:韓語(yǔ);
options.put("language_type", "CHN_ENG");
// 是否檢測(cè)圖像朝向,默認(rèn)不檢測(cè),即:false。朝向是指輸入圖像是正常方向、逆時(shí)針旋轉(zhuǎn)90/180/270度。
options.put("detect_direction", "true");
// 是否檢測(cè)語(yǔ)言,默認(rèn)不檢測(cè)。當(dāng)前支持(中文、英語(yǔ)、日語(yǔ)、韓語(yǔ))
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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java靜態(tài)static關(guān)鍵字原理詳解
這篇文章主要介紹了Java靜態(tài)static關(guān)鍵字原理詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
Spring?Boot在啟動(dòng)時(shí)執(zhí)行一次的功能實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于Spring?Boot在啟動(dòng)時(shí)執(zhí)行一次的功能實(shí)現(xiàn),在實(shí)習(xí)過(guò)程中,有時(shí)候會(huì)遇到一些項(xiàng)目啟動(dòng)初始化的需求,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08
詳解Java集合類之HashTable,Properties篇
這篇文章主要為大家詳細(xì)介紹一下Java集合類中HashTable和Properties的用法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定幫助,感興趣的可以了解一下2022-07-07
Java中內(nèi)部類使用方法實(shí)戰(zhàn)案例分析
這篇文章主要介紹了Java中內(nèi)部類使用方法,結(jié)合具體案例形式分析了Java內(nèi)部類原理、調(diào)用方法及相關(guān)使用注意事項(xiàng),需要的朋友可以參考下2019-09-09
mybatis的大于小于號(hào)轉(zhuǎn)義符號(hào)一覽
這篇文章主要介紹了mybatis的大于小于號(hào)轉(zhuǎn)義符號(hào)一覽,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java并發(fā)編程中的CyclicBarrier使用解析
這篇文章主要介紹了Java并發(fā)編程中的CyclicBarrier使用解析,CyclicBarrier從字面意思上來(lái)看,循環(huán)柵欄,這篇文章就來(lái)分析下是到底是如何實(shí)現(xiàn)循環(huán)和柵欄的,需要的朋友可以參考下2023-12-12
解決poi導(dǎo)出excel無(wú)法打開(kāi)文件的問(wèn)題
這篇文章主要介紹了解決poi導(dǎo)出excel無(wú)法打開(kāi)文件的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12

