webdriver结合tesseract-ocr处理简单验证码
来源:互联网 发布:淘宝客服打字要多快 编辑:程序博客网 时间:2024/05/17 02:37
使用OCR自动化识别,一般识别率不是太高,处理一般简单验证码还是没问题,这里使用的是Tesseract-OCR,下载地址:http://pan.baidu.com/s/1kUGaw8R
怎么使用呢?
首先,环境变量path添加tesseract-ocr的安装路径,然后使用命令窗口查看:
如果出现如上输出,表示安装正常。
我准备了一张验证码cp.png放在e盘tesseract目录下:结果为:
现在,具体实践,先准备一份网页:
<html><head><title>验证码</title></head><body><form><td>验证码:</td><input id="cp" type="text"/><img src="http://www.csti.cn/uc/index/verify.htm"></form></body></html>
要识别验证码,首先得取得验证码,首先获取整个页面的截图,然后找到页面元素坐标进行截取:
//元素截图public static void captureElement(WebDriver driver, WebElement element, String path){// 截图整个页面File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);try {// 获得元素的高度和宽度int width = element.getSize().getWidth();int height = element.getSize().getHeight();// 创建一个矩形使用上面的高度和宽度Rectangle rect = new Rectangle(width, height);// 得到元素的坐标Point p = element.getLocation();BufferedImage img = ImageIO.read(srcFile);BufferedImage dest = img.getSubimage(p.getX(), p.getY(), rect.width,rect.height);// 存为png格式ImageIO.write(dest, "png", srcFile);Thread.sleep(1000);FileUtils.copyFile(srcFile, new File(path));} catch (Exception e) {e.printStackTrace();}}
截取完元素,就可以调用Tesseract-OCR生成text:
Runtime rt = Runtime.getRuntime();rt.exec("cmd.exe /C tesseract e:\\tesseract\\cp.png e:\\tesseract\\cp");接下来通过java读取txt:
public static String readTextFile(String filePath) {String lineTxt = null; try { String encoding = "GBK"; File file = new File(filePath); if (file.isFile() && file.exists()) { // 判断文件是否存在 InputStreamReader read = new InputStreamReader( new FileInputStream(file), encoding);// 考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); while ((lineTxt = bufferedReader.readLine()) != null) { return lineTxt; } read.close(); } else { System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); }return lineTxt;}最后,直接调用:
public static void main(String[] args) throws IOException, InterruptedException {WebDriver driver = new FirefoxDriver();driver.manage().window().maximize();driver.get("file:///E:/tesseract/cp.html");WebElement cp = driver.findElement(By.xpath("//img"));captureElement(driver, cp, "e:\\tesseract\\cp.png");Runtime rt = Runtime.getRuntime(); rt.exec("cmd.exe /C tesseract e:\\tesseract\\cp.png e:\\tesseract\\cp"); Thread.sleep(1000); String cp2 = readTextFile("e:\\tesseract\\cp.txt"); driver.findElement(By.id("cp")).sendKeys(cp2); //driver.quit();}效果如下:
0 0
- webdriver结合tesseract-ocr处理简单验证码
- webdriver结合tesseract-ocr处理简单验证码
- Webdriver配合Tesseract-OCR 自动识别简单的验证码
- imagemagick tesseract-ocr破解简单验证码
- tesseract-ocr破解 验证码
- Tesseract-OCR 验证码训练
- 用imagemagick和tesseract-ocr破解简单验证码
- 关于tesseract-ocr识别验证码
- 使用tesseract-ocr破解网站验证码
- C#使用Tesseract OCR 解析验证码
- Tesseract-OCR引擎识别验证码
- 使用tesseract-ocr破解网站验证码
- C#使用Tesseract OCR 解析验证码
- C#使用Tesseract OCR 解析验证码
- C#使用Tesseract OCR 解析验证码
- C#使用Tesseract OCR 解析验证码
- 利用tesseract-ocr进行验证码识别
- Tesseract Ocr 引擎识别图形验证码
- 浏览器的工作原理:新式网络浏览器幕后揭秘
- 线性表的基本操作
- BootStrap学习笔记一
- java多元线性回归
- 工厂模式
- webdriver结合tesseract-ocr处理简单验证码
- servlet-4 状态码
- 网址收藏
- 关于按钮设置属性的小细节
- Android 6.0 使用蓝牙BLE 时 报错:java.lang.SecurityException:Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOC
- makefile常用参数
- 把字符串转换为对应的整数
- 干货系列之手把手教你使用Core animation 做动画
- 《HTML5移动开发指南》——笔记8(Geolocation地理定位)