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 FreePerceptual 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:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then, we can add a reference to the dependency:
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.90</version>
</dependency>
</dependencies>
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:
xxxxxxxxxx
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.RecognizeApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
RecognizeApi apiInstance = new RecognizeApi();
File imageFile = new File("/path/to/inputfile"); // File | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
String recognitionMode = "recognitionMode_example"; // String | Optional, specify the recognition mode; possible values are Normal, Basic and Advanced. Default is Normal.
try {
ImageSimilarityHashResponse result = apiInstance.recognizeSimilarityHash(imageFile, recognitionMode);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling RecognizeApi#recognizeSimilarityHash");
e.printStackTrace();
}
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:
x
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.RecognizeApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
RecognizeApi apiInstance = new RecognizeApi();
ImageSimilarityHashDistanceRequest request = new ImageSimilarityHashDistanceRequest(); // ImageSimilarityHashDistanceRequest |
try {
ImageSimilarityHashDistanceResponse result = apiInstance.recognizeSimilarityHashDistance(request);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling RecognizeApi#recognizeSimilarityHashDistance");
e.printStackTrace();
}
For the second function you will only need to input your API key and your image similarity request, which should look like this:
xxxxxxxxxx
{
"ImageHash1": "string",
"ImageHash2": "string"
}
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.
Comments