Ocrking圖片識別之Java實現本地驗證碼的識別
阿新 • • 發佈:2019-01-10
本例項程式碼為Java實現本地驗證碼的識別
依賴庫為 httpclient-4.2 使用最新的庫 需要修改部分程式碼
Author: [email protected]
//構造一個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()));
}
依賴庫為 httpclient-4.2 使用最新的庫 需要修改部分程式碼
Author: [email protected]
//構造一個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()));
}