对于类似以下简单的验证码的识别方案:

1、

2

3

4、

1、建库:切割验证码为单个字符,人工标记,比如:A。

2、识别:给一个验证码:切割为单个字符,在库中查询识别。

/***
* author:chzeze
* 识别验证码并返回
* train_path 验证码字母图库位置
* 验证码图片缓存位置:Configuration.getProperties("web_save_path")+"/captcha.jpg"
*/
public class AmGetCaptchaTest {
private static Logger logger = Logger.getLogger(AmGetCaptchaTest.class);
private static String train_path = "/data/sata/share_sata/AmazonCrawl/amazonWeb/captcha";
private static Map<BufferedImage, String> trainMap = null;
private static int index = 0;
private static int imgnum = 0;
private static MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
private static HttpClient client = new HttpClient(httpConnectionManager);
/* static {
//每主机最大连接数和总共最大连接数,通过hosfConfiguration设置host来区分每个主机
client.getHttpConnectionManager().getParams().setDefaultMaxConnectionsPerHost(8);
client.getHttpConnectionManager().getParams().setMaxTotalConnections(48);
client.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
client.getHttpConnectionManager().getParams().setSoTimeout(10000);
client.getHttpConnectionManager().getParams().setTcpNoDelay(true);
client.getHttpConnectionManager().getParams().setLinger(1000);
//失败的情况下会进行3次尝试,成功之后不会再尝试
client.getHttpConnectionManager().getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
}*/
public static int isBlack(int colorInt) {
Color color = new Color(colorInt);
if (color.getRed() + color.getGreen() + color.getBlue() <= 100) {
return 1;
}
return 0;
} public static int isWhite(int colorInt) {
Color color = new Color(colorInt);
if (color.getRed() + color.getGreen() + color.getBlue() > 600) {
return 1;
}
return 0;
} public static BufferedImage removeBackgroud(String picFile)
throws Exception {
BufferedImage img = ImageIO.read(new File(picFile));
img = img.getSubimage(1, 1, img.getWidth() - 2, img.getHeight() - 2);
int width = img.getWidth();
int height = img.getHeight();
double subWidth = width / 5.0;
for (int i = 0; i < 5; i++) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int x = (int) (1 + i * subWidth); x < (i + 1) * subWidth
&& x < width - 1; ++x) {
for (int y = 0; y < height; ++y) {
if (isWhite(img.getRGB(x, y)) == 1)
continue;
if (map.containsKey(img.getRGB(x, y))) {
map.put(img.getRGB(x, y), map.get(img.getRGB(x, y)) + 1);
} else {
map.put(img.getRGB(x, y), 1);
}
}
}
int max = 0;
int colorMax = 0;
for (Integer color : map.keySet()) {
if (max < map.get(color)) {
max = map.get(color);
colorMax = color;
}
}
for (int x = (int) (1 + i * subWidth); x < (i + 1) * subWidth
&& x < width - 1; ++x) {
for (int y = 0; y < height; ++y) {
if (img.getRGB(x, y) != colorMax) {
img.setRGB(x, y, Color.WHITE.getRGB());
} else {
img.setRGB(x, y, Color.BLACK.getRGB());
}
}
}
}
return img;
} public static BufferedImage removeBlank(BufferedImage img) throws Exception {
int width = img.getWidth();
int height = img.getHeight();
int start = 0;
int end = 0;
Label1: for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
if (isBlack(img.getRGB(x, y)) == 1) {
start = y;
break Label1;
}
}
}
Label2: for (int y = height - 1; y >= 0; --y) {
for (int x = 0; x < width; ++x) {
if (isBlack(img.getRGB(x, y)) == 1) {
end = y;
break Label2;
}
}
}
return img.getSubimage(0, start, width, end - start + 1);
} public static List<BufferedImage> splitImage(BufferedImage img)
throws Exception {
List<BufferedImage> subImgs = new ArrayList<BufferedImage>();
int width = img.getWidth();
int height = img.getHeight();
List<Integer> weightlist = new ArrayList<Integer>();
for (int x = 0; x < width; ++x) {
int count = 0;
for (int y = 0; y < height; ++y) {
if (isBlack(img.getRGB(x, y)) == 1) {
count++;
}
}
weightlist.add(count);
}
for (int i = 0; i < weightlist.size();i++) {
int length = 0;
while (i < weightlist.size() && weightlist.get(i) > 0) {
i++;
length++;
}
if (length > 2) {
subImgs.add(removeBlank(img.getSubimage(i - length, 0,
length, height)));
}
}
return subImgs;
} public static Map<BufferedImage, String> loadTrainData() throws Exception {
if (trainMap == null) {
Map<BufferedImage, String> map = new HashMap<BufferedImage, String>();
File dir = new File(train_path);
File[] files = dir.listFiles();
for (File file : files) {
map.put(ImageIO.read(file), file.getName().charAt(0) + "");
}
trainMap = map;
}
return trainMap;
} public static String getSingleCharOcr(BufferedImage img,
Map<BufferedImage, String> map) {
String result = "#";
int width = img.getWidth();
int height = img.getHeight();
int min = width * height;
for (BufferedImage bi : map.keySet()) {
int count = 0;
if (Math.abs(bi.getWidth()-width) > 2)
continue;
int widthmin = width < bi.getWidth() ? width : bi.getWidth();
int heightmin = height < bi.getHeight() ? height : bi.getHeight();
Label1: for (int x = 0; x < widthmin; ++x) {
for (int y = 0; y < heightmin; ++y) {
if (isBlack(img.getRGB(x, y)) != isBlack(bi.getRGB(x, y))) {
count++;
if (count >= min)
break Label1;
}
}
}
if (count < min) {
min = count;
result = map.get(bi);
}
}
return result;
} public static String getAllOcr(String file) throws Exception {
BufferedImage img = removeBackgroud(file);//去除重影
List<BufferedImage> listImg = splitImage(img);//切割图片
Map<BufferedImage, String> map = loadTrainData();
String result = "";
for (BufferedImage bi : listImg) {
result += getSingleCharOcr(bi, map);
}
//ImageIO.write(img, "JPG", new File("result6\\" + result + ".jpg"));
return result;
}
/***
* 下载验证码图片暂时保存供识别程序使用
* @param imgurl 验证码图片url
*/
public static void downloadimg(String imgurl)
{
//HttpClient httpClient = new HttpClient(); //httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
//httpClient.getHttpConnectionManager().getParams().setSoTimeout(10000);
GetMethod getMethod = new GetMethod(imgurl);
try {
int statusCode = client.executeMethod(getMethod);
System.out.println(statusCode);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("("+statusCode+")Method failed: "+ getMethod.getStatusLine());
logger.info("("+statusCode+")Method failed: "+ getMethod.getStatusLine());
}
InputStream inputStream = getMethod.getResponseBodyAsStream();
OutputStream outStream = new FileOutputStream("/data/sata/share_sata/AmazonCrawl/amazonWeb/captcha.jpg");
IOUtils.copy(inputStream, outStream);
inputStream.close();
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
//logger.info(new Date()+"captcha appear exception:"+e.getMessage());
try {
//若遇到异常则睡眠20秒后继续重试
Thread.sleep(20000);
} catch (InterruptedException e1) {
logger.error(e1);
}
e.printStackTrace();
}finally {
getMethod.releaseConnection();
}
}
/***
* 抽取页面验证码并返回
* @param stringBuffer
* @return 验证码字符串
*/
public static String GetCaptcha(StringBuilder html){
String captcha_str="######";//未识别则为#
Document doc = Jsoup.parse(html.toString());
String imgurl = doc.select("div[class=a-row a-text-center]").get(0).child(0).attr("src");
//System.out.println(imgurl);
downloadimg(imgurl);
try {
captcha_str = getAllOcr("/data/sata/share_sata/AmazonCrawl/amazonWeb/captcha.jpg");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return captcha_str;
}
}

后记:复杂验证码识别

对于复杂的验证码识别:目前的最简单的方案就是交给第三方人工打码平台:可以参考我做的EBay多线程打码兔验证码解决方案:

http://www.cnblogs.com/zeze/p/6402963.html

更专业的可以采用机器学习、模式识别等方法去实现,但是识别成功率,我目前测试的结果不是很理想,复杂的验证码,正确率在百分之二三十上下,但是我的训练样本库不是很大,提高训练的样本可能结果会好一点。

【Java】验证码识别解决方案的更多相关文章

  1. Java验证码识别解决方案

    建库,去重,切割,识别. package edu.fzu.ir.test; import java.awt.Color; import java.awt.image.BufferedImage; im ...

  2. Java 验证码识别库 Tess4j 学习

    Java 验证码识别库 Tess4j 学习 [在用java的Jsoup做爬虫爬取数据时遇到了验证码识别的问题(基于maven),找了网上挺多的资料,发现Tess4j可以自动识别验证码,在这里简单记录下 ...

  3. python之web自动化验证码识别解决方案

    验证码识别解决方案 对于web应用程序来讲,处于安全性考虑,在登录的时候,都会设置验证码,验证码的类型种类繁多,有图片中辨别数字字母的,有点击图片中指定的文字的,也有算术计算结果的,再复杂一点就是滑动 ...

  4. Java 验证码识别之多线程打码兔

    验证码识别,爬虫永远的话题~ 用打码兔总体的体验就是单线程速度太慢~ import java.io.IOException; import java.net.MalformedURLException ...

  5. JAVA验证码识别:基于jTessBoxEditorFX和Tesseract-OCR训练样本

    JAVA验证识别:基于jTessBoxEditorFX和Tesseract-OCR训练样本 工具准备: jTessBoxEditorFX下载:https://github.com/nguyenq/jT ...

  6. java验证码识别

    首先参考了csdn大佬的文章,但是写的不全ImgUtils类没有给出代码,无法进行了 写不完整就是制造垃圾 不过这个大佬又说这个大佬的文章值得参考于是又查看这篇文章 有案例https://blog.c ...

  7. atitit.验证码识别step3----去除边框---- 图像处理类库 attilax总结java版本

    atitit.验证码识别step3----去除边框---- 图像处理类库 attilax总结java版本 1. 去除边框思路原理 1 2. Thumbnailator 是一个用来生成图像缩略图.裁切. ...

  8. Python&selenium&tesseract自动化测试随机码、验证码(Captcha)的OCR识别解决方案参考

    在自动化测试或者安全渗透测试中,Captcha验证码的问题经常困扰我们,还好现在OCR和AI逐渐发展起来,在这块解决上越来越支撑到位. 我推荐的几种方式,一种是对于简单的验证码,用开源的一些OCR图片 ...

  9. atitit  验证码理论与概览与 验证码规范 解决方案.docx

    atitit  验证码理论与概览与 验证码规范 解决方案.docx 1.1. 验证码的前世今生1 1.2. 第三代:无知识型验证码1 1.3. 短信验证码1 1.4. 语言验证码1 1.5. 图片验证 ...

随机推荐

  1. 数学图形(1.41)super spiral超级螺线

    一种很酷的螺线,看着有种分形学的感觉.参考自http://www.2dcurves.com/spiral/spirallos.html 其数学的极坐标表达式如下: 我的脚本代码如下: #http:// ...

  2. 【Networking】Libevent客户端例子

    [原]Libevent客户端例子 时间 -- :: luotuo44的专栏 原文 http://blog.csdn.net/luotuo44/article/details/34416429 主题 l ...

  3. zedboard--嵌入式网络摄像机(mjpg-streamer)的移植和搭建(二十二)

    在zedboard上移植和搭建嵌入式网络摄像机mjpg-streamer.具体步骤如下: 来自:http://write.blog.csdn.net/postedit/13741451 1.安装lib ...

  4. Cognos权限认证CJP方式之用户密码加密

    在项目开发过程中,用户往往对系统的安全都有明确的要求,下面针对cognos门户认证用户密码如何加密来提供一个简单的wf 1Cognos权限认证方式:CJP 2Cognos用户数据库类型:Oracle ...

  5. Android多点触控技术,实现对图片的放大缩小平移,惯性滑动等功能

    首先推荐一下鸿洋大大的打造个性的图片预览与多点触控视频教程,这套教程教我们一步一步实现了多点触控实现对图片的平移和缩放的功能.这篇文章我将在鸿洋大大的基础之上做了一些扩展功能: 1.图片的惯性滑动 2 ...

  6. Sqlserver获取行号

    Sqlserver获取行号   select row_number()over(order by userid )as RowNum,*from OUM_User

  7. Unity WP8开发环境

    Unity WP8开发环境   VS2012旗舰版: 安装WP SDK8.0出错提示: 根据当前系统时钟或签名文件中的时间戳验证时要求的证书不在有效期内 解决办法: 方法一:把操作系统的时间日期调整到 ...

  8. Win8多平台引用配置

    之前移植过DLNA的库,这个库是C++写的,然后我们的项目是C#的.接着很郁闷的事情发生了,主项目引用一个C#的DLL,然后这个DLL引用这个C++/CX封装的库.如果有C++的源代码的话,做项目依赖 ...

  9. Appium环境的安装与配置,Python测试脚本测试

    Appium自动化测试系列1 - Appium环境的安装与配置 发表于4个月前(2015-01-27 14:34)   阅读(803) | 评论(0) 0人收藏此文章, 我要收藏 赞0 寻找 会’偷懒 ...

  10. spring mvc 返回json的配置

    转载自:http://my.oschina.net/haopeng/blog/324934 springMVC-servlet.xml 配置 1 2 3 4 5 6 7 8 9 10 11 12 13 ...