User Guide for Document Scanner with C++
In this guide, you will learn step by step on how to build a document scanner solution with Dynamsoft Capture Vision SDK using c++.
Table of Contents
System Requirements
To find out whether your environment is supported, please read the System Requirements.
Installation
If you haven’t downloaded the SDK yet, download the C/C++ Package
now and unpack the package into a directory of your choice.
For this tutorial, we unpack it to a pseudo directory
[INSTALLATION FOLDER]
, change it to your unpacking path for the following content.
Build Your Own Application
In this section, we’ll walk through the key steps needed to build an application that capture a document from an image file.
You can download the complete source code from here.
Create A New Project
For Windows
-
Open Visual Studio. Go to File > New > Project, select Empty App and enter
DocumentScanner
in thename
text box. -
Add a new source file named
DocumentScanner.cpp
into the project.
For Linux
-
Create a new source file named
DocumentScanner.cpp
and place it into the folder[INSTALLATION FOLDER]\Samples\DocumentScanner
. -
Create a file named
Makefile
and put it in the same directory as the fileDocumentScanner.cpp
. The content ofMakefile
is as follows:CC=gcc CCFLAGS=-c -std=c++11 DS_LIB_PATH=[INSTALLATION FOLDER]/Distributables/Lib/Linux/x64 LDFLAGS=-L $(DS_LIB_PATH) -Wl,-rpath=$(DS_LIB_PATH) -Wl,-rpath=./ DS_LIB=-lDynamsoftCaptureVisionRouter -lDynamsoftCore -lDynamsoftLicense -lDynamsoftUtility DS_JSON_PATH=[INSTALLATION FOLDER]/Distributables/Templates STDLIB=-lstdc++ TARGET=DocumentScanner OBJECT=DocumentScanner.o SOURCE=DocumentScanner.cpp # build rule for target. $(TARGET): $(OBJECT) $(CC) -o $(TARGET) $(OBJECT) $(STDLIB) $(DS_LIB) $(LDFLAGS) cp -r $(DS_JSON_PATH) $(DS_LIB_PATH) # target to build an object file $(OBJECT): $(SOURCE) $(CC) $(CCFLAGS) $(SOURCE) # the clean target .PHONY : clean clean: rm -f $(OBJECT) $(TARGET) -r $(DS_LIB_PATH)/Templates
Note: The variable
DS_LIB_PATH
should be set to the correct directory where the library files are located.
Include the Library
-
Add headers and libs in
DocumentScanner.cpp
.#include <iostream> #include <string> #include "[INSTALLATION FOLDER]/Include/DynamsoftCaptureVisionRouter.h" #include "[INSTALLATION FOLDER]/Include/DynamsoftUtility.h" using namespace std; using namespace dynamsoft::cvr; using namespace dynamsoft::ddn; using namespace dynamsoft::license; using namespace dynamsoft::utility; // The following code only applies to Windows. #if defined(_WIN64) || defined(_WIN32) #ifdef _WIN64 #pragma comment(lib, "[INSTALLATION FOLDER]/Distributables/Lib/Windows/x64/DynamsoftCaptureVisionRouterx64.lib") #pragma comment(lib, "[INSTALLATION FOLDER]/Distributables/Lib/Windows/x64/DynamsoftCorex64.lib") #pragma comment(lib, "[INSTALLATION FOLDER]/Distributables/Lib/Windows/x64/DynamsoftLicensex64.lib") #pragma comment(lib, "[INSTALLATION FOLDER]/Distributables/Lib/Windows/x64/DynamsoftUtilityx64.lib") #else #pragma comment(lib, "[INSTALLATION FOLDER]/Distributables/Lib/Windows/x86/DynamsoftCaptureVisionRouterx86.lib") #pragma comment(lib, "[INSTALLATION FOLDER]/Distributables/Lib/Windows/x86/DynamsoftCorex86.lib") #pragma comment(lib, "[INSTALLATION FOLDER]/Distributables/Lib/Windows/x86/DynamsoftLicensex86.lib") #pragma comment(lib, "[INSTALLATION FOLDER]/Distributables/Lib/Windows/x86/DynamsoftUtilityx86.lib") #endif #endif
Initialize a Capture Vision Router Instance
-
Initialize the license key
int errorcode = 0; char error[512]; errorcode = CLicenseManager::InitLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", error, 512); if (errorcode != ErrorCode::EC_OK && errorcode != ErrorCode::EC_LICENSE_CACHE_USED) { cout << "License initialization failed: ErrorCode: " << errorcode << ", ErrorString: " << error << endl; } else { // codes from following steps... }
Note: 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 free trial license from the Customer Portal.
-
Create an instance of Dynamsoft Capture Vision Router
CCaptureVisionRouter* router = new CCaptureVisionRouter();
Detect and Save the Normalized Document
-
Apply detection and normalization for an image file.
string imageFile = "[PATH-TO-THE-IMAGE-FILE]"; CCapturedResult* result = router->Capture(imageFile.c_str(), CPresetTemplate::PT_DETECT_AND_NORMALIZE_DOCUMENT);
Please change the
[PATH-TO-THE-IMAGE-FILE]
to a real image file path. -
Save the normalized result as an image file
cout << "File: " << imageFile << endl; if (result->GetErrorCode() != 0) { cout << "Error: " << result->GetErrorCode() << "," << result->GetErrorString() << endl; } CNormalizedImagesResult *normalizedResult = result->GetNormalizedImagesResult(); if (normalizedResult == nullptr || normalizedResult->GetItemsCount() == 0) { cout << "No document found." << endl; } else { int count = normalizedResult->GetItemsCount(); cout << "Normalized " << count << " documents" << endl; for (int i = 0; i < count; i++) { const CNormalizedImageResultItem *normalizedImage = normalizedResult->GetItem(i); string outPath = "normalizedResult_"; outPath += to_string(i) + ".png"; CImageManager manager; errorcode = manager.SaveToFile(normalizedImage->GetImageData(), outPath.c_str()); if (errorcode == 0) { cout << "Document " << i << " saved to file: " << outPath << endl; } } }
Release the Allocated Memory
if (normalizedResult)
normalizedResult->Release();
if (result)
result->Release();
delete router, router = NULL;
Build and Run the Project
On Windows
-
Build the application through Visual Studio.
-
Copy the following items from
[INSTALLATION FOLDER]\Distributables
to the same folder as the EXE file.- All the DLL files from
.\Lib\Windows\[PLATFORMS]
- Folder
Templates
- All the DLL files from
-
Run the program
DocumentScanner.exe
.
On Linux
-
Open a terminal and change to the target directory where
Makefile
located in. Build the sample:>make
-
Run the program
DocumentScanner
.>./DocumentScanner
Note:
Please change all
[INSTALLATION FOLDER]
in above code snippet to your unpacking path.