webdriver结合tesseract-ocr处理简单验证码

来源:互联网 发布:网络购物的发展 编辑:程序博客网 时间:2024/05/05 22:09

使用OCR自动化识别,一般识别率不是太高,处理一般简单验证码还是没问题,这里使用的是Tesseract-OCR,下载地址:http://pan.baidu.com/s/1kUGaw8R

怎么使用呢?

首先,环境变量path添加tesseract-ocr的安装路径,然后使用命令窗口查看:

如果出现如上输出,表示安装正常。

我准备了一张验证码cp.png放在e盘tesseract目录下


结果为:

现在,具体实践,先准备一份网页:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <html>  
  2. <head>  
  3. <title>验证码</title>  
  4. </head>  
  5. <body>  
  6.     <form>  
  7.         <td>验证码:</td>  
  8.         <input id="cp" type="text"/>  
  9.         <img src="http://www.csti.cn/uc/index/verify.htm">  
  10.     </form>  
  11. </body>  
  12. </html>  







要识别验证码,首先得取得验证码,首先获取整个页面的截图,然后找到页面元素坐标进行截取:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //元素截图  
  2. public static void captureElement(WebDriver driver, WebElement element, String path){  
  3.     // 截图整个页面  
  4.     File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);  
  5.     try {  
  6.         // 获得元素的高度和宽度  
  7.         int width = element.getSize().getWidth();  
  8.         int height = element.getSize().getHeight();  
  9.         // 创建一个矩形使用上面的高度和宽度  
  10.         Rectangle rect = new Rectangle(width, height);  
  11.         // 得到元素的坐标  
  12.         Point p = element.getLocation();  
  13.         BufferedImage img = ImageIO.read(srcFile);  
  14.         BufferedImage dest = img.getSubimage(p.getX(), p.getY(), rect.width,rect.height);  
  15.         // 存为png格式  
  16.         ImageIO.write(dest, "png", srcFile);  
  17.         Thread.sleep(1000);  
  18.         FileUtils.copyFile(srcFile, new File(path));  
  19.     } catch (Exception e) {  
  20.         e.printStackTrace();  
  21.     }  
  22. }  

截取完元素,就可以调用Tesseract-OCR生成text:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. Runtime rt = Runtime.getRuntime();  
  2. rt.exec("cmd.exe /C  tesseract e:\\tesseract\\cp.png e:\\tesseract\\cp");  
接下来通过java读取txt:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public static String readTextFile(String filePath) {  
  2.     String lineTxt = null;  
  3.         try {  
  4.             String encoding = "GBK";  
  5.             File file = new File(filePath);  
  6.             if (file.isFile() && file.exists()) { // 判断文件是否存在  
  7.                 InputStreamReader read = new InputStreamReader(  
  8.                         new FileInputStream(file), encoding);// 考虑到编码格式  
  9.                 BufferedReader bufferedReader = new BufferedReader(read);  
  10.                 while ((lineTxt = bufferedReader.readLine()) != null) {  
  11.                     return lineTxt;  
  12.                 }  
  13.                 read.close();  
  14.             } else {  
  15.                 System.out.println("找不到指定的文件");  
  16.             }  
  17.         } catch (Exception e) {  
  18.             System.out.println("读取文件内容出错");  
  19.             e.printStackTrace();  
  20.         }  
  21.         return lineTxt;  
  22. }  
最后,直接调用:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public static void main(String[] args) throws IOException, InterruptedException {  
  2.     WebDriver driver = new FirefoxDriver();  
  3.     driver.manage().window().maximize();  
  4.     driver.get("file:///E:/tesseract/cp.html");  
  5.     WebElement cp = driver.findElement(By.xpath("//img"));  
  6.     captureElement(driver, cp, "e:\\tesseract\\cp.png");  
  7.     Runtime rt = Runtime.getRuntime();  
  8.         rt.exec("cmd.exe /C  tesseract e:\\tesseract\\cp.png e:\\tesseract\\cp");  
  9.         Thread.sleep(1000);  
  10.         String cp2 = readTextFile("e:\\tesseract\\cp.txt");  
  11.         driver.findElement(By.id("cp")).sendKeys(cp2);  
  12.         //driver.quit();  
  13. }  
0 0
原创粉丝点击