溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2020-09-16 13:35:23 來(lái)源:腳本之家 閱讀:126 作者:syy363250763 欄目:編程語(yǔ)言

本文實(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í)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI