Implementing a Perceptual Hash Algorithm for UI Image Similarity in Android Automation
The article explains how to use a perceptual hash algorithm to generate image fingerprints, compare them to assess UI screen similarity, and integrate this method with UIAutomator for automated decision‑making in the 360 testing project, reducing computational complexity compared to naive double‑loop approaches.
In the 360 testing project, it is necessary to compare the similarity of two UI screens and perform corresponding actions based on the result.
A naive double‑loop comparison has high time complexity; introducing a hash algorithm before traversal can significantly reduce execution time.
The perceptual hash algorithm creates a fingerprint string for each image; comparing these strings yields a similarity measure, where a smaller difference indicates higher similarity.
Implementation steps include:
Resize the image to 8×8 pixels (64 total pixels).
Convert the RGB image to grayscale using the weighted formula Gray = 0.299 R + 0.587 G + 0.114 B.
Calculate the average gray value of the 64 pixels.
Generate a 64‑bit binary value by setting each bit to 1 if the pixel’s gray value is greater than or equal to the average, otherwise 0.
The resulting binary string is the image’s hash (fingerprint).
Compare the two hashes; a diffNum < 5 indicates the images are very similar, while diffNum > 10 means they are completely different.
By capturing screenshots with UIAutomator before and after UI changes, the similarity score can be used to decide whether further actions are needed.
Reference implementation can be found at: https://github.com/gavinliu/SimilarPhoto/blob/master/app/src/main/java/cn/gavinliu/similar/photo/SimilarPhoto.java
360 Quality & Efficiency
360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.