我要求它识别图像中存在的字符,并且要求它读取内容并将其存储在变量中

时间:2018-10-05 12:40:08

标签: java selenium selenium-webdriver appium-android

我已经编写了这段代码:

import java.awt.Image;
import java.awt.image.RenderedImage;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType; 

// class and method declaration omitted

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//store screenshot at a specidfied location
File targetFile=new File("C:\\Users\\Srujan\\eclipse-workspace\\Batch98Appium\\images\\kiran.png");
FileUtils.copyFile(scrFile,targetFile );
System.out.println(targetFile.toString());

// URL url1 = new URL(targetFile.toString());  
Image image = ImageIO.read(targetFile);         

// we are asking it to recognize the characers present in the image and we are asking to it to read the contnet and to store the content in a variable
String s = new OCR().recognizeCharacters((RenderedImage) image);  

//below cmd is simply printing the information
System.out.println("Text From Image : \n"+ s);  

if (s.contains("Monitored switch is on")) {
    System.out.println("toastr verified successfully");
} else {
    System.out.println("toastr not verified successfully");
}

我将jar添加为Maven依赖项:

  

Java OCR API 15.3.0.1

     

java-client 6.1.0

出现以下编译错误:

  

String s = new OCR()。recognizeCharacters((RenderedImage)image);

     

OCR()。Recog ... OCR无法解析为类型。

1 个答案:

答案 0 :(得分:0)

您未包括Ocr类的导入文件(请注意小写字母)。将其添加到文件顶部附近:

 import com.asprise.ocr.Ocr;

然后将类的名称更正为Ocr