@@ -25,6 +25,7 @@ public class ByImage extends By {
25
25
private static boolean wasLibraryLoaded = false ;
26
26
private final Mat template ;
27
27
private final String description ;
28
+ private float threshold = 1 - AqualityServices .getConfiguration ().getVisualizationConfiguration ().getDefaultThreshold ();
28
29
29
30
private static void loadLibrary () {
30
31
if (!wasLibraryLoaded ) {
@@ -56,6 +57,27 @@ public ByImage(byte[] bytes) {
56
57
this .template = Imgcodecs .imdecode (new MatOfByte (bytes ), Imgcodecs .IMREAD_UNCHANGED );
57
58
}
58
59
60
+ /**
61
+ * Sets threshold of image similarity.
62
+ * @param threshold a float between 0 and 1, where 1 means 100% match, and 0.5 means 50% match.
63
+ * @return current instance of ByImage locator.
64
+ */
65
+ public ByImage setThreshold (float threshold ) {
66
+ if (threshold < 0 || threshold > 1 ) {
67
+ throw new IllegalArgumentException ("Threshold must be a float between 0 and 1." );
68
+ }
69
+ this .threshold = threshold ;
70
+ return this ;
71
+ }
72
+
73
+ /**
74
+ * Gets threshold of image similarity.
75
+ * @return current value of threshold.
76
+ */
77
+ public float getThreshold () {
78
+ return threshold ;
79
+ }
80
+
59
81
@ Override
60
82
public String toString () {
61
83
return String .format ("ByImage: %s, size: (width:%d, height:%d)" , description , template .width (), template .height ());
@@ -84,7 +106,6 @@ public List<WebElement> findElements(SearchContext context) {
84
106
Mat result = new Mat ();
85
107
Imgproc .matchTemplate (source , template , result , Imgproc .TM_CCOEFF_NORMED );
86
108
87
- float threshold = 1 - AqualityServices .getConfiguration ().getVisualizationConfiguration ().getDefaultThreshold ();
88
109
Core .MinMaxLocResult minMaxLoc = Core .minMaxLoc (result );
89
110
90
111
int matchCounter = Math .abs ((result .width () - template .width () + 1 ) * (result .height () - template .height () + 1 ));
0 commit comments