爬虫中遇到登录验证码,java 如何识别图片验证码

逃离我推掉我的手 2022-02-28 06:44 714阅读 0赞

https://blog.csdn.net/persistencegoing/article/details/84376427

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();

  1. Ocr.setUp(); // one time setup
  2. Ocr ocr = new Ocr(); // create a new OCR engine
  3. ocr.startEngine("eng", Ocr.SPEED\_FASTEST); // English
  4. String s = ocr.recognize(new File\[\] \{filepicF\},Ocr.RECOGNIZE\_TYPE\_TEXT, Ocr.OUTPUT\_FORMAT\_PLAINTEXT);
  5. System.out.println("Result: " + s);
  6. 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"));
  7. // ocr more images here ...
  8. 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

希望大家关注我一波,防止以后迷路,有需要的可以加群讨论互相学习java ,学习路线探讨,经验分享与java求职

群号:721 515 304

发表评论

表情:
评论列表 (有 0 条评论,714人围观)

还没有评论,来说两句吧...

相关阅读

    相关 登录图片验证

    页面刷新时,自动发送获取验证码到服务端 输入验证码登录,登录成功返回token 通过token获取用户详情,跳转主页 //获取验证码图片 @GetMappi

    相关 网络爬虫验证识别

    网络爬虫遇到的验证码 在写网络,爬虫时,遇到很多网站存在验证码的情形,有其是比较烦的是,爬取数据的每一页都有验证码,如果只有登陆时,存在验证码,这个很好解决,只需将验证码