-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUsing Tessaract OCR to Extract Text from Image
72 lines (53 loc) · 2.84 KB
/
Using Tessaract OCR to Extract Text from Image
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package sauceLab;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
public class ExtractTextformImage {
public static final String URL = "https://krukmangada:846508c1-b42d-4fc7-a1e1-0770df454237@ondemand.us-west-1.saucelabs.com:443/wd/hub";
public static void main(String[] args) throws IOException, InterruptedException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "Google_Pixel_4a_real_us");
capabilities.setCapability("platformVersion", "11");
capabilities.setCapability("app", "storage:1c0f75b7-f012-47d5-acf0-faacb0a3a3ab");
capabilities.setCapability("browserName", "");
capabilities.setCapability("deviceOrientation", "portrait");
AndroidDriver<WebElement> driver = new AndroidDriver<WebElement>( new URL(URL), capabilities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id("com.wibmo.wibmo_banking.stage.onboarding:id/mobile_edit_text")).sendKeys("8951339695");
driver.findElement(By.id("com.wibmo.wibmo_banking.stage.onboarding:id/tc_check_box")).click();
driver.findElement(By.id("com.wibmo.wibmo_banking.stage.onboarding:id/continue_btn")).click();
Thread.sleep(5000);
driver.findElement(By.id("com.android.permissioncontroller:id/permission_allow_button")).click();
for(int i=1;i<=4;i++) {
Thread.sleep(5000);
driver.findElement(By.xpath("//*[@text='1']")).click();
}
driver.findElement(By.xpath("//*[@text='Enter']")).click();
Thread.sleep(5000);
File as=driver.findElement(By.xpath("(//android.widget.ImageView[@resource-id=\"com.wibmo.wibmo_banking.stage:id/imageSlide\"])[1]")).getScreenshotAs(OutputType.FILE);
File tempfile = File.createTempFile("temp", ".png");
String temppath=tempfile.getAbsolutePath();
FileUtils.copyFile(as,new File(temppath));
// FileUtils.copyFile(as,new File("src\\screenshots\\Page.png"));
ITesseract image=new Tesseract();
try {
String text=image.doOCR(new File(temppath));
//String text=image.doOCR(new File("src\\screenshots\\Page.png"));
System.out.println(text);
} catch (TesseractException e) {
System.out.println(e.getMessage());
}
System.out.println("Done");
}
}