一毁靶、百度云使用步驟
1.進入https://console.bce.baidu.com/#/index/overview 登錄百度云賬號鼠次。百度所有賬號都是通用的稠曼,可以直接利用百度的其他賬號(百度網(wǎng)盤葛账、百度貼吧等)進行登錄婚惫。沒有可以進行注冊椿胯。進入到如下界面筷登,登錄成功后,滑動到頁面最下邊哩盲。
首頁.png
2.選擇文字識別
文字識別.png
選中文字識別進入到如下頁面前方。
文字識別詳情.png
3.在應用列表下,創(chuàng)建應用
應用.png
這里需要注意种冬,如果你的是android或者Ios應用則需要把包名和應用名與你填寫的保持一致镣丑。
4.創(chuàng)建成功后就可以看到我們所需要的API Key和Secret Key 。
成功.png
百度云識別圖片文字所需要的API Key 和Secrect Key已經(jīng)獲取成功娱两,下面開始進行圖片文字識別的實現(xiàn)莺匠。
二、程序設計
1十兢、下載所需要的jar包趣竣,在百度云官網(wǎng)下載http://ai.baidu.com/sdk#ocr。選擇文字識別中的 java SDK 進行下載旱物。解壓后獲取到所需要的jar包遥缕。http://ai.baidu.com/docs#/OCR-Java-SDK/top 可參考該頁面的使用步驟。
api
2宵呛、新建一個java工程单匣。
初始化一個AipOcr
* 初始化AipOcr
* @param appId
* @param apiKey
* @param secretKey
* @return
*/
public static AipOcr getAipOCR(String appId,String apiKey,String secretKey){
AipOcr api = null;
if(appId == null || appId.trim().length() == 0
|| apiKey == null || apiKey.trim().length() == 0
|| secretKey == null || secretKey.trim().length() == 0)
{
logger.info("appID or apiKey or secretKey is error! ");
return api;
}
api = new AipOcr(appId, apiKey, secretKey);
return api;
}
通用文字識別的方法(識別本地圖片上的文字)
/**
* 識別本地圖片文字
* @param imgUrl
* @return
*/
public static String getOCRText(String imgUrl){
String ocrText = null;
String appId = "你的 App ID";
String apiKey = "你的 Api Key" ;
String secretKey = "你的 Secret Key";
AipOcr api = getAipOCR(appId, apiKey, secretKey);
if(api == null ){
logger.warn("api is null,unable to continue!");
return ocrText;
}
HashMap<String, String> options = new HashMap<String, String>();
options.put("detect_direction", "true");
options.put("probability", "true");
options.put("recognize_granularity", "big");
options.put("vertexes_location", "true");
JSONObject res = api.basicAccurateGeneral(imgUrl, options);
if(res == null || res.length() == 0){
ocrText = "There is no text in this picture.";
return ocrText;
}
JSONArray dataArray = res.getJSONArray("words_result");
System.out.println(dataArray);
JSONObject jsonData;
if(dataArray == null || dataArray.length() == 0){
ocrText = "There is no text in this picture.";
return ocrText;
}
for (int i = 0; i < dataArray.length(); i++) {
jsonData = dataArray.getJSONObject(i);
if(jsonData != null){
ocrText += jsonData.getString("words");
}else {
ocrText += "There is no text in this picture.";
}
}
return ocrText;
}
測試圖片
test1.png
測試結果
result1.png
識別網(wǎng)絡上的圖片 http://dimg04.c-ctrip.com/images/300n0y000000lykmiC927.jpg
/**
* 識別網(wǎng)絡圖片上的文字
* @param imgUrl
* @return
*/
public static String getOCR(String imgUrl){
String ocrWord="";
if(!imgUrl.startsWith("http"))
return ocrWord;
String appId = "你的 App ID";
String apiKey = "你的 Api Key" ;
String secretKey = "你的 Secret Key";
AipOcr api = getAipOCR(appId, apiKey, secretKey);
if(api == null ){
logger.warn("api is null,unable to continue!");
return ocrWord;
}
HashMap<String, String> options = new HashMap<String, String>();
options.put("detect_direction", "true");
options.put("detect_language", "true");
JSONObject res = api.webImageUrl(imgUrl, options );
JSONArray dataArray = res.getJSONArray("words_result");
JSONObject jsonData;
for (int i = 0; i < dataArray.length(); i++) {
jsonData = dataArray.getJSONObject(i);
if(jsonData != null)
ocrWord += jsonData.getString("words");
}
return ocrWord;
}
測試的圖片http://dimg04.c-ctrip.com/images/300n0y000000lykmiC927.jpg
測試結果
result2.png
不足之處請指出,謝謝
源碼鏈接:
鏈接:https://pan.baidu.com/s/1TMrVLUHYhyvHEbFq2T89bA 提取碼:8udn
參考資料:https://blog.csdn.net/xiaoxsen/article/details/80459971