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

Commit 63c5fd0

Browse files
author
Severi Haverila
committed
refactored variable names
1 parent edaecd2 commit 63c5fd0

File tree

1 file changed

+78
-120
lines changed

1 file changed

+78
-120
lines changed

library/src/main/java/imagerecognition/ImageRecognition.java

Lines changed: 78 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -30,60 +30,19 @@ private static void log(String message) {
3030
AkazeImageFinder.setupOpenCVEnv();
3131
}
3232

33-
// TODO remove this and make private when a way is found to get the screen size for iOS without the appium driver
34-
// and then remove the screen size parameter from other methods
35-
public static Dimension getScreenSize(PlatformType platform, AppiumDriver<MobileElement> driver) throws Exception {
36-
if (platform.equals(PlatformType.IOS)) {
37-
return driver.manage().window().getSize();
38-
} else {
39-
return getAndroidScreenSize();
40-
}
41-
}
42-
43-
private static Dimension getAndroidScreenSize() throws IOException, InterruptedException {
44-
String adb = "adb";
45-
String[] adbCommand = {adb, "shell", "dumpsys", "window"};
46-
ProcessBuilder p = new ProcessBuilder(adbCommand);
47-
Process proc = p.start();
48-
InputStream stdin = proc.getInputStream();
49-
InputStreamReader isr = new InputStreamReader(stdin);
50-
BufferedReader br = new BufferedReader(isr);
51-
String line = null;
52-
String[] size = null;
53-
while ((line = br.readLine()) != null) {
54-
if (!line.contains("OriginalmUnrestrictedScreen")) { //we do this check for devices with android 5.x+ The adb command returns an extra line with the values 0x0 which must be filtered out.
55-
if (line.contains("mUnrestrictedScreen")) {
56-
proc.waitFor();
57-
String[] tmp = line.split("\\) ");
58-
size = tmp[1].split("x");
59-
}
60-
}
61-
}
62-
int width = Integer.parseInt(size[0]);
63-
int height = Integer.parseInt(size[1]);
64-
Dimension screenSize = new Dimension(width, height);
65-
return screenSize;
66-
}
67-
68-
69-
public static ImageLocation findImage(String image, String scene, PlatformType platform, Dimension screenSize) throws Exception {
33+
public static ImageLocation findImage(String searchedImageFilePath, String sceneImageFilePath, PlatformType platform, Dimension screenSize) throws Exception {
7034
ImageRecognitionSettings setting = new ImageRecognitionSettings();
71-
return findImage(image, scene, setting, platform, screenSize);
35+
return findImage(searchedImageFilePath, sceneImageFilePath, setting, platform, screenSize);
7236
}
7337

74-
75-
//This method calls on the Akaze scripts to find the coordinates of a given image in another image.
76-
//The "image" parameter is the image that you are searching for
77-
//The "scene" parameter is the image in which we are looking for "image"
78-
// "tolerance" sets the required accuracy for the image recognition algorithm.
79-
public static ImageLocation findImage(String image, String scene, ImageRecognitionSettings settings, PlatformType platform, Dimension screenSize) throws Exception {
80-
log("Searching for " + image);
81-
log("Searching in " + scene);
82-
ImageLocation imgLocation = findImageUsingAkaze(image, scene, settings);
38+
public static ImageLocation findImage(String searchedImageFilePath, String sceneImageFilePath, ImageRecognitionSettings settings, PlatformType platform, Dimension screenSize) throws Exception {
39+
log("Searching for " + searchedImageFilePath);
40+
log("Searching in " + sceneImageFilePath);
41+
ImageLocation imgLocation = imageFinder.findImage(searchedImageFilePath, sceneImageFilePath, settings.getTolerance());
8342

8443
if (imgLocation != null) {
8544
if (platform.equals(PlatformType.IOS)) {
86-
imgLocation = scaleImageRectangleForIos(screenSize, imgLocation, scene);
45+
imgLocation = scaleImageRectangleForIos(screenSize, imgLocation, sceneImageFilePath);
8746
}
8847
Point center = imgLocation.getCenter();
8948
if (!isPointInsideScreenBounds(center, screenSize)) {
@@ -95,19 +54,10 @@ public static ImageLocation findImage(String image, String scene, ImageRecogniti
9554
return imgLocation;
9655
}
9756

98-
99-
100-
private static ImageLocation findImageUsingAkaze(String image, String scene, ImageRecognitionSettings settings) {
101-
ImageLocation location = imageFinder.findImage(image, scene, settings.getTolerance());
102-
return location;
103-
}
104-
105-
106-
107-
private static ImageLocation scaleImageRectangleForIos(Dimension screenSize, ImageLocation imageLocation, String scene) {
57+
private static ImageLocation scaleImageRectangleForIos(Dimension screenSize, ImageLocation imageLocation, String sceneImageFilePath) {
10858
//for retina devices we need to recalculate coordinates
109-
double sceneHeight = imageFinder.getSceneHeight(scene);
110-
double sceneWidth = imageFinder.getSceneWidth(scene);
59+
double sceneHeight = imageFinder.getSceneHeight(sceneImageFilePath);
60+
double sceneWidth = imageFinder.getSceneWidth(sceneImageFilePath);
11161
int screenHeight = screenSize.getHeight();
11262
int screenWidth = screenSize.getWidth();
11363

@@ -140,22 +90,20 @@ private static ImageLocation scaleImageRectangleForIos(Dimension screenSize, Ima
14090
return imageLocation;
14191
}
14292

143-
144-
14593
private static boolean isPointInsideScreenBounds(Point center, Dimension screenSize) {
14694
return !((center.x >= screenSize.width) || (center.x < 0) || (center.y >= screenSize.height) || (center.y < 0));
14795
}
14896

14997

150-
public static boolean hasImageDissappearedFromScreenBeforeTimeout(String imageFile,
151-
String screenshotsFolder, Dimension screenSize, PlatformType platform) throws Exception {
152-
log("==> Trying to find image: " + imageFile);
98+
public static boolean hasImageDissappearedFromScreenBeforeTimeout(String searchedImageFilePath,
99+
String screenshotBaseDirectory, Dimension screenSize, PlatformType platform) throws Exception {
100+
log("==> Trying to find image: " + searchedImageFilePath);
153101
int retry_counter=0;
154102
long start = System.nanoTime();
155103
while (((System.nanoTime() - start) / 1e6 / 1000 < 300)) {
156-
String screenshotName = parseFileName(imageFile) + "_screenshot_"+retry_counter;
157-
String screenShotFile = ImageRecognition.takeScreenshot(screenshotName, screenshotsFolder, platform);
158-
if ((findImage(imageFile, screenShotFile, platform, screenSize)) == null) {
104+
String screenshotName = FilenameUtils.getBaseName(searchedImageFilePath) + "_screenshot_"+retry_counter;
105+
String screenShotFile = ImageRecognition.takeScreenshot(screenshotName, screenshotBaseDirectory, platform);
106+
if ((findImage(searchedImageFilePath, screenShotFile, platform, screenSize)) == null) {
159107
log("Image has successfully disappeared from screen.");
160108
return true;
161109
}
@@ -166,12 +114,6 @@ public static boolean hasImageDissappearedFromScreenBeforeTimeout(String imageFi
166114
return false;
167115
}
168116

169-
private static String parseFileName(String imageFile){
170-
return FilenameUtils.getBaseName(imageFile);
171-
}
172-
173-
174-
175117
public static String getTextStringFromImage(String imageInput) {
176118
String[] tesseractCommand = {"tesseract", imageInput, "stdout"};
177119
String value = "";
@@ -195,40 +137,36 @@ public static String getTextStringFromImage(String imageInput) {
195137

196138

197139

198-
public static ImageSearchResult findImageOnScreen(String imageFile, String screenshotsFolder, ImageRecognitionSettings settings, Dimension screenSize, PlatformType platform) throws InterruptedException, IOException, Exception {
199-
ImageSearchResult foundImageDto = findImageLoop(imageFile, screenshotsFolder, settings, screenSize, platform);
200-
if (foundImageDto.isFound() && settings.isCrop()) {
201-
cropImage(foundImageDto);
140+
public static ImageSearchResult findImageOnScreen(String searchedImagePath, String screenshotBaseDirectory, ImageRecognitionSettings settings, Dimension screenSize, PlatformType platform) throws InterruptedException, IOException, Exception {
141+
ImageSearchResult imageSearchResult = findImageLoop(searchedImagePath, screenshotBaseDirectory, settings, screenSize, platform);
142+
if (imageSearchResult.isFound() && settings.isCrop()) {
143+
log("Cropping image..");
144+
imageFinder.cropImage(imageSearchResult);
145+
log("Cropping image.. Succeeded!");
202146
}
203-
return foundImageDto;
147+
return imageSearchResult;
204148
}
205149

206-
private static ImageSearchResult findImageLoop(String imageFile, String screenshotsFolder, ImageRecognitionSettings settings, Dimension screenSize, PlatformType platform) throws InterruptedException, IOException, Exception {
150+
private static ImageSearchResult findImageLoop(String searchedImagePath, String screenshotBaseDirectory, ImageRecognitionSettings settings, Dimension screenSize, PlatformType platform) throws InterruptedException, IOException, Exception {
207151
long start_time = System.nanoTime();
208-
ImageSearchResult foundImageDto = new ImageSearchResult();
209-
String imageName = parseFileName(imageFile);
152+
ImageSearchResult imageSearchResult = new ImageSearchResult();
153+
String imageName = FilenameUtils.getBaseName(searchedImagePath);
210154
for (int i = 0; i < settings.getRetries(); i++) {
211155
String screenshotName = imageName + "_screenshot_"+i;
212-
String screenshotFile = takeScreenshot(screenshotName,screenshotsFolder, platform);
213-
ImageLocation imageLocation = ImageRecognition.findImage(imageFile, screenshotFile, settings, platform, screenSize);
156+
String screenshotFile = takeScreenshot(screenshotName,screenshotBaseDirectory, platform);
157+
ImageLocation imageLocation = ImageRecognition.findImage(searchedImagePath, screenshotFile, settings, platform, screenSize);
214158
if (imageLocation!=null){
215159
long end_time = System.nanoTime();
216160
int difference = (int) ((end_time - start_time) / 1e6 / 1000);
217161
log("==> Find image took: " + difference + " secs.");
218-
foundImageDto.setImageLocation(imageLocation);
219-
foundImageDto.setScreenshotFile(screenshotFile);
220-
return foundImageDto;
162+
imageSearchResult.setImageLocation(imageLocation);
163+
imageSearchResult.setScreenshotFile(screenshotFile);
164+
return imageSearchResult;
221165
}
222166
retryWait(settings);
223167
}
224168
log("==> Image not found");
225-
return foundImageDto;
226-
}
227-
228-
private static void cropImage(ImageSearchResult foundImage) {
229-
log("Cropping image..");
230-
imageFinder.cropImage(foundImage);
231-
log("Cropping image.. Succeeded!");
169+
return imageSearchResult;
232170
}
233171

234172
private static void retryWait(ImageRecognitionSettings settings) throws InterruptedException {
@@ -238,31 +176,20 @@ private static void retryWait(ImageRecognitionSettings settings) throws Interrup
238176
}
239177
}
240178

241-
//Stops the script for the given amount of seconds.
242179
private static void sleep(int seconds) throws InterruptedException {
243180
Thread.sleep(seconds * 1000);
244181
}
245182

246-
247-
248-
249-
/* TODO
250-
* if (idevicescreenshotExists) {
251-
// Keep Appium session alive between multiple non-driver screenshots
252-
driver.manage().window().getSize();
253-
}
254-
255-
*/
256-
public static String takeScreenshot(String screenshotName, String screenshotsFolder, PlatformType platform) throws Exception {
183+
public static String takeScreenshot(String screenshotName, String screenshotBaseDirectory, PlatformType platform) throws Exception {
257184
long start_time = System.nanoTime();
258185

259-
String screenshotFile = screenshotsFolder + screenshotName + ".png";
260-
String fullFileName = System.getProperty("user.dir") + "/" + screenshotFile;
186+
String screenshotFile = screenshotBaseDirectory + screenshotName + ".png";
187+
String screenShotFilePath = System.getProperty("user.dir") + "/" + screenshotFile;
261188

262189
if (platform.equals(PlatformType.IOS)) {
263-
takeIDeviceScreenshot(fullFileName);
190+
takeIDeviceScreenshot(screenShotFilePath);
264191
} else if (platform.equals(PlatformType.ANDROID)) {
265-
takeAndroidScreenshot(fullFileName);
192+
takeAndroidScreenshot(screenShotFilePath);
266193
} else{
267194
throw new Exception("Invalid platformType: "+platform);
268195
}
@@ -273,11 +200,10 @@ public static String takeScreenshot(String screenshotName, String screenshotsFol
273200
return screenshotFile;
274201
}
275202

276-
277-
private static void takeAndroidScreenshot(String fullFileName) throws IOException, InterruptedException {
203+
private static void takeAndroidScreenshot(String screenShotFilePath) throws IOException, InterruptedException {
278204
log("Taking android screenshot...");
279-
log(fullFileName);
280-
String[] cmd = new String[]{"screenshot2", "-d", fullFileName};
205+
log(screenShotFilePath);
206+
String[] cmd = new String[]{"screenshot2", "-d", screenShotFilePath};
281207
Process p = Runtime.getRuntime().exec(cmd);
282208
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
283209
String line;
@@ -290,15 +216,12 @@ private static void takeAndroidScreenshot(String fullFileName) throws IOExceptio
290216
}
291217
}
292218

293-
294-
295-
296-
private static void takeIDeviceScreenshot(String fullFileName) throws Exception {
219+
private static void takeIDeviceScreenshot(String screenShotFilePath) throws Exception {
297220
String udid = System.getenv("UDID");
298221
if (udid==null){
299222
throw new Exception("$UDID was null, set UDID environment variable and try again");
300223
}
301-
String[] cmd = new String[]{"idevicescreenshot", "-u", udid, fullFileName};
224+
String[] cmd = new String[]{"idevicescreenshot", "-u", udid, screenShotFilePath};
302225
Process p = Runtime.getRuntime().exec(cmd);
303226
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
304227
String line;
@@ -309,15 +232,50 @@ private static void takeIDeviceScreenshot(String fullFileName) throws Exception
309232
if (exitVal != 0) {
310233
log("idevicescreenshot process exited with value: " + exitVal);
311234
}
312-
cmd = new String[]{"sips", "-s", "format", "png", fullFileName, "--out", fullFileName};
235+
cmd = new String[]{"sips", "-s", "format", "png", screenShotFilePath, "--out", screenShotFilePath};
313236
p = Runtime.getRuntime().exec(cmd);
314237
exitVal = p.waitFor();
315238
if (exitVal != 0) {
316239
log("sips process exited with value: " + exitVal);
317240
}
318241
}
242+
243+
319244

245+
// TODO remove this and make private when a way is found to get the screen size for iOS without the appium driver
246+
// and then remove the screen size parameter from other methods
247+
public static Dimension getScreenSize(PlatformType platform, AppiumDriver<MobileElement> driver) throws Exception {
248+
if (platform.equals(PlatformType.IOS)) {
249+
return driver.manage().window().getSize();
250+
} else {
251+
return getAndroidScreenSize();
252+
}
253+
}
320254

255+
private static Dimension getAndroidScreenSize() throws IOException, InterruptedException {
256+
String adb = "adb";
257+
String[] adbCommand = {adb, "shell", "dumpsys", "window"};
258+
ProcessBuilder p = new ProcessBuilder(adbCommand);
259+
Process proc = p.start();
260+
InputStream stdin = proc.getInputStream();
261+
InputStreamReader isr = new InputStreamReader(stdin);
262+
BufferedReader br = new BufferedReader(isr);
263+
String line = null;
264+
String[] size = null;
265+
while ((line = br.readLine()) != null) {
266+
if (!line.contains("OriginalmUnrestrictedScreen")) { //we do this check for devices with android 5.x+ The adb command returns an extra line with the values 0x0 which must be filtered out.
267+
if (line.contains("mUnrestrictedScreen")) {
268+
proc.waitFor();
269+
String[] tmp = line.split("\\) ");
270+
size = tmp[1].split("x");
271+
}
272+
}
273+
}
274+
int width = Integer.parseInt(size[0]);
275+
int height = Integer.parseInt(size[1]);
276+
Dimension screenSize = new Dimension(width, height);
277+
return screenSize;
278+
}
321279

322280

323281
}

0 commit comments

Comments
 (0)