JAVA+selenium+tess4j识别登录验证码

public static String getValidCode() throws IOException {
	byte[] screenshot = null;
	screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);// 得到截图
	BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(screenshot));
	WebElement element = driver.findElement(By.id("LoginImgVerifyCode"));
	Dimension size = element.getSize();
	Rectangle rect = new Rectangle(element.getLocation().x, element.getLocation().y, size.getWidth(), size.getHeight());
    ITesseract instance = new Tesseract();
    instance.setDatapath("./tessdata");  //解压tess4j-4.3.0.jar包,拷贝tessdata目录到工程根目录下
    String result = null;
    try {
        result = instance.doOCR(originalImage, rect);
    } catch (TesseractException e) {
        System.err.println(e.getMessage());
    }
    
    return result.trim();
}

Maven工程所需要的jar包


    net.sourceforge.tess4j
    tess4j
    4.3.0


    org.seleniumhq.selenium
    selenium-java
    3.5.3

你可能感兴趣的:(JAVA+selenium+tess4j识别登录验证码)