@@ -22,7 +22,21 @@ const DEFAULT_MATCH_THRESHOLD = 0.4;
22
22
* @param {string } secondImage - Base64-encoded image file.
23
23
* All image formats, that OpenCV library itself accepts, are supported.
24
24
* @param {?Object } options [{}] - The content of this dictionary depends
25
- * on the actual `mode` value. See the documentation on `appium-support`
25
+ * on the actual `mode` value.
26
+ * For MATCH_TEMPLATE_MODE:
27
+ * - visualize: include the visualization of the match in the result
28
+ * (default: false)
29
+ * - threshold: the image match threshold, higher values
30
+ * require a closer image similarity to match
31
+ * (default: 0.5)
32
+ * - multiple: return multiple matches in the image
33
+ * (default: false)
34
+ * - matchNeighbourThreshold: if multiple is specified,
35
+ * determine the number of pixels within
36
+ * which to consider a pixel as part of the
37
+ * same match result
38
+ * (default: 10)
39
+ * See the documentation in the `appium-support`
26
40
* module for more details.
27
41
* @returns {Object } The content of the resulting dictionary depends
28
42
* on the actual `mode` and `options` values. See the documentation on
@@ -34,7 +48,8 @@ const DEFAULT_MATCH_THRESHOLD = 0.4;
34
48
commands . compareImages = async function compareImages ( mode , firstImage , secondImage , options = { } ) {
35
49
const img1 = Buffer . from ( firstImage , 'base64' ) ;
36
50
const img2 = Buffer . from ( secondImage , 'base64' ) ;
37
- let result = { } ;
51
+ let result = null ;
52
+
38
53
switch ( _ . toLower ( mode ) ) {
39
54
case MATCH_FEATURES_MODE . toLowerCase ( ) :
40
55
result = await imageUtil . getImagesMatches ( img1 , img2 , options ) ;
@@ -45,17 +60,34 @@ commands.compareImages = async function compareImages (mode, firstImage, secondI
45
60
case MATCH_TEMPLATE_MODE . toLowerCase ( ) :
46
61
// firstImage/img1 is the full image and secondImage/img2 is the partial one
47
62
result = await imageUtil . getImageOccurrence ( img1 , img2 , options ) ;
63
+
64
+ if ( options . multiple ) {
65
+ return result . multiple . map ( convertVisualizationToBase64 ) ;
66
+ }
48
67
break ;
49
68
default :
50
69
throw new errors . InvalidArgumentError ( `'${ mode } ' images comparison mode is unknown. ` +
51
70
`Only ${ JSON . stringify ( [ MATCH_FEATURES_MODE , GET_SIMILARITY_MODE , MATCH_TEMPLATE_MODE ] ) } modes are supported.` ) ;
52
71
}
53
- if ( ! _ . isEmpty ( result . visualization ) ) {
54
- result . visualization = result . visualization . toString ( 'base64' ) ;
55
- }
56
- return result ;
72
+
73
+ return convertVisualizationToBase64 ( result ) ;
57
74
} ;
58
75
76
+ /**
77
+ * base64 encodes the visualization part of the result
78
+ * (if necessary)
79
+ *
80
+ * @param {OccurenceResult } element - occurrence result
81
+ *
82
+ **/
83
+ function convertVisualizationToBase64 ( element ) {
84
+ if ( _ . isString ( element . visualization ) ) {
85
+ element . visualization = element . visualization . toString ( 'base64' ) ;
86
+ }
87
+
88
+ return element ;
89
+ }
90
+
59
91
Object . assign ( extensions , commands , helpers ) ;
60
92
export { commands , helpers , DEFAULT_MATCH_THRESHOLD , MATCH_TEMPLATE_MODE } ;
61
93
export default extensions ;
0 commit comments