User Guide for C++ Language
In this guide, you will learn step by step on how to build a document normalization application with Dynamsoft Document Normalizer SDK using C++ language.
Requirements
- Operating System:
- Windows 7, 8, 10, 11
- Windows Server 2003, 2008, 2008 R2, 2012, 2016, 2019, 2022
- Linux x64: Ubuntu 14.04.4+ LTS, Debian 8+, etc
- Developing Tool
- Visual Studio 2008 or above
- G++ 5.4+
Installation
If you haven’t downloaded the SDK yet, download the C/C++ Package
now and unpack the package into the directory of your choice.
For this tutorial, we unpack it to
[INSTALLATION FOLDER]
, change it to your unpacking path for the following content.
Build Your First Application
Let’s start by creating a console application which demonstrates how to use the minimum code to detect and normalize document from an image file.
Create a New Project
- For Windows
-
Open Visual Studio. Go to File > New > Project, create a new Empty Project and set Project name as
DDNCPPSample
. -
Add a new source file named
DDNCPPSample.cpp
into the project.
- For Linux
- Create a new source file named
DDNCPPSample.cpp
and place it into the folder[INSTALLATION FOLDER]/Samples
.
Include the Library
-
Add headers and libs in
DDNCPPSample.cpp
.#include <iostream> #include "[INSTALLATION FOLDER]/Include/DynamsoftDocumentNormalizer.h" using namespace std; using namespace dynamsoft::ddn; using namespace dynamsoft::core; #if defined(_WIN64) || defined(_WIN32) #ifdef _WIN64 #pragma comment(lib, "[INSTALLATION FOLDER]/Lib/Windows/x64/DynamsoftCorex64.lib") #pragma comment(lib, "[INSTALLATION FOLDER]/Lib/Windows/x64/DynamsoftDocumentNormalizerx64.lib") #else #pragma comment(lib, "[INSTALLATION FOLDER]/Lib/Windows/x86/DynamsoftCorex86.lib") #pragma comment(lib, "[INSTALLATION FOLDER]/Lib/Windows/x86/DynamsoftDocumentNormalizerx86.lib") #endif #endif
Initialize a Document Normalizer Instance
-
Initialize the license key.
char szErrorMsg[256]; CLicenseManager::InitLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", szErrorMsg, 256);
The string “DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9” here is a free public trial license. Note that network connection is required for this license to work. When it expires, you can request a 30-day trial license via the Request a Trial License link.
-
Create an instance of Dynamsoft Document Normalizer.
CDocumentNormalizer ddn;
Detect and Save the Normalized Document
-
Apply normalization for an image file.
int errorCode = 0; CNormalizedImageResult* normalizedResult = NULL; errorCode = ddn.Normalize("[INSTALLATION FOLDER]/Images/sample-image.png", "", NULL, &normalizedResult); if(errorCode != DM_OK) cout << DC_GetErrorString(errorCode) << endl;
For the error handling mechanism, the SDK returns Error Code for each function and provides a function
DC_GetErrorString
to get the readable message. You should add codes for error handling based on your needs. Check out Error Code for full supported error codes. -
Save the normalized result as an image file.
if (normalizedResult != NULL) { CDocumentNormalizer::SaveImageDataToFile(normalizedResult->image, "result-image.png"); }
Release Allocated Memory
-
Release the allocated memory for the normalized result and instance.
if (normalizedResult != NULL) CDocumentNormalizer::FreeNormalizedImageResult(&normalizedResult);
Note:
Please change all[INSTALLATION FOLDER]
in above code snippet to your unpacking path.
Build and Run the Project
- For Windows
-
In Visual Studio, set the solution to build as Release|x64.
-
Build the project to generate program
DDNCPPSample.exe
. -
Copy ALL
*.dll
files under[INSTALLATION FOLDER]\Lib\Windows\x64
to the same folder as theDDNCPPSample.exe
. -
Run the program
DDNCPPSample.exe
.
The SDK supports both x86 and x64, please set the platform based on your needs.
- For Linux
-
Open a terminal and change to the target directory where
DDNCPPSample.cpp
located in. Build the sample:g++ -o DDNCPPSample DDNCPPSample.cpp -lDynamsoftCore -lDynamsoftDocumentNormalizer -L ../Lib/Linux -Wl,-rpath=../Lib/Linux -std=c++11
-
Run the program
DDNCPPSample
../DDNCPPSample