23
23
public class ByImage extends By {
24
24
private static boolean wasLibraryLoaded = false ;
25
25
private final Mat template ;
26
+ private final boolean doScaling ;
26
27
27
28
private static void loadLibrary () {
28
29
if (!wasLibraryLoaded ) {
@@ -38,8 +39,19 @@ private static void loadLibrary() {
38
39
* @param file image file to locate element by.
39
40
*/
40
41
public ByImage (File file ) {
42
+ this (file , false );
43
+ }
44
+
45
+ /**
46
+ * Constructor accepting image file.
47
+ *
48
+ * @param file image file to locate element by.
49
+ * @param doScaling perform screenshot scaling if devicePixelRatio != 1
50
+ */
51
+ public ByImage (File file , boolean doScaling ) {
41
52
loadLibrary ();
42
53
this .template = Imgcodecs .imread (file .getAbsolutePath (), Imgcodecs .IMREAD_UNCHANGED );
54
+ this .doScaling = doScaling ;
43
55
}
44
56
45
57
/**
@@ -48,8 +60,19 @@ public ByImage(File file) {
48
60
* @param bytes image bytes to locate element by.
49
61
*/
50
62
public ByImage (byte [] bytes ) {
63
+ this (bytes , false );
64
+ }
65
+
66
+ /**
67
+ * Constructor accepting image file.
68
+ *
69
+ * @param bytes image bytes to locate element by.
70
+ * @param doScaling perform screenshot scaling if devicePixelRatio != 1
71
+ */
72
+ public ByImage (byte [] bytes , boolean doScaling ) {
51
73
loadLibrary ();
52
74
this .template = Imgcodecs .imdecode (new MatOfByte (bytes ), Imgcodecs .IMREAD_UNCHANGED );
75
+ this .doScaling = doScaling ;
53
76
}
54
77
55
78
@ Override
@@ -61,6 +84,12 @@ public String toString() {
61
84
public List <WebElement > findElements (SearchContext context ) {
62
85
byte [] screenshotBytes = getScreenshot (context );
63
86
Mat source = Imgcodecs .imdecode (new MatOfByte (screenshotBytes ), Imgcodecs .IMREAD_UNCHANGED );
87
+ long devicePixelRatio = (long ) AqualityServices .getBrowser ().executeScript (JavaScript .GET_DEVICE_PIXEL_RATIO );
88
+ if (devicePixelRatio != 1 && doScaling ) {
89
+ int scaledWidth = (int ) (source .width () / devicePixelRatio );
90
+ int scaledHeight = (int ) (source .height () / devicePixelRatio );
91
+ Imgproc .resize (source , source , new Size (scaledWidth , scaledHeight ), 0 , 0 , Imgproc .INTER_AREA );
92
+ }
64
93
Mat result = new Mat ();
65
94
Imgproc .matchTemplate (source , template , result , Imgproc .TM_CCOEFF_NORMED );
66
95
0 commit comments