1+ /*
2+ * Licensed under the Apache License, Version 2.0 (the "License");
3+ * you may not use this file except in compliance with the License.
4+ * See the NOTICE file distributed with this work for additional
5+ * information regarding copyright ownership.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package io .appium .java_client ;
18+
19+ import static io .appium .java_client .MobileCommand .compareImagesCommand ;
20+
21+ import io .appium .java_client .imagecomparison .ComparisonMode ;
22+ import io .appium .java_client .imagecomparison .FeaturesMatchingOptions ;
23+ import io .appium .java_client .imagecomparison .FeaturesMatchingResult ;
24+ import io .appium .java_client .imagecomparison .OccurrenceMatchingOptions ;
25+ import io .appium .java_client .imagecomparison .OccurrenceMatchingResult ;
26+ import io .appium .java_client .imagecomparison .SimilarityMatchingOptions ;
27+ import io .appium .java_client .imagecomparison .SimilarityMatchingResult ;
28+ import org .apache .commons .codec .binary .Base64 ;
29+ import org .apache .commons .io .FileUtils ;
30+
31+ import java .io .File ;
32+ import java .io .IOException ;
33+ import java .util .Map ;
34+ import javax .annotation .Nullable ;
35+
36+ public interface ComparesImages extends ExecutesMethod {
37+
38+ /**
39+ * Performs images matching by features with default options. Read
40+ * https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_matcher/py_matcher.html
41+ * for more details on this topic.
42+ *
43+ * @param base64image1 base64-encoded representation of the first image
44+ * @param base64Image2 base64-encoded representation of the second image
45+ * @return The matching result.
46+ */
47+ default FeaturesMatchingResult matchImagesFeatures (byte [] base64image1 , byte [] base64Image2 ) {
48+ return matchImagesFeatures (base64image1 , base64Image2 , null );
49+ }
50+
51+ /**
52+ * Performs images matching by features. Read
53+ * https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_matcher/py_matcher.html
54+ * for more details on this topic.
55+ *
56+ * @param base64image1 base64-encoded representation of the first image
57+ * @param base64Image2 base64-encoded representation of the second image
58+ * @param options comparison options
59+ * @return The matching result. The configuration of fields in the result depends on comparison options.
60+ */
61+ default FeaturesMatchingResult matchImagesFeatures (byte [] base64image1 , byte [] base64Image2 ,
62+ @ Nullable FeaturesMatchingOptions options ) {
63+ Object response = CommandExecutionHelper .execute (this ,
64+ compareImagesCommand (ComparisonMode .MATCH_FEATURES , base64image1 , base64Image2 , options ));
65+ //noinspection unchecked
66+ return new FeaturesMatchingResult ((Map <String , Object >) response );
67+ }
68+
69+ /**
70+ * Performs images matching by features with default options. Read
71+ * https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_matcher/py_matcher.html
72+ * for more details on this topic.
73+ *
74+ * @param image1 The location of the first image
75+ * @param image2 The location of the second image
76+ * @return The matching result.
77+ */
78+ default FeaturesMatchingResult matchImagesFeatures (File image1 , File image2 ) throws IOException {
79+ return matchImagesFeatures (image1 , image2 , null );
80+ }
81+
82+ /**
83+ * Performs images matching by features. Read
84+ * https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_matcher/py_matcher.html
85+ * for more details on this topic.
86+ *
87+ * @param image1 The location of the first image
88+ * @param image2 The location of the second image
89+ * @param options comparison options
90+ * @return The matching result. The configuration of fields in the result depends on comparison options.
91+ */
92+ default FeaturesMatchingResult matchImagesFeatures (File image1 , File image2 ,
93+ @ Nullable FeaturesMatchingOptions options ) throws IOException {
94+ return matchImagesFeatures (Base64 .encodeBase64 (FileUtils .readFileToByteArray (image1 )),
95+ Base64 .encodeBase64 (FileUtils .readFileToByteArray (image2 )), options );
96+ }
97+
98+ /**
99+ * Performs images matching by template to find possible occurrence of the partial image
100+ * in the full image with default options. Read
101+ * https://docs.opencv.org/2.4/doc/tutorials/imgproc/histograms/template_matching/template_matching.html
102+ * for more details on this topic.
103+ *
104+ * @param fullImage base64-encoded representation of the full image
105+ * @param partialImage base64-encoded representation of the partial image
106+ * @return The matching result.
107+ */
108+ default OccurrenceMatchingResult findImageOccurrence (byte [] fullImage , byte [] partialImage ) {
109+ return findImageOccurrence (fullImage , partialImage , null );
110+ }
111+
112+ /**
113+ * Performs images matching by template to find possible occurrence of the partial image
114+ * in the full image. Read
115+ * https://docs.opencv.org/2.4/doc/tutorials/imgproc/histograms/template_matching/template_matching.html
116+ * for more details on this topic.
117+ *
118+ * @param fullImage base64-encoded representation of the full image
119+ * @param partialImage base64-encoded representation of the partial image
120+ * @param options comparison options
121+ * @return The matching result. The configuration of fields in the result depends on comparison options.
122+ */
123+ default OccurrenceMatchingResult findImageOccurrence (byte [] fullImage , byte [] partialImage ,
124+ @ Nullable OccurrenceMatchingOptions options ) {
125+ Object response = CommandExecutionHelper .execute (this ,
126+ compareImagesCommand (ComparisonMode .MATCH_TEMPLATE , fullImage , partialImage , options ));
127+ //noinspection unchecked
128+ return new OccurrenceMatchingResult ((Map <String , Object >) response );
129+ }
130+
131+ /**
132+ * Performs images matching by template to find possible occurrence of the partial image
133+ * in the full image with default options. Read
134+ * https://docs.opencv.org/2.4/doc/tutorials/imgproc/histograms/template_matching/template_matching.html
135+ * for more details on this topic.
136+ *
137+ * @param fullImage The location of the full image
138+ * @param partialImage The location of the partial image
139+ * @return The matching result. The configuration of fields in the result depends on comparison options.
140+ */
141+ default OccurrenceMatchingResult findImageOccurrence (File fullImage , File partialImage ) throws IOException {
142+ return findImageOccurrence (fullImage , partialImage , null );
143+ }
144+
145+ /**
146+ * Performs images matching by template to find possible occurrence of the partial image
147+ * in the full image. Read
148+ * https://docs.opencv.org/2.4/doc/tutorials/imgproc/histograms/template_matching/template_matching.html
149+ * for more details on this topic.
150+ *
151+ * @param fullImage The location of the full image
152+ * @param partialImage The location of the partial image
153+ * @param options comparison options
154+ * @return The matching result. The configuration of fields in the result depends on comparison options.
155+ */
156+ default OccurrenceMatchingResult findImageOccurrence (File fullImage , File partialImage ,
157+ @ Nullable OccurrenceMatchingOptions options )
158+ throws IOException {
159+ return findImageOccurrence (Base64 .encodeBase64 (FileUtils .readFileToByteArray (fullImage )),
160+ Base64 .encodeBase64 (FileUtils .readFileToByteArray (partialImage )), options );
161+ }
162+
163+ /**
164+ * Performs images matching to calculate the similarity score between them
165+ * with default options. The flow there is similar to the one used in
166+ * {@link #findImageOccurrence(byte[], byte[], OccurrenceMatchingOptions)},
167+ * but it is mandatory that both images are of equal size.
168+ *
169+ * @param base64image1 base64-encoded representation of the first image
170+ * @param base64Image2 base64-encoded representation of the second image
171+ * @return Matching result. The configuration of fields in the result depends on comparison options.
172+ */
173+ default SimilarityMatchingResult getImagesSimilarity (byte [] base64image1 , byte [] base64Image2 ) {
174+ return getImagesSimilarity (base64image1 , base64Image2 , null );
175+ }
176+
177+ /**
178+ * Performs images matching to calculate the similarity score between them.
179+ * The flow there is similar to the one used in
180+ * {@link #findImageOccurrence(byte[], byte[], OccurrenceMatchingOptions)},
181+ * but it is mandatory that both images are of equal size.
182+ *
183+ * @param base64image1 base64-encoded representation of the first image
184+ * @param base64Image2 base64-encoded representation of the second image
185+ * @param options comparison options
186+ * @return Matching result. The configuration of fields in the result depends on comparison options.
187+ */
188+ default SimilarityMatchingResult getImagesSimilarity (byte [] base64image1 , byte [] base64Image2 ,
189+ @ Nullable SimilarityMatchingOptions options ) {
190+ Object response = CommandExecutionHelper .execute (this ,
191+ compareImagesCommand (ComparisonMode .GET_SIMILARITY , base64image1 , base64Image2 , options ));
192+ //noinspection unchecked
193+ return new SimilarityMatchingResult ((Map <String , Object >) response );
194+ }
195+
196+ /**
197+ * Performs images matching to calculate the similarity score between them
198+ * with default options. The flow there is similar to the one used in
199+ * {@link #findImageOccurrence(byte[], byte[], OccurrenceMatchingOptions)},
200+ * but it is mandatory that both images are of equal size.
201+ *
202+ * @param image1 The location of the full image
203+ * @param image2 The location of the partial image
204+ * @return Matching result. The configuration of fields in the result depends on comparison options.
205+ */
206+ default SimilarityMatchingResult getImagesSimilarity (File image1 , File image2 ) throws IOException {
207+ return getImagesSimilarity (image1 , image2 , null );
208+ }
209+
210+ /**
211+ * Performs images matching to calculate the similarity score between them.
212+ * The flow there is similar to the one used in
213+ * {@link #findImageOccurrence(byte[], byte[], OccurrenceMatchingOptions)},
214+ * but it is mandatory that both images are of equal size.
215+ *
216+ * @param image1 The location of the full image
217+ * @param image2 The location of the partial image
218+ * @param options comparison options
219+ * @return Matching result. The configuration of fields in the result depends on comparison options.
220+ */
221+ default SimilarityMatchingResult getImagesSimilarity (File image1 , File image2 ,
222+ @ Nullable SimilarityMatchingOptions options )
223+ throws IOException {
224+ return getImagesSimilarity (Base64 .encodeBase64 (FileUtils .readFileToByteArray (image1 )),
225+ Base64 .encodeBase64 (FileUtils .readFileToByteArray (image2 )), options );
226+ }
227+ }
0 commit comments