一:前面的文章写了如何右键另存为图片,把验证码存为图片后,接下来就是要做,怎么把图片上的内容获取到,借住tesseract工具
1.下载tesseract:http://sourceforge.net/projects/tesseract-ocr/
2.安装tesseract,安装成功后,最好重启电脑,因为eclipse要读取path,在cmd输入tesseract.exe,出现参数列表则安装成功(不出现的话,就查看下系统path下是否有安装路径)
3.将tesseract.exe命令保存为bat文件,bat内容为
@echo off
tesseract.exe yzm.png 1 -l
exit
验证码图片的位置最后放在项目的根目录下
4.java调用该bat文件
String cmd = "cmd /c start d://yanzhengm.bat";
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); }
运行成功后,会生成一个1.txt文件,该文件保存了验证码的文本内容
5.java读取文件获得文本内容
二:上面的方法是右键另存为保存验证码图片后,再识别图片验证码,下面介绍用坐标的方法保存验证码图片
package com.imgyzm; import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException; import javax.imageio.ImageIO; import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.io.FileHandler;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; /**
* @author QiaoJiaofei
* @version 创建时间:2015年8月27日 上午10:29:57
* 类说明
*/
public class TestYzmByElementPoint {
WebDriver dr;
@BeforeTest
public void before() {
String key = "webdriver.chrome.driver";
String value = "D:/BaiduYunDownload/selenium/chromedriver.exe";
System.setProperty(key, value);
dr = new ChromeDriver();
dr.manage().window().maximize();
} @Test
public void test1() {
dr.get("http://172.16.30.242:5555/register.shtml");
WebDriverWait wait = new WebDriverWait(dr,10);
WebElement element = wait.until(new ExpectedCondition<WebElement>() { @Override
public WebElement apply(WebDriver arg0) {
// TODO Auto-generated method stub
return arg0.findElement(By.id("codeimg"));
} });
File scrFile = ((TakesScreenshot)dr).getScreenshotAs(OutputType.FILE);
//WebElement element = dr.findElement(By.id("codeimg")); try {
Point p = element.getLocation();
int width = element.getSize().getWidth();
int higth = element.getSize().getHeight();
Rectangle rect = new Rectangle(width, higth);
BufferedImage img = ImageIO.read(scrFile);
BufferedImage dest = img.getSubimage(p.getX(), p.getY(), width, higth);
ImageIO.write(dest, "png", scrFile);
Thread.sleep(1000);
File fng = new File("D:/ddd/yzm.png");
if(fng.exists()){
fng.delete();
}
FileUtils.copyFile(scrFile, fng); Runtime rt = Runtime.getRuntime();
rt.exec("cmd.exe /C tesseract.exe D:\\ddd\\yzm.png D:\\ddd\\yzm -1 ");
Thread.sleep(1000);
File file = new File("D:\\ddd\\yzm.txt");
if(file.exists()) {
FileHandler fh = new FileHandler();
String s = fh.readAsString(file).trim();
System.out.println(s);
} else {
System.out.print("yzm.txt不存在");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@AfterTest
public void after() {
dr.quit();
}
}