生活随笔
收集整理的這篇文章主要介紹了
如何利用百度ocr实现验证码自动识别
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在爬取網(wǎng)站的時候都遇到過驗證碼,那么我們有什么方法讓程序自動的識別驗證碼呢?其實網(wǎng)上已有很多打碼平臺,但是這些都是需要money。但對于僅僅爬取點數(shù)據(jù)而接入打碼平臺實屬浪費。所以百度免費ocr正好可以利用。(每天500次免費)
1、注冊百度賬號、百度云管理中心創(chuàng)建應(yīng)用、生成AppKey、SecretKey(程序調(diào)用接口是要生成access_token)
?
2、利用AppKey、SecretKey生成access_token
向授權(quán)服務(wù)地址https://aip.baidubce.com/oauth/2.0/token發(fā)送請求(推薦使用POST)并在URL中帶上以下參數(shù):
grant_type: 必須參數(shù),固定為client_credentials;
client_id: 必須參數(shù),應(yīng)用的API Key;
client_secret: 必須參數(shù),應(yīng)用的Secret Key
代碼如下:
/*** 獲取AccessToken* 百度開發(fā)* AppId:* APIKey:* SecretKey:** @return*/public static String getAccessToken() {String accessToken = ""
;HttpRequestData httpRequestData =
new HttpRequestData();HashMap<String, String> params =
new HashMap<>
();params.put("grant_type", "client_credentials"
);params.put("client_id", "xxxxxx"
);params.put("client_secret", "xxxxxx"
);httpRequestData.setRequestMethod("GET"
);httpRequestData.setParams(params);httpRequestData.setRequestUrl("https://aip.baidubce.com/oauth/2.0/token"
);HttpResponse response =
HttpClientUtils.execute(httpRequestData);String json = ""
;try {json =
IOUtils.toString(response.getEntity().getContent());} catch (IOException e) {e.printStackTrace();}if (response.getStatusLine().getStatusCode() == 200
) {JSONObject jsonObject =
JSONObject.parseObject(json);if (jsonObject !=
null && !
jsonObject.isEmpty()) {accessToken = jsonObject.getString("access_token"
);}}return accessToken;} ?
3、請求百度ocr通用文字識別API(下面以百度通用識別api識別為例)
請求API的URL https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic
請求方法 POST
請求URL參數(shù) access_token
請求頭 (Header) Content-Type application/x-www-form-urlencoded
Body中放置請求參數(shù),主要參數(shù)詳情如下:
- ?image : 圖像數(shù)據(jù),base64編碼,要求base64編碼后大小不超過4M,最短邊至少15px,最長邊最大4096px,支持jpg/png/bmp格式,當(dāng)image字段存在時url字段失效
- url : 圖片完整URL,URL長度不超過1024字節(jié),URL對應(yīng)的圖片base64編碼后大小不超過4M,最短邊至少15px,最長邊最大4096px,支持jpg/png/bmp格式,當(dāng)image字段存在時url字段失效
/*** 獲取識別驗證碼* @param imageUrl* @return*/public static String OCRVCode(String imageUrl){String VCode = ""
;if (StringUtils.isBlank(ACCESS_TOKEN)) {logger.error("accessToken為空"
);return VCode;}OCRUrl = OCRUrl + "?access_token=" +
ACCESS_TOKEN;HashMap<String, String> headers =
new HashMap<>
();headers.put("Content-Type", "application/x-www-form-urlencoded"
);HashMap<String, String> params =
new HashMap<>
();imageUrl =
ImageBase64ToStringUtils.imageToStringByBase64(imageUrl);params.put("image"
, imageUrl);HttpRequestData httpRequestData =
new HttpRequestData();httpRequestData.setHeaders(headers);httpRequestData.setRequestMethod("post"
);httpRequestData.setParams(params);httpRequestData.setRequestUrl(OCRUrl);HttpResponse response =
HttpClientUtils.execute(httpRequestData);String json = ""
;if (response.getStatusLine().getStatusCode() == 200
) {try {json =
IOUtils.toString(response.getEntity().getContent());JSONObject jsonObject =
JSONObject.parseObject(json);JSONArray wordsResult = jsonObject.getJSONArray("words_result"
);VCode = wordsResult.getJSONObject(0).getString("words"
);} catch (IOException e) {logger.error("請求識別失敗!"
, e);}}return VCode;} ?
對圖片進行base64編碼字符
/*** 將本地圖片進行Base64位編碼* @param imageFile* @return*/public static String encodeImgageToBase64(String imageFile) {// 其進行Base64編碼處理byte[] data =
null;// 讀取圖片字節(jié)數(shù)組try {InputStream in =
new FileInputStream(imageFile);data =
new byte[in.available()];in.read(data);in.close();} catch (IOException e) {e.printStackTrace();}// 對字節(jié)數(shù)組Base64編碼return Base64Util.encode(data);} ?
4、返回結(jié)果以json方式返回
{"log_id": 2471272194
,"words_result_num": 2
,"words_result"
: [{"words": " TSINGTAO"
},{"words": "青島睥酒"
}]
} ?
項目github地址:https://github.com/xwlmdd/ipProxyPool
注:ocr圖片識別模塊在這個項目里的一個工具類
我的公眾號,喜歡的朋友可以關(guān)注哦
轉(zhuǎn)載于:https://www.cnblogs.com/xwlhyy1072552712/p/7470985.html
總結(jié)
以上是生活随笔為你收集整理的如何利用百度ocr实现验证码自动识别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。