Ocrking图片识别之Java实现本地验证码的识别
生活随笔
收集整理的這篇文章主要介紹了
Ocrking图片识别之Java实现本地验证码的识别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近一個項目需要用到驗證碼的識別,網上找到了Ocrking的識別平臺,就拿來使用一下。平臺為Java一下是實現方式:
轉載請寫明出處:
本實例代碼為Java實現本地驗證碼的識別
依賴庫為 httpclient-4.2 使用最新的庫 需要修改部分代碼
Author: niostars@gmail.com
?? ???? //構造一個httpclient
?? ??? ?HttpClient client = new DefaultHttpClient();
?? ??? ?HttpPost post = new HttpPost(URL);
?? ??? ?
?? ??? ?//設置請求頭?? 經過測試 如果需要傳入參數 同時要上傳文件 http頭只能設置為如下代碼 或者不予設置
//?? ??? ?post.addHeader("Accept", "*/*");
//?? ??? ?post.addHeader("Referer", "http://lab.ocrking.com");
//?? ??? ?post.addHeader("Accept-Encoding", "gzip");
//?? ??? ?post.addHeader("Accept-Language", "zh-cn,zh,en");
//?? ??? ?post.addHeader("Host", "lab.ocrking.com");
//?? ??? ?post.addHeader("Connection", "Keep-Alive");
?? ??? ?//實例化一個MultipartEntity
?? ??? ?MultipartEntity entity = new MultipartEntity();
?? ??? ?
?? ??? ?try {
?? ??? ??? ?
?? ??? ?File file = new File("C:\\getcodeimage.jpg"); ?
?? ??? ?entity.addPart("url", new StringBody("","text/plain",Charset.forName("UTF-8")));
?? ??? ?entity.addPart("service", new StringBody("OcrKingForCaptcha","text/plain",Charset.forName("UTF-8")));
?? ??? ?entity.addPart("language", new StringBody("eng","text/plain",Charset.forName("UTF-8")));
?? ??? ?entity.addPart("charset", new StringBody("7","text/plain",Charset.forName("UTF-8")));
?? ??? ?entity.addPart("apiKey", new StringBody(apiKey,"text/plain",Charset.forName("UTF-8")));
?? ??? ?entity.addPart("type", new StringBody(type,"text/plain",Charset.forName("UTF-8")));
?? ??? ??? ?
?? ??? ??? ?/*addPart 建議使用上面的代碼進行設置*/
//?? ??? ??? ?entity.addPart("url", new StringBody(""));
//?? ??? ??? ?entity.addPart("service", new StringBody("OcrKingForCaptcha"));
//?? ??? ??? ?entity.addPart("language", new StringBody("eng"));
//?? ??? ??? ?entity.addPart("charset", new StringBody("7"));
//?? ??? ??? ?entity.addPart("apiKey", new StringBody(apiKey));
//?? ??? ??? ?entity.addPart("type", new StringBody(type));
?? ??? ??? ?
?? ??? ??? ?/*加入文件*/
?? ??? ??? ?entity.addPart("filename",new FileBody(file));
?? ??? ?} catch (Exception e1) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e1.printStackTrace();
?? ??? ?}
?? ??? ?
?? ??? ?
?? ??? ?try {
?? ??? ??? ?post.setEntity(entity);
?? ??? ??? ?System.out.println("executing request="+post.getRequestLine());
?? ??? ??? ?HttpResponse response = client.execute(post);
?? ??? ??? ?
?? ??? ??? ?System.out.println("code="+response.getStatusLine().getStatusCode());
?? ??? ??? ?System.out.println(EntityUtils.toString(response.getEntity()));
?? ??? ??? ?}
總結
以上是生活随笔為你收集整理的Ocrking图片识别之Java实现本地验证码的识别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu14.04 使用keepas
- 下一篇: java美元兑换,(Java实现) 美元