tess4j识别图形验证码

看了很多博客,试了下只能识别数字,字母识别还是有问题,不准确。
整理的代码如下

package com.oceansoft.qingdaomsjw;
import java.io.File;
import java.awt.image.BufferedImage;
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.chrome.ChromeOptions;

import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
 
public class Test {
 	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		System.setProperty("webdriver.chrome.driver", "D:\\selenium-java-3.12.0\\chromedriver.exe");
		ChromeOptions options = new ChromeOptions();
		options.addArguments("disable-infobars");
		WebDriver driver= new ChromeDriver(options);
		//open the browser
		driver.get("xxx");
		WebElement ele = driver.findElement(By.id("verifyImg"));
		File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
		// 获取整个截图
		BufferedImage fullImg=ImageIO.read(screenshot);
		// Get the location of element on the page
		Point point= ele.getLocation();
		// Get width and height of the element
		int eleWidth= ele.getSize().getWidth();
		int eleHeight= ele.getSize().getHeight();
		// Crop the entire page screenshot to get only element screenshot
		BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);
		ImageIO.write(eleScreenshot, "png", screenshot);
		// Copy the element screenshot to disk
		File screenshotLocation= new File("C:/Users/Default/Documents/yzm.png");
		FileUtils.copyFile(screenshot, screenshotLocation);
		VCR();
	}
	//识别图片验证码为txt(数字识别率较高,字母有待完善)
	 public static void VCR() {
	        File imageFile = new File("C:/Users/Default/Documents/yzm.png");
	        ITesseract instance = new Tesseract();
	         try {
	            String result = instance.doOCR(imageFile);
	            System.out.println(result);
	        } catch (TesseractException e) {
	            System.err.println(e.getMessage());
	        }
	    }
	}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值