@@ -27,68 +27,81 @@ private static void log(String message) {
27
27
//The "scene" parameter is the image in which we are looking for "image"
28
28
// "tolerance" sets the required accuracy for the image recognition algorithm.
29
29
public static Point [] findImage (String image , String scene , ImageRecognitionSettingsDTO settings , String platformName , Dimension screenSize ) throws Exception {
30
- Point [] imgRect = new Point [0 ];
31
- Point [] imgRectScaled ;
32
-
33
30
log ("Searching for " + image );
34
31
log ("Searching in " + scene );
35
- try {
36
- imgRect = imageFinder .findImage (image , scene , settings .getTolerance ());
37
- } catch (Exception e ) {
38
- e .printStackTrace ();
39
- }
32
+ Point [] imgRect = findImageUsingAkaze (image , scene , settings );
40
33
41
34
if (imgRect != null ) {
42
-
43
35
if (platformName .equalsIgnoreCase ("iOS" )) {
44
- //for retina devices we need to recalculate coordinates
45
- double sceneHeight = imageFinder .getSceneHeight ();
46
- double sceneWidth = imageFinder .getSceneWidth ();
47
-
48
- int screenHeight = screenSize .getHeight ();
49
- int screenWidth = screenSize .getWidth ();
50
-
51
- // Make sure screenshot size values are "landscape" for comparison
52
- if (sceneHeight > sceneWidth ) {
53
- double temp = sceneHeight ;
54
- sceneHeight = sceneWidth ;
55
- sceneWidth = temp ;
56
- }
57
-
58
- // Make sure screen size values are "landscape" for comparison
59
- if (screenHeight > screenWidth ) {
60
- int temp = screenHeight ;
61
- screenHeight = screenWidth ;
62
- screenWidth = temp ;
63
- }
64
-
65
- if ((screenHeight <sceneHeight ) && (screenWidth <sceneWidth )) {
66
- if ((screenHeight <sceneHeight /2 )&&(screenWidth <sceneWidth /2 )) {
67
- imgRectScaled = new Point []{new Point (imgRect [0 ].x / 3 , imgRect [0 ].y / 3 ), new Point (imgRect [1 ].x / 3 , imgRect [1 ].y / 3 ), new Point (imgRect [2 ].x / 3 , imgRect [2 ].y / 3 ), new Point (imgRect [3 ].x / 3 , imgRect [3 ].y / 3 ), new Point (imgRect [4 ].x / 3 , imgRect [4 ].y / 3 )};
68
- log ("Device with Retina display rendered at x3 => coordinates have been recalculated" );
69
- imgRect = imgRectScaled ;
70
- }
71
- else {
72
- imgRectScaled = new Point []{new Point (imgRect [0 ].x / 2 , imgRect [0 ].y / 2 ), new Point (imgRect [1 ].x / 2 , imgRect [1 ].y / 2 ), new Point (imgRect [2 ].x / 2 , imgRect [2 ].y / 2 ), new Point (imgRect [3 ].x / 2 , imgRect [3 ].y / 2 ), new Point (imgRect [4 ].x / 2 , imgRect [4 ].y / 2 )};
73
- log ("Device with Retina display rendered at x2 => coordinates have been recalculated" );
74
- imgRect = imgRectScaled ;
75
- }
76
- }
36
+ imgRect = scaleImageRectangleForIos (screenSize , imgRect );
77
37
}
78
-
79
38
Point center = imgRect [4 ];
80
-
81
- // Check that found center coordinate isn't out of screen bounds
82
- if ((center .x >= screenSize .width ) || (center .x < 0 ) || (center .y >= screenSize .height ) || (center .y < 0 )) {
39
+ if (!isPointInsideScreenBounds (center , screenSize )) {
83
40
log ("Screen size is (width, height): " + screenSize .getWidth () + ", " + screenSize .getHeight ());
84
41
log ("WARNING: Coordinates found do not match the screen --> image not found." );
85
42
imgRect = null ;
86
- } else {
87
- return imgRect ;
88
43
}
89
44
}
90
- return null ;
45
+ return imgRect ;
91
46
}
47
+
48
+
49
+
50
+ private static Point [] findImageUsingAkaze (String image , String scene , ImageRecognitionSettingsDTO settings ) {
51
+ Point [] imgRect = new Point [0 ];
52
+ try {
53
+ imgRect = imageFinder .findImage (image , scene , settings .getTolerance ());
54
+ } catch (Exception e ) {
55
+ e .printStackTrace ();
56
+ }
57
+ return imgRect ;
58
+ }
59
+
60
+
61
+
62
+ private static Point [] scaleImageRectangleForIos (Dimension screenSize , Point [] imgRect ) {
63
+ Point [] imgRectScaled ;
64
+ //for retina devices we need to recalculate coordinates
65
+ double sceneHeight = imageFinder .getSceneHeight ();
66
+ double sceneWidth = imageFinder .getSceneWidth ();
67
+
68
+ int screenHeight = screenSize .getHeight ();
69
+ int screenWidth = screenSize .getWidth ();
70
+
71
+ // Make sure screenshot size values are "landscape" for comparison
72
+ if (sceneHeight > sceneWidth ) {
73
+ double temp = sceneHeight ;
74
+ sceneHeight = sceneWidth ;
75
+ sceneWidth = temp ;
76
+ }
77
+
78
+ // Make sure screen size values are "landscape" for comparison
79
+ if (screenHeight > screenWidth ) {
80
+ int temp = screenHeight ;
81
+ screenHeight = screenWidth ;
82
+ screenWidth = temp ;
83
+ }
84
+
85
+ if ((screenHeight <sceneHeight ) && (screenWidth <sceneWidth )) {
86
+ if ((screenHeight <sceneHeight /2 )&&(screenWidth <sceneWidth /2 )) {
87
+ imgRectScaled = new Point []{new Point (imgRect [0 ].x / 3 , imgRect [0 ].y / 3 ), new Point (imgRect [1 ].x / 3 , imgRect [1 ].y / 3 ), new Point (imgRect [2 ].x / 3 , imgRect [2 ].y / 3 ), new Point (imgRect [3 ].x / 3 , imgRect [3 ].y / 3 ), new Point (imgRect [4 ].x / 3 , imgRect [4 ].y / 3 )};
88
+ log ("Device with Retina display rendered at x3 => coordinates have been recalculated" );
89
+ imgRect = imgRectScaled ;
90
+ }
91
+ else {
92
+ imgRectScaled = new Point []{new Point (imgRect [0 ].x / 2 , imgRect [0 ].y / 2 ), new Point (imgRect [1 ].x / 2 , imgRect [1 ].y / 2 ), new Point (imgRect [2 ].x / 2 , imgRect [2 ].y / 2 ), new Point (imgRect [3 ].x / 2 , imgRect [3 ].y / 2 ), new Point (imgRect [4 ].x / 2 , imgRect [4 ].y / 2 )};
93
+ log ("Device with Retina display rendered at x2 => coordinates have been recalculated" );
94
+ imgRect = imgRectScaled ;
95
+ }
96
+ }
97
+ return imgRect ;
98
+ }
99
+
100
+
101
+
102
+ private static boolean isPointInsideScreenBounds (Point center , Dimension screenSize ) {
103
+ return !((center .x >= screenSize .width ) || (center .x < 0 ) || (center .y >= screenSize .height ) || (center .y < 0 ));
104
+ }
92
105
93
106
94
107
public static String getTextStringFromImage (String imageInput ) {
0 commit comments