Java 识别图片验证码

package com.demo.bobo;

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;
import com.asprise.util.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("图片文字为:" + 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();
	}
}

注意:主要的jar包

1.aocr.jar    -- 去Asprise官网下载最新jar包 http://asprise.com/royalty-free-library/java-ocr-for-windows-mac-linux-download.html

2.commons-codec.jar

3.commons-httpclient-3.1.jar

4.commons-io.jar

5.commons-logging-1.0.4.jar

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值