tess 4 j認識グラフィック検証コード

2399 ワード

多くのブログを見て、数字しか認識できないことを試してみましたが、アルファベット認識には問題があり、正確ではありません.整理したコードは以下の通りです.
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());
	        }
	    }
	}