直接上源碼夸浅,這是一個(gè)收費(fèi)的且不開源的庫又憨,測(cè)試效果也不太理想
親測(cè)效果:
- 對(duì)于相同字體牛柒。非傾斜的,比如http://dz.bjjtgl.gov.cn/service/checkCode.do滓技,識(shí)別率還是挺高的;
- 對(duì)于斜體或者其他變異的病苗,如下代碼識(shí)別率就很低了惩嘉,可能需要調(diào)整識(shí)別引擎的參數(shù)了,大家自己查找測(cè)試下乱灵。
- 這個(gè)測(cè)試版本的塑崖,會(huì)有彈出對(duì)話框 框框~~
package com.example;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import javax.imageio.ImageIO;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.io.IOUtils;
import com.asprise.ocr.Ocr;
public class ReadImg {
public static void main(String[] args) throws IOException {
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod("http://dz.bjjtgl.gov.cn/service/checkCode.do");
// GetMethod getMethod = new GetMethod("https://dynamic.12306.cn/otsweb/passCodeAction.do?rand=sjrand");
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + getMethod.getStatusLine());
return ;
}
String picName = "F:\\img\\";
File filepic=new File(picName);
if(!filepic.exists())
filepic.mkdir();
File filepicF=new File(picName+new Date().getTime() + ".jpg");
InputStream inputStream = getMethod.getResponseBodyAsStream();
OutputStream outStream = new FileOutputStream(filepicF);
IOUtils.copy(inputStream, outStream);
outStream.close();
Ocr.setUp(); // one time setup
Ocr ocr = new Ocr(); // create a new OCR engine
ocr.startEngine("eng", Ocr.SPEED_FASTEST); // English
String s = ocr.recognize(new File[] {filepicF},Ocr.RECOGNIZE_TYPE_TEXT, Ocr.OUTPUT_FORMAT_PLAINTEXT);
System.out.println("Result: " + s);
System.out.println("num is:" + s.replace(",", "").replace("i", "1").replace(" ", "").replace("'", "").replace("o", "0").replace("O", "0").replace("g", "6").replace("B", "8").replace("s", "5").replace("z", "2"));
// ocr more images here ...
ocr.stopEngine();
}
}
需要的java包
注意:主要的jar包
- aocr.jar -- 去Asprise官網(wǎng)下載最新jar包
- commons-codec.jar
- commons-httpclient-3.1.jar
- commons-io.jar
- commons-logging-1.0.4.jar