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

Commit fa55d86

Browse files
author
Severi Haverila
committed
more refactoring
1 parent c73add8 commit fa55d86

File tree

1 file changed

+18
-80
lines changed

1 file changed

+18
-80
lines changed

src/main/java/TestdroidImageRecognition.java

Lines changed: 18 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,35 @@ public Point[] findImageOnScreen(String image, ImageRecognitionSettingsDTO setti
5454
}
5555

5656
public ImageSearchDTO findImageOnScreen2(String image, ImageRecognitionSettingsDTO settings) throws Exception {
57-
ImageSearchDTO foundImage = findImageLoop(image, settings);
57+
ImageSearchDTO foundImage = findImageLoop(image, settings, getScreenSizeADB());
5858
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);
6560
}
6661
return foundImage;
6762
}
6863

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 {
7075
long start_time = System.nanoTime();
7176
ImageSearchDTO foundImageDto = new ImageSearchDTO();
7277
for (int i = 0; i < settings.getRetries(); i++) {
7378
// queryImageFolder is "", unless set by setQueryImageFolder()
74-
String queryImageFile = "queryimages/" + queryimageFolder + image + "_screenshot";
79+
String queryImageFile = "queryimages/" + queryimageFolder + image;
7580
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);
7782
if (imgRect!=null){
7883
long end_time = System.nanoTime();
7984
int difference = (int) ((end_time - start_time) / 1e6 / 1000);
8085
log("==> Find image took: " + difference + " secs.");
81-
8286
foundImageDto.setImageRectangle(imgRect);
8387
foundImageDto.setScreenshotFile(screenshotFile);
8488
return foundImageDto;
@@ -216,63 +220,6 @@ public boolean findDeviceTypeADB() throws Exception {
216220
return false;
217221
}
218222

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-
276223
public void processBuilder(String[] adbCommand) {
277224
try {
278225
found = true;
@@ -293,21 +240,12 @@ public void processBuilder(String[] adbCommand) {
293240
}
294241
}
295242

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 {
305244
ImageRecognitionSettingsDTO settings = new ImageRecognitionSettingsDTO();
306245
settings.setCrop(true);
307246
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;
311249
}
312250

313251

0 commit comments

Comments
 (0)