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
- User Guide for Document Scanner with C++
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
DocumentScannerin thenametext box. -
Add a new source file named
DocumentScanner.cppinto the project.
For Linux
-
Create a new source file named
DocumentScanner.cppand place it into the folder[INSTALLATION FOLDER]\Samples\DocumentScanner. -
Create a file named
Makefileand put it in the same directory as the fileDocumentScanner.cpp. The content ofMakefileis as follows:CC=gcc CCFLAGS=-c -std=c++11 DS_LIB_PATH=[INSTALLATION FOLDER]/Dist/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]/Dist/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)/TemplatesThe variable
DS_LIB_PATHshould 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]/Dist/Lib/Windows/x64/DynamsoftCaptureVisionRouterx64.lib") #pragma comment(lib, "[INSTALLATION FOLDER]/Dist/Lib/Windows/x64/DynamsoftCorex64.lib") #pragma comment(lib, "[INSTALLATION FOLDER]/Dist/Lib/Windows/x64/DynamsoftLicensex64.lib") #pragma comment(lib, "[INSTALLATION FOLDER]/Dist/Lib/Windows/x64/DynamsoftUtilityx64.lib") #else #pragma comment(lib, "[INSTALLATION FOLDER]/Dist/Lib/Windows/x86/DynamsoftCaptureVisionRouterx86.lib") #pragma comment(lib, "[INSTALLATION FOLDER]/Dist/Lib/Windows/x86/DynamsoftCorex86.lib") #pragma comment(lib, "[INSTALLATION FOLDER]/Dist/Lib/Windows/x86/DynamsoftLicensex86.lib") #pragma comment(lib, "[INSTALLATION FOLDER]/Dist/Lib/Windows/x86/DynamsoftUtilityx86.lib") #endif #endif
Initialize the License Key
If this is your first time using the library, you will need to obtain a trial license key. We recommend getting your own 30-day trial license through the following modal:
Open the DocumentScanner.cpp file and add the following code inside the Main method to initialize the license for using the SDK in the application:
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...
}
The string “DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9” here is a free public trial license. Note that network connection is required for this license to work. Please replace it with your own 30-day trial license.
Create a CaptureVisionRouter Instance
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; } CProcessedDocumentResult *processedDocumentResult = result->GetProcessedDocumentResult(); if (processedDocumentResult == nullptr || processedDocumentResult->GetDeskewedImageResultItemsCount() == 0) { cout << "No document found." << endl; } else { int count = processedDocumentResult->GetDeskewedImageResultItemsCount(); cout << "Deskewed " << count << " documents" << endl; for (int i = 0; i < count; i++) { const CDeskewedImageResultItem *deskewedImage = processedDocumentResult->GetDeskewedImageResultItem(i); string outPath = "deskewedResult_"; outPath += to_string(i) + ".png"; CImageIO imageIO; // 5.Save deskewed image to file. errorCode = imageIO.SaveToFile(deskewedImage->GetImageData(), outPath.c_str()); if (errorCode == 0) { cout << "Document " << i << " file: " << outPath << endl; } } }
Release the Allocated Memory
if (processedDocumentResult)
processedDocumentResult->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]\Distto 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
Makefilelocated in. Build the sample:>make -
Run the program
DocumentScanner.>./DocumentScanner
Please change all
[INSTALLATION FOLDER]in above code snippet to your unpacking path.