How to Generate and Compare Perceptual Image Hashes in Java
Learn how to generate a hash value for an image and compare two perceptual image hashes using an API in Java.
Join the DZone community and get the full member experience.Join For Free
Perceptual image hashing is a relatively new process used primarily in the multimedia industry for content identification and authentication. The process itself uses an algorithm to extract specific features from an image and calculate a hash value based on that information. The hash value that is generated acts as a kind of ‘fingerprint’ for the image; it is a distinct identifier that is unique to its parent image.
As you may have guessed by the fingerprint comparison, perceptual image hashing is particularly useful for digital forensics, but it has become an important player in prohibiting online copyright infringement as well. By comparing the hash value of an original/authentic image with the hash value of a similar image, you can identify and match various images and calculate the Hamming Distance between them. For reference, Hamming Distance measures the minimum number of substitutions it takes to change one image to the other, so hash values that are closer together are more similar.
To protect your multimedia from copyright infringement, perceptual image hashing may be the ideal technique for the job; it has proven to be effective at not only providing a unique hash value for an image, but also at resisting and detecting malicious manipulation attempts. In this brief tutorial, we will demonstrate how to use APIs in Java to generate a hash value for an image, as well as compare the similarity of two perceptual image hashes in terms of Hamming Distance.
We will begin installing the Maven SDK by adding a reference to the repository in pom.xml:
Then, we can add a reference to the dependency:
Once the installation is complete, we are ready to start using our APIs. For our first function, we will focus on generating a perceptual image hash for a specific image. To ensure the process runs smoothly, we will need to input the following parameters:
- Image File: the image file to perform the operation on; common file formats such as PNG, JPEG, etc. are supported
- API Key: your personal API key; if you need to obtain an API key, you can do so by registering for a free account on the Cloudmersive website. This will provide access to 800 calls/month across the entire API library.
- Recognition Mode (optional): specify the recognition mode; possible values are Normal, Basic, and advanced. Default is Normal.
Once we have our parameters set, we are ready to call the function:
This will return a hash value for your input image, which we could potentially use as an input for our next function. Our second API will allow us to compare the similarity between two hash values by calculating the Hamming distance between them; as I mentioned earlier, hash values that are closer together according to the Hamming distance are more alike. Now, we can call the function with the following code:
For the second function you will only need to input your API key and your image similarity request, which should look like this:
And that’s it! Your result from the image comparison API will be a similarity score that you can use to determine how alike or different the images are. Between these two APIs, you can automate what would normally be a time-consuming and manual process of sifting through images in a database.
Opinions expressed by DZone contributors are their own.