java ocr 验证码_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()));
}
總結
以上是生活随笔為你收集整理的java ocr 验证码_Ocrking图片识别之Java实现本地验证码的识别 | 学步园的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Revit转点云pcd模型
- 下一篇: 列表播放列表播放