|
6 | 6 | import org.slf4j.Logger;
|
7 | 7 | import org.slf4j.LoggerFactory;
|
8 | 8 |
|
| 9 | +import dtos.ImageRecognitionSettingsDTO; |
| 10 | +import dtos.ImageSearchDTO; |
| 11 | + |
9 | 12 | import java.io.*;
|
10 | 13 |
|
11 | 14 | import static org.junit.Assert.assertNotNull;
|
|
17 | 20 | public class TestdroidImageRecognition extends AbstractAppiumTest {
|
18 | 21 |
|
19 | 22 | public Logger logger = LoggerFactory.getLogger(TestdroidImageRecognition.class);
|
20 |
| - private final int DEFAULT_RETRIES = 5; |
21 |
| - private final int DEFAULT_RETRY_WAIT = 0; |
22 |
| - private final double DEFAULT_TOLERANCE = 0.6; |
23 |
| - private final boolean DEFAULT_WITH_ASSERT = true; |
24 |
| - private final boolean DEFAULT_TAKE_SCREENSHOT = true; |
25 |
| - private final boolean DEFAULT_CROP = false; |
| 23 | + |
26 | 24 | AkazeImageFinder imageFinder = new AkazeImageFinder();
|
27 | 25 |
|
28 | 26 | private String queryimageFolder = "";
|
@@ -200,62 +198,52 @@ public void tapAtRelativeCoordinates(double x_offset, double y_offset) throws Ex
|
200 | 198 | // "image" is the searched image name
|
201 | 199 | // "retries" is the number of times the method will try to find the searched image. If not set, default is 5.
|
202 | 200 | // "tolerance" sets the required accuracy for the image recognition algorithm.
|
203 |
| - // "with_assert" specifies if the method will return a fail or not if the searched image is not found on the screen. Use findImageOnScreenNoAssert() to have this set by default to FALSE |
204 |
| - public Point[] findImageOnScreen(String image, int retries, int retryWait, double tolerance, boolean withAssert, boolean take_screenshot, boolean crop) throws Exception { |
205 |
| - Point[] imgRect = null; |
206 |
| - boolean new_step = true; |
207 |
| - long start_time = System.nanoTime(); |
208 |
| - int originalRetries = retries; |
209 |
| - while ((retries > 0) && (imgRect == null)) { |
210 |
| - if (retries < originalRetries) { |
211 |
| - if (retryWait > 0) { |
212 |
| - log("retryWait given, sleeping " + retryWait + " seconds."); |
213 |
| - sleep(retryWait); |
214 |
| - } |
215 |
| - new_step = false; |
216 |
| - } |
217 |
| - |
218 |
| - log("Find image started, retries left: " + retries); |
219 |
| - if (take_screenshot) |
220 |
| - takeScreenshot(image + "_screenshot"); |
221 |
| - |
222 |
| - // queryImageFolder is "", unless set by setQueryImageFolder() |
223 |
| - String queryImageFile = "queryimages/" + queryimageFolder + image + "_screenshot"; |
224 |
| - String screenshotFile = screenshotsFolder+image + "_screenshot"; |
225 |
| - |
226 |
| - imgRect = findImage(queryImageFile, screenshotFile, tolerance); |
227 |
| - retries = retries - 1; |
228 |
| - } |
229 |
| - |
230 |
| - long end_time = System.nanoTime(); |
231 |
| - int difference = (int) ((end_time - start_time) / 1e6 / 1000); |
232 |
| - log("==> Find image took: " + difference + " secs."); |
233 |
| - |
234 |
| - if (withAssert) { |
235 |
| - assertNotNull("Image " + image + " not found on screen.", imgRect); |
236 |
| - } |
237 |
| - |
238 |
| - if (crop) { |
| 201 | + public Point[] findImageOnScreen(String image, ImageRecognitionSettingsDTO settings) throws Exception { |
| 202 | + ImageSearchDTO foundImage = findImageLoop(image, settings); |
| 203 | + if (foundImage.isFound() && settings.isCrop()) { |
| 204 | + Point[] imgRect = foundImage.getImageRectangle(); |
239 | 205 | Point top_left = imgRect[0];
|
240 | 206 | Point top_right = imgRect[1];
|
241 | 207 | Point bottom_left = imgRect[2];
|
242 | 208 | Point center = imgRect[4];
|
243 |
| - imageFinder.cropImage(screenshotsFolder + getScreenshotsCounter() + "_" + image + "_screenshot" + getRetryCounter() + "_" + timeDifferenceStartTest + "sec", top_left.x, top_left.y, top_right.x - top_left.x, bottom_left.y - top_left.y); |
| 209 | + imageFinder.cropImage(foundImage.getScreenshotFile(), top_left.x, top_left.y, top_right.x - top_left.x, bottom_left.y - top_left.y); |
244 | 210 | }
|
245 |
| - return imgRect; |
| 211 | + return foundImage.getImageRectangle(); |
246 | 212 | }
|
247 | 213 |
|
248 |
| - public Point[] findImageOnScreen(String image, int retries, int retryWait, double tolerance, boolean withAssert, boolean takeScreenshot) throws Exception { |
249 |
| - return findImageOnScreen(image, retries, retryWait, tolerance, withAssert, takeScreenshot, DEFAULT_CROP); |
250 |
| - } |
251 |
| - |
252 |
| - |
253 |
| - public Point[] findImageOnScreen(String image) throws Exception { |
254 |
| - return findImageOnScreen(image, DEFAULT_RETRIES, DEFAULT_RETRY_WAIT, DEFAULT_TOLERANCE, DEFAULT_WITH_ASSERT, DEFAULT_TAKE_SCREENSHOT, DEFAULT_CROP); |
255 |
| - } |
| 214 | + private ImageSearchDTO findImageLoop(String image, ImageRecognitionSettingsDTO settings) throws InterruptedException, IOException, Exception { |
| 215 | + long start_time = System.nanoTime(); |
| 216 | + ImageSearchDTO foundImageDto = new ImageSearchDTO(); |
| 217 | + for (int i = 0; i < settings.getRetries(); i++) { |
| 218 | + // queryImageFolder is "", unless set by setQueryImageFolder() |
| 219 | + String queryImageFile = "queryimages/" + queryimageFolder + image + "_screenshot"; |
| 220 | + String screenshotFile = takeScreenshot(image + "_screenshot"); |
| 221 | + Point[] imgRect = findImage(queryImageFile, screenshotFile, settings.getTolerance()); |
| 222 | + if (imgRect!=null){ |
| 223 | + long end_time = System.nanoTime(); |
| 224 | + int difference = (int) ((end_time - start_time) / 1e6 / 1000); |
| 225 | + log("==> Find image took: " + difference + " secs."); |
| 226 | + |
| 227 | + foundImageDto.setImageRectangle(imgRect); |
| 228 | + foundImageDto.setScreenshotFile(screenshotFile); |
| 229 | + return foundImageDto; |
| 230 | + } |
| 231 | + retryWait(settings); |
| 232 | + } |
| 233 | + log("==> Image not found"); |
| 234 | + return foundImageDto; |
| 235 | + } |
| 236 | + |
| 237 | + private void retryWait(ImageRecognitionSettingsDTO settings) throws InterruptedException { |
| 238 | + if (settings.getRetryWaitTime() > 0) { |
| 239 | + log("retryWait given, sleeping " + settings.getRetryWaitTime() + " seconds."); |
| 240 | + sleep(settings.getRetryWaitTime()); |
| 241 | + } |
| 242 | + } |
256 | 243 |
|
257 |
| - public Point[] findImageOnScreen(String image, boolean take_screenshot) throws Exception { |
258 |
| - return findImageOnScreen(image, DEFAULT_RETRIES, DEFAULT_RETRY_WAIT, DEFAULT_TOLERANCE, DEFAULT_WITH_ASSERT, take_screenshot, DEFAULT_CROP); |
| 244 | + public Point[] findImageOnScreen(String image) throws Exception { |
| 245 | + ImageRecognitionSettingsDTO defaultSettings = new ImageRecognitionSettingsDTO(); |
| 246 | + return findImageOnScreen(image, defaultSettings); |
259 | 247 | }
|
260 | 248 |
|
261 | 249 | //Searches for an image until it disappears from the current view. Good for checking if a loading screen has disappeared.
|
|
0 commit comments