How to use the OCR SDK with C++ and libCurl. The purpose of this article is to teach you how to use C++ to do OCR with the OCR SDK.
You should see these two lines of placeholders in our code.
C++
#define APP_ID "<Your Application ID>"
#define PASSWORD "<Your Application Password"
1
2
3
4
5
Copy the code
Copy code type: [CPP] Replace these strings with your assigned application ID and application password.
Start the libCurl
First, we launch the Curl object: assword.
C++
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init(a);1
2
3
4
5
Copy the code
Copy code type: [CPP] processes input files
Our input file can be any image, even a PDF file. We need to convert it to MIME format, to do this we call:
C++
<a href="https://curl.se/libcurl/c/curl_mime_init.html">curl_mime_init()</a>
1
2
Copy the code
Copy code type: [CPP] Next, we generate the uploaded part of the request.
C++
field = curl_mime_addpart(form);
curl_mime_name(field, "upload");
1
2
3
4
5
Copy the code
Copy code type: [CPP] and use curl_mine_fileData () to generate filedata, which is used to set the body data of our MIME part from the content of the input file.
C++
curl_mime_filedata(field, file_to_upload.c_str());
1
2
3
4
Copy the code
Copy code type: [CPP] Now we set the options by calling curl_easy_setopt(), which, as its name implies, prepares the Settings options for our request.
We need the following properties.
● PROCESSING_URL is the URL given by the Abbyy SDK.
● HeaderList is already set up in the front.
● Form is the upload part.
● APP_ID is the application-specific identification that the Abbyy SDK provides for each piece of software we develop.
● PASSWORD is the PASSWORD of the application and needs to be generated.
● CURLOPT_WRITEFUNCTION is a callback function used to write the result of a request. The data is written to the readBuffer, which holds the results we received from the API. APP_ID is an application specific identifying provided by Abbyy SDK per each software we develop.
C++
curl_easy_setopt(curl, CURLOPT_URL, PROCESSING_URL);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);
curl_easy_setopt(curl, CURLOPT_USERNAME, APP_ID);
curl_easy_setopt(curl, CURLOPT_PASSWORD, PASSWORD);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWrite_CallbackFunc_StdString);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
1
2
3
4
5
6
7
8
Copy the code
Copy code type: [CPP] Then we are ready to execute our request. We call curl_easy_perform().
C++
res = curl_easy_perform(curl);
1
2
Copy the code
Copy code type: [CPP] Next we check the result.
Check the result
Next, we get the task ID, which is assigned to each OCR task. You can start several tasks and wait for each task to complete. We use the Task_ID we obtained to check the status of the task and wait for it to complete.
C++
while (1)
{
res = curl_easy_perform(status_curl);
if(res ! = CURLE_OK) {WriteLogFile(L"Error: %S".curl_easy_strerror(res));
}
else
{
WriteLogFile(L"Read Buffer:\n%S", readBuffer.c_str());
task_status = ObtainStatus(readBuffer);
WriteLogFile(L"task_status: %S", task_status.c_str());
}
if(task_status ! ="Completed")
{
//wait 5s before next check
Sleep(2000);
}
else
{
setcolor(LOG_COLOR_DARKGREEN, 0);
SetConsoleTitle(L"OCR completed");
setcolor(LOG_COLOR_WHITE, 0);
result_url = ObtainURL(readBuffer);
//replace all & to &
result_url = ReplaceAll(result_url, "&"."&");
//downloading text file of response
WriteLogFile(L"Downloading File from URL: %S", result_url.c_str());
op_curl = curl_easy_init(a);if (op_curl)
{
headerlist = curl_slist_append(headerlist, buf);
curl_easy_setopt(op_curl, CURLOPT_URL, result_url.c_str());
curl_easy_setopt(op_curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(op_curl, CURLOPT_HTTPGET, 1L);
FILE* wfd = fopen(json_result_file.c_str(), "ab");
fprintf(wfd, "\n");
curl_easy_setopt(op_curl, CURLOPT_WRITEDATA, wfd);
curl_easy_perform(op_curl);
curl_easy_cleanup(op_curl);
fclose(wfd);
WriteLogFile(L"FILE saved");
}
break;
}
readBuffer = "";
} // While
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Copy the code
Copy code type: [CPP] Now, once we have the results, we just need to clean everything up.
Building blocks
One of the key building blocks of this project is libCurl. I use it as a static library. The.lib file is included with the article’s source code, but you can read about using libCurl as a static library here.
Note: WriteLogFile() is an old logging function that I described in this article.
Use the code
You can use different export formats. Look at the options in this link.
You can define the language you want. Look at the options in this link.
You can use many languages, most of which also work as written text (ICR). You set the list of expected languages in the PROCESSING_URL string.
C++
#define PROCESSING_URL
"https://cloud-westus.ocrsdk.com/processImageexportFormat=txt&language=English,Hebrew#define PROCESSING_URL "https://cloud-westus.ocrsdk.com/processImageexportFormat=txt&language=English,Hebrew
Copy the code
Free C++ resource pack