java實(shí)現(xiàn)百度云文字識別接口代碼
本文實(shí)例為大家分享了java實(shí)現(xiàn)百度云文字識別的接口具體代碼,供大家參考,具體內(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有過期時(shí)間, 客戶端可自行緩存,過期后重新獲取。 */ 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為固定參數(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; } }
測試:
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文字識別功能,作者是:笑釋一切。
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的官網(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"); // 識別語言類型,默認(rèn)為CHN_ENG??蛇x值包括: // CHN_ENG:中英文混合; // ENG:英文; // POR:葡萄牙語; // FRE:法語; // GER:德語; // ITA:意大利語; // SPA:西班牙語; // RUS:俄語; // JAP:日語; // KOR:韓語; options.put("language_type", "CHN_ENG"); // 是否檢測圖像朝向,默認(rèn)不檢測,即:false。朝向是指輸入圖像是正常方向、逆時(shí)針旋轉(zhuǎn)90/180/270度。 options.put("detect_direction", "true"); // 是否檢測語言,默認(rèn)不檢測。當(dāng)前支持(中文、英語、日語、韓語) options.put("detect_language", "true"); // 是否返回文字外接多邊形頂點(diǎn)位置,不支持單字位置。默認(rèn)為false options.put("vertexes_location", "false"); // 是否返回識別結(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")); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot實(shí)現(xiàn)簡易支付寶網(wǎng)頁支付功能
小編最近實(shí)現(xiàn)一個(gè)功能基于springboot程序的支付寶支付demo,非常不錯(cuò)適合初學(xué)者入門學(xué)習(xí)使用,今天把SpringBoot實(shí)現(xiàn)簡易支付寶網(wǎng)頁支付功能的示例代碼分享給大家,感興趣的朋友參考下吧2021-10-10SpringBoot服務(wù)上實(shí)現(xiàn)接口限流的方法
這篇文章主要介紹了SpringBoot服務(wù)上實(shí)現(xiàn)接口限流的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10Spring Boot啟動時(shí)調(diào)用自己的非web邏輯
在spring Boot中,有些代碼是WEB功能,例如API等,但是有些邏輯是非WEB,啟動時(shí)就要調(diào)用并持續(xù)運(yùn)行的,該如何加載自己的非WEB邏輯呢,下面通過實(shí)例代碼給大家講解,一起看看吧2017-07-07java編程經(jīng)典案例之基于斐波那契數(shù)列解決兔子問題實(shí)例
這篇文章主要介紹了java編程經(jīng)典案例之基于斐波那契數(shù)列解決兔子問題,結(jié)合完整實(shí)例形式分析了斐波那契數(shù)列的原理及java解決兔子問題的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10Java 反射調(diào)用靜態(tài)方法的簡單實(shí)例
下面小編就為大家?guī)硪黄狫ava 反射調(diào)用靜態(tài)方法的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06java實(shí)現(xiàn)在SSM下使用支付寶掃碼支付功能
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)在SSM下使用支付寶掃碼支付功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02