12
12
import org .slf4j .LoggerFactory ;
13
13
14
14
import dtos .ImageLocation ;
15
- import dtos .ImageRecognitionSettingsDTO ;
16
- import dtos .ImageSearchDTO ;
15
+ import dtos .ImageRecognitionSettings ;
16
+ import dtos .ImageSearchResult ;
17
+ import dtos .PlatformType ;
17
18
18
19
public class ImageRecognition {
19
20
@@ -24,23 +25,23 @@ private static void log(String message) {
24
25
}
25
26
26
27
27
- public static ImageLocation findImage (String image , String scene , String platformName , Dimension screenSize ) throws Exception {
28
- ImageRecognitionSettingsDTO setting = new ImageRecognitionSettingsDTO ();
29
- return findImage (image , scene , setting , platformName , screenSize );
28
+ public static ImageLocation findImage (String image , String scene , PlatformType platform , Dimension screenSize ) throws Exception {
29
+ ImageRecognitionSettings setting = new ImageRecognitionSettings ();
30
+ return findImage (image , scene , setting , platform , screenSize );
30
31
}
31
32
32
33
33
34
//This method calls on the Akaze scripts to find the coordinates of a given image in another image.
34
35
//The "image" parameter is the image that you are searching for
35
36
//The "scene" parameter is the image in which we are looking for "image"
36
37
// "tolerance" sets the required accuracy for the image recognition algorithm.
37
- public static ImageLocation findImage (String image , String scene , ImageRecognitionSettingsDTO settings , String platformName , Dimension screenSize ) throws Exception {
38
+ public static ImageLocation findImage (String image , String scene , ImageRecognitionSettings settings , PlatformType platform , Dimension screenSize ) throws Exception {
38
39
log ("Searching for " + image );
39
40
log ("Searching in " + scene );
40
41
ImageLocation imgLocation = findImageUsingAkaze (image , scene , settings );
41
42
42
43
if (imgLocation != null ) {
43
- if (platformName . equalsIgnoreCase ( "iOS" )) {
44
+ if (platform . equals ( PlatformType . IOS )) {
44
45
imgLocation = scaleImageRectangleForIos (screenSize , imgLocation , scene );
45
46
}
46
47
Point center = imgLocation .getCenter ();
@@ -55,7 +56,7 @@ public static ImageLocation findImage(String image, String scene, ImageRecogniti
55
56
56
57
57
58
58
- private static ImageLocation findImageUsingAkaze (String image , String scene , ImageRecognitionSettingsDTO settings ) {
59
+ private static ImageLocation findImageUsingAkaze (String image , String scene , ImageRecognitionSettings settings ) {
59
60
ImageLocation location = imageFinder .findImage (image , scene , settings .getTolerance ());
60
61
return location ;
61
62
}
@@ -106,14 +107,14 @@ private static boolean isPointInsideScreenBounds(Point center, Dimension screenS
106
107
107
108
108
109
public static boolean hasImageDissappearedFromScreenBeforeTimeout (String imageFile ,
109
- String screenshotsFolder , Dimension screenSize , String platformName ) throws Exception {
110
+ String screenshotsFolder , Dimension screenSize , PlatformType platform ) throws Exception {
110
111
log ("==> Trying to find image: " + imageFile );
111
112
int retry_counter =0 ;
112
113
long start = System .nanoTime ();
113
114
while (((System .nanoTime () - start ) / 1e6 / 1000 < 300 )) {
114
115
String screenshotName = parseFileName (imageFile ) + "_screenshot_" +retry_counter ;
115
- String screenShotFile = ImageRecognition .takeScreenshot (screenshotName , screenshotsFolder , platformName );
116
- if ((findImage (imageFile , screenShotFile , platformName , screenSize )) == null ) {
116
+ String screenShotFile = ImageRecognition .takeScreenshot (screenshotName , screenshotsFolder , platform );
117
+ if ((findImage (imageFile , screenShotFile , platform , screenSize )) == null ) {
117
118
log ("Image has successfully disappeared from screen." );
118
119
return true ;
119
120
}
@@ -153,22 +154,22 @@ public static String getTextStringFromImage(String imageInput) {
153
154
154
155
155
156
156
- public static ImageSearchDTO findImageOnScreen (String imageFile , String screenshotsFolder , ImageRecognitionSettingsDTO settings , Dimension screenSize , String platformName ) throws InterruptedException , IOException , Exception {
157
- ImageSearchDTO foundImageDto = findImageLoop (imageFile , screenshotsFolder , settings , screenSize , platformName );
157
+ public static ImageSearchResult findImageOnScreen (String imageFile , String screenshotsFolder , ImageRecognitionSettings settings , Dimension screenSize , PlatformType platform ) throws InterruptedException , IOException , Exception {
158
+ ImageSearchResult foundImageDto = findImageLoop (imageFile , screenshotsFolder , settings , screenSize , platform );
158
159
if (foundImageDto .isFound () && settings .isCrop ()) {
159
160
cropImage (foundImageDto );
160
161
}
161
162
return foundImageDto ;
162
163
}
163
164
164
- private static ImageSearchDTO findImageLoop (String imageFile , String screenshotsFolder , ImageRecognitionSettingsDTO settings , Dimension screenSize , String platformName ) throws InterruptedException , IOException , Exception {
165
+ private static ImageSearchResult findImageLoop (String imageFile , String screenshotsFolder , ImageRecognitionSettings settings , Dimension screenSize , PlatformType platform ) throws InterruptedException , IOException , Exception {
165
166
long start_time = System .nanoTime ();
166
- ImageSearchDTO foundImageDto = new ImageSearchDTO ();
167
+ ImageSearchResult foundImageDto = new ImageSearchResult ();
167
168
String imageName = parseFileName (imageFile );
168
169
for (int i = 0 ; i < settings .getRetries (); i ++) {
169
170
String screenshotName = imageName + "_screenshot_" +i ;
170
- String screenshotFile = takeScreenshot (screenshotName ,screenshotsFolder , platformName );
171
- ImageLocation imageLocation = ImageRecognition .findImage (imageFile , screenshotFile , settings , platformName , screenSize );
171
+ String screenshotFile = takeScreenshot (screenshotName ,screenshotsFolder , platform );
172
+ ImageLocation imageLocation = ImageRecognition .findImage (imageFile , screenshotFile , settings , platform , screenSize );
172
173
if (imageLocation !=null ){
173
174
long end_time = System .nanoTime ();
174
175
int difference = (int ) ((end_time - start_time ) / 1e6 / 1000 );
@@ -183,13 +184,13 @@ private static ImageSearchDTO findImageLoop(String imageFile, String screenshots
183
184
return foundImageDto ;
184
185
}
185
186
186
- private static void cropImage (ImageSearchDTO foundImage ) {
187
+ private static void cropImage (ImageSearchResult foundImage ) {
187
188
log ("Cropping image.." );
188
189
imageFinder .cropImage (foundImage );
189
190
log ("Cropping image.. Succeeded!" );
190
191
}
191
192
192
- private static void retryWait (ImageRecognitionSettingsDTO settings ) throws InterruptedException {
193
+ private static void retryWait (ImageRecognitionSettings settings ) throws InterruptedException {
193
194
if (settings .getRetryWaitTime () > 0 ) {
194
195
log ("retryWait given, sleeping " + settings .getRetryWaitTime () + " seconds." );
195
196
sleep (settings .getRetryWaitTime ());
@@ -211,18 +212,18 @@ private static void sleep(int seconds) throws InterruptedException {
211
212
}
212
213
213
214
*/
214
- public static String takeScreenshot (String screenshotName , String screenshotsFolder , String platformName ) throws Exception {
215
+ public static String takeScreenshot (String screenshotName , String screenshotsFolder , PlatformType platform ) throws Exception {
215
216
long start_time = System .nanoTime ();
216
217
217
218
String screenshotFile = screenshotsFolder + screenshotName + ".png" ;
218
219
String fullFileName = System .getProperty ("user.dir" ) + "/" + screenshotFile ;
219
220
220
- if (platformName . equalsIgnoreCase ( "iOS" )) {
221
+ if (platform . equals ( PlatformType . IOS )) {
221
222
takeIDeviceScreenshot (fullFileName );
222
- } else if (platformName . equalsIgnoreCase ( "Android" )) {
223
+ } else if (platform . equals ( PlatformType . ANDROID )) {
223
224
takeAndroidScreenshot (fullFileName );
224
225
} else {
225
- throw new Exception ("Invalid platformName : " +platformName );
226
+ throw new Exception ("Invalid platformType : " +platform );
226
227
}
227
228
228
229
long end_time = System .nanoTime ();
0 commit comments