How to Generate a Hash Value for an Image in C/C++
Utilizing perceptual image hashing to generate a hash value can be a great tool for digital forensics and cases of online copyright infringement. This is a more modern and secure approach than a watermark, but unfortunately the lengthy processing has the potential to cut into your productivity. In this tutorial, we will discuss how to use an API in C/C++ to generate a perceptual image hash value for a specific image; hash values that are closer together in terms of Hamming Distance are more similar.
To use the API, we will first install libcurl in our C/C++ project:
libcurl/7.75.0
Next, we can call the function with the following code:
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.cloudmersive.com/image/recognize/similarity/hash");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "recognitionMode: <string>");
headers = curl_slist_append(headers, "Content-Type: multipart/form-data");
headers = curl_slist_append(headers, "Apikey: YOUR-API-KEY-HERE");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_mime *mime;
curl_mimepart *part;
mime = curl_mime_init(curl);
part = curl_mime_addpart(mime);
curl_mime_name(part, "imageFile");
curl_mime_filedata(part, "/path/to/file");
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
res = curl_easy_perform(curl);
curl_mime_free(mime);
}
curl_easy_cleanup(curl);
Your hash value will be delivered in a matter of moments and can be used to compare with other hash values. To retrieve your API key, head to the Cloudmersive website to register for a free account.