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

Commit c73add8

Browse files
author
Severi Haverila
committed
general refactoring
1 parent f42efc2 commit c73add8

File tree

1 file changed

+62
-49
lines changed

1 file changed

+62
-49
lines changed

src/main/java/library/ImageRecognition.java

Lines changed: 62 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,68 +27,81 @@ private static void log(String message) {
2727
//The "scene" parameter is the image in which we are looking for "image"
2828
// "tolerance" sets the required accuracy for the image recognition algorithm.
2929
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-
3330
log("Searching for " + image);
3431
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);
4033

4134
if (imgRect != null) {
42-
4335
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);
7737
}
78-
7938
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)) {
8340
log("Screen size is (width, height): " + screenSize.getWidth() + ", " + screenSize.getHeight());
8441
log("WARNING: Coordinates found do not match the screen --> image not found.");
8542
imgRect = null;
86-
} else {
87-
return imgRect;
8843
}
8944
}
90-
return null;
45+
return imgRect;
9146
}
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+
}
92105

93106

94107
public static String getTextStringFromImage(String imageInput) {

0 commit comments

Comments
 (0)