Skip to content
This repository was archived by the owner on Dec 7, 2020. It is now read-only.

Commit f42efc2

Browse files
author
Severi Haverila
committed
extracted tesseract
1 parent a6d99d5 commit f42efc2

File tree

2 files changed

+39
-31
lines changed

2 files changed

+39
-31
lines changed

src/main/java/TestdroidImageRecognition.java

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -293,17 +293,7 @@ public void processBuilder(String[] adbCommand) {
293293
}
294294
}
295295

296-
/**
297-
* ======================================================================================
298-
* CROP IMAGE
299-
* ======================================================================================
300-
*/
301296

302-
public void findAndCropImageFromScreen(String image) throws Exception {
303-
ImageRecognitionSettingsDTO settings = new ImageRecognitionSettingsDTO();
304-
settings.setCrop(true);
305-
findImageOnScreen(image, settings);
306-
}
307297

308298
/**
309299
* ======================================================================================
@@ -312,29 +302,17 @@ public void findAndCropImageFromScreen(String image) throws Exception {
312302
*/
313303

314304
public String grabText(String image) throws Exception {
315-
findAndCropImageFromScreen(image);
316-
String imageInput = screenshotsFolder + getScreenshotsCounter() + "_" + image + "_screenshot" + getRetryCounter() + "_" + timeDifferenceStartTest + "sec" + ".png";
317-
String[] tesseractCommand = {"tesseract", imageInput, "stdout"};
318-
String value = "";
319-
try {
320-
ProcessBuilder p = new ProcessBuilder(tesseractCommand);
321-
Process proc = p.start();
322-
InputStream stdin = proc.getInputStream();
323-
InputStreamReader isr = new InputStreamReader(stdin);
324-
BufferedReader br = new BufferedReader(isr);
325-
String line;
326-
String[] size = null;
327-
String[] splitLines;
328-
while ((line = br.readLine()) != null) {
329-
value += line;
330-
}
331-
332-
} catch (Throwable t) {
333-
t.printStackTrace();
334-
}
335-
return value;
305+
ImageRecognitionSettingsDTO settings = new ImageRecognitionSettingsDTO();
306+
settings.setCrop(true);
307+
ImageSearchDTO imageSearch = findImageOnScreen2(image, settings);
308+
309+
String imageInput = imageSearch.getScreenshotFile();
310+
return ImageRecognition.getTextStringFromImage(imageInput);
336311
}
337312

313+
314+
315+
338316
/**
339317
* ======================================================================================
340318
* OTHER UTILITIES

src/main/java/library/ImageRecognition.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package library;
22

3+
import java.io.BufferedReader;
4+
import java.io.InputStream;
5+
import java.io.InputStreamReader;
6+
37
import org.opencv.core.Point;
48
import org.openqa.selenium.Dimension;
59
import org.slf4j.Logger;
@@ -85,4 +89,30 @@ public static Point[] findImage(String image, String scene, ImageRecognitionSett
8589
}
8690
return null;
8791
}
92+
93+
94+
public static String getTextStringFromImage(String imageInput) {
95+
String[] tesseractCommand = {"tesseract", imageInput, "stdout"};
96+
String value = "";
97+
try {
98+
ProcessBuilder p = new ProcessBuilder(tesseractCommand);
99+
Process proc = p.start();
100+
InputStream stdin = proc.getInputStream();
101+
InputStreamReader isr = new InputStreamReader(stdin);
102+
BufferedReader br = new BufferedReader(isr);
103+
String line;
104+
String[] size = null;
105+
String[] splitLines;
106+
while ((line = br.readLine()) != null) {
107+
value += line;
108+
}
109+
110+
} catch (Throwable t) {
111+
t.printStackTrace();
112+
}
113+
return value;
114+
}
115+
116+
117+
88118
}

0 commit comments

Comments
 (0)