@@ -54,31 +54,35 @@ public Point[] findImageOnScreen(String image, ImageRecognitionSettingsDTO setti
54
54
}
55
55
56
56
public ImageSearchDTO findImageOnScreen2 (String image , ImageRecognitionSettingsDTO settings ) throws Exception {
57
- ImageSearchDTO foundImage = findImageLoop (image , settings );
57
+ ImageSearchDTO foundImage = findImageLoop (image , settings , getScreenSizeADB () );
58
58
if (foundImage .isFound () && settings .isCrop ()) {
59
- Point [] imgRect = foundImage .getImageRectangle ();
60
- Point top_left = imgRect [0 ];
61
- Point top_right = imgRect [1 ];
62
- Point bottom_left = imgRect [2 ];
63
- Point center = imgRect [4 ];
64
- imageFinder .cropImage (foundImage .getScreenshotFile (), top_left .x , top_left .y , top_right .x - top_left .x , bottom_left .y - top_left .y );
59
+ cropImage (foundImage );
65
60
}
66
61
return foundImage ;
67
62
}
68
63
69
- private ImageSearchDTO findImageLoop (String image , ImageRecognitionSettingsDTO settings ) throws InterruptedException , IOException , Exception {
64
+
65
+ private void cropImage (ImageSearchDTO foundImage ) {
66
+ Point [] imgRect = foundImage .getImageRectangle ();
67
+ Point top_left = imgRect [0 ];
68
+ Point top_right = imgRect [1 ];
69
+ Point bottom_left = imgRect [2 ];
70
+ Point center = imgRect [4 ];
71
+ imageFinder .cropImage (foundImage .getScreenshotFile (), top_left .x , top_left .y , top_right .x - top_left .x , bottom_left .y - top_left .y );
72
+ }
73
+
74
+ private ImageSearchDTO findImageLoop (String image , ImageRecognitionSettingsDTO settings , Dimension screenSize ) throws InterruptedException , IOException , Exception {
70
75
long start_time = System .nanoTime ();
71
76
ImageSearchDTO foundImageDto = new ImageSearchDTO ();
72
77
for (int i = 0 ; i < settings .getRetries (); i ++) {
73
78
// queryImageFolder is "", unless set by setQueryImageFolder()
74
- String queryImageFile = "queryimages/" + queryimageFolder + image + "_screenshot" ;
79
+ String queryImageFile = "queryimages/" + queryimageFolder + image ;
75
80
String screenshotFile = takeScreenshot (image + "_screenshot" );
76
- Point [] imgRect = ImageRecognition .findImage (queryImageFile , screenshotFile , settings , platformName , getScreenSizeADB () );
81
+ Point [] imgRect = ImageRecognition .findImage (queryImageFile , screenshotFile , settings , platformName , screenSize );
77
82
if (imgRect !=null ){
78
83
long end_time = System .nanoTime ();
79
84
int difference = (int ) ((end_time - start_time ) / 1e6 / 1000 );
80
85
log ("==> Find image took: " + difference + " secs." );
81
-
82
86
foundImageDto .setImageRectangle (imgRect );
83
87
foundImageDto .setScreenshotFile (screenshotFile );
84
88
return foundImageDto ;
@@ -216,63 +220,6 @@ public boolean findDeviceTypeADB() throws Exception {
216
220
return false ;
217
221
}
218
222
219
- //Uses adb commands to tap at relative coordinates. To be used when appium methods fail. Only works on Android devices.
220
- public void tapAtRelativeCoordinatesADB (double x_offset , double y_offset ) throws Exception {
221
- if (platformName .equalsIgnoreCase ("iOS" )) {
222
- tapAtRelativeCoordinates (x_offset , y_offset );
223
- } else {
224
- Dimension size = getScreenSizeADB ();
225
- log ("Size of device as seen by ADB is - width: " + size .width + " height: " + size .height );
226
- String x = String .valueOf (size .width * x_offset );
227
- String y = String .valueOf (size .height * y_offset );
228
- log ("ADB: x and y: " + x + ", " + y );
229
- String [] adbCommand = {"adb" , "shell" , "input" , "tap" , x , y };
230
- // String[] adbCommand = {"adb", "shell", "input", "touchscreen", "swipe", x, y, x, y, "2000"};
231
-
232
- try {
233
- ProcessBuilder p = new ProcessBuilder (adbCommand );
234
- Process proc = p .start ();
235
- InputStream stdin = proc .getInputStream ();
236
- InputStreamReader isr = new InputStreamReader (stdin );
237
- BufferedReader br = new BufferedReader (isr );
238
- String line = null ;
239
- while ((line = br .readLine ()) != null )
240
- System .out .print (line );
241
-
242
-
243
- proc .waitFor ();
244
-
245
- } catch (Throwable t ) {
246
- t .printStackTrace ();
247
- }
248
- }
249
- }
250
-
251
- //Uses adb commands to tap at coordinates. To be used when appium methods fail. Only works on Android devices.
252
- public void tapAtCoordinatesADB (double x , double y ) throws Exception {
253
- String [] adbCommand ;
254
- if (platformName .equalsIgnoreCase ("iOS" )) {
255
- tapAtCoordinates ((int ) x , (int ) y );
256
- } else {
257
- int Xx = (int ) x ;
258
- int Yy = (int ) y ;
259
- String X = String .valueOf (Xx );
260
- String Y = String .valueOf (Yy );
261
- log ("ADB: X: " + X + ", Y: " + Y );
262
- // String[] adbCommand = {"adb", "shell", "input", "tap", X, Y};
263
-
264
- if (automationName .equalsIgnoreCase ("selendroid" )) {
265
- log ("adb_shell_input_tap" ); //works for 4.1.x. Will not work for 4.0.x
266
- adbCommand = new String []{"adb" , "shell" , "input" , "tap" , X , Y };
267
- processBuilder (adbCommand );
268
- log ("Tap done." );
269
- } else {
270
- adbCommand = new String []{"adb" , "shell" , "input" , "touchscreen" , "swipe" , X , Y , X , Y , "2000" };
271
- processBuilder (adbCommand );
272
- }
273
- }
274
- }
275
-
276
223
public void processBuilder (String [] adbCommand ) {
277
224
try {
278
225
found = true ;
@@ -293,21 +240,12 @@ public void processBuilder(String[] adbCommand) {
293
240
}
294
241
}
295
242
296
-
297
-
298
- /**
299
- * ======================================================================================
300
- * TESSERACT GRAB TEXT FROM IMAGE
301
- * ======================================================================================
302
- */
303
-
304
- public String grabText (String image ) throws Exception {
243
+ public String grabTextFromImage (String image ) throws Exception {
305
244
ImageRecognitionSettingsDTO settings = new ImageRecognitionSettingsDTO ();
306
245
settings .setCrop (true );
307
246
ImageSearchDTO imageSearch = findImageOnScreen2 (image , settings );
308
-
309
- String imageInput = imageSearch .getScreenshotFile ();
310
- return ImageRecognition .getTextStringFromImage (imageInput );
247
+ String text = ImageRecognition .getTextStringFromImage (imageSearch .getScreenshotFile ());
248
+ return text ;
311
249
}
312
250
313
251
0 commit comments