java ocr 验证码_Ocrking图片识别之Java实现本地验证码的识别 | 学步园
本實(shí)例代碼為Java實(shí)現(xiàn)本地驗(yàn)證碼的識(shí)別
依賴庫為 httpclient-4.2 使用最新的庫 需要修改部分代碼
Author: niostars@gmail.com
//構(gòu)造一個(gè)httpclient
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
//設(shè)置請求頭?? 經(jīng)過測試 如果需要傳入?yún)?shù) 同時(shí)要上傳文件 http頭只能設(shè)置為如下代碼 或者不予設(shè)置
//?? ??? ?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");
//實(shí)例化一個(gè)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 建議使用上面的代碼進(jìn)行設(shè)置*/
//?? ??? ??? ?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()));
}
總結(jié)
以上是生活随笔為你收集整理的java ocr 验证码_Ocrking图片识别之Java实现本地验证码的识别 | 学步园的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Revit转点云pcd模型
- 下一篇: java美元兑换,(Java实现) 美元