Getting Started with Dynamsoft Barcode Reader SDK C++ Edition
In this guide, you will learn step by step how to build a barcode reading application with Dynamsoft Barcode Reader SDK using C++.
Learn more about Dynamsoft Barcode Reader Features
- Getting Started with Dynamsoft Barcode Reader SDK C++ Edition
Installation
If you haven’t downloaded the SDK yet, download the C/C++ Package now and unpack it into a directory of your choice.
For this tutorial, we will unpack it to a pseudo directory named
[INSTALLATION FOLDER]. Please change it to your preferred unpacking path for the following content.
To find out whether your environment is supported, please read the System Requirements.
Build Your First Application
Let’s start by creating a console application that demonstrates how to use the minimum code to read barcodes from an image file.
Create a New Project
For Windows
- Open Visual Studio. Go to “File > New > Project…” or click “Create a new project” on the starting page, choose “Console App”, create a new Empty Project, and set the Project name as
DBRCPPSample. - Add a new source file named
DBRCPPSample.cppinto the project.
For Linux
Create a new source file named DBRCPPSample.cpp and place it into the folder [INSTALLATION FOLDER]/DynamsoftBarcodeReader/Samples.
Include the Library
Add headers and libraries in DBRCPPSample.cpp.
#include <iostream>
#include <string>
#include "[INSTALLATION FOLDER]/DynamsoftBarcodeReader/Include/DynamsoftCaptureVisionRouter.h"
using namespace std;
using namespace dynamsoft::license;
using namespace dynamsoft::cvr;
using namespace dynamsoft::dbr;
#if defined(_WIN64) || defined(_WIN32)
#ifdef _WIN64
#pragma comment(lib, "[INSTALLATION FOLDER]/DynamsoftBarcodeReader/Dist/Lib/Windows/x64/DynamsoftCorex64.lib")
#pragma comment(lib, "[INSTALLATION FOLDER]/DynamsoftBarcodeReader/Dist/Lib/Windows/x64/DynamsoftLicensex64.lib")
#pragma comment(lib, "[INSTALLATION FOLDER]/DynamsoftBarcodeReader/Dist/Lib/Windows/x64/DynamsoftCaptureVisionRouterx64.lib")
#else
#pragma comment(lib, "[INSTALLATION FOLDER]/DynamsoftBarcodeReader/Dist/Lib/Windows/x86/DynamsoftCorex86.lib")
#pragma comment(lib, "[INSTALLATION FOLDER]/DynamsoftBarcodeReader/Dist/Lib/Windows/x86/DynamsoftLicensex86.lib")
#pragma comment(lib, "[INSTALLATION FOLDER]/DynamsoftBarcodeReader/Dist/Lib/Windows/x86/DynamsoftCaptureVisionRouterx86.lib")
#endif
#endif
Initialize a Capture Vision Router Instance
Initialize the License Key
char errorMsg[512];
CLicenseManager::InitLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", errorMsg, 512);
The string “DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9” here is a free public trial license. Note that network connection is required for this license to work. Alternatively, you can request a 30-day offline free trial license from the Customer Portal.
Create a Capture Vision Router Instance
CCaptureVisionRouter* cvRouter = new CCaptureVisionRouter;
Decode and Output Results
Decode barcodes from an image file.
string imageFile = "[PATH-TO-A-BARCODE-IMAGE]";
CCapturedResult* result = cvRouter->Capture(imageFile.c_str(), CPresetTemplate::PT_READ_BARCODES);
if (result->GetErrorCode() != 0) {
cout << "Error: " << result->GetErrorCode() << ", " << result->GetErrorString() << endl;
}
CDecodedBarcodesResult *barcodeResult = result->GetDecodedBarcodesResult();
if (barcodeResult == nullptr || barcodeResult->GetItemsCount() == 0)
{
cout << "No barcode found." << endl;
}
else
{
int barcodeResultItemCount = barcodeResult->GetItemsCount();
cout << "Decoded " << barcodeResultItemCount << " barcodes" << endl;
for (int j = 0; j < barcodeResultItemCount; j++)
{
const CBarcodeResultItem *barcodeResultItem = barcodeResult->GetItem(j);
cout << "Result " << j + 1 << endl;
cout << "Barcode Format: " << barcodeResultItem->GetFormatString() << endl;
cout << "Barcode Text: " << barcodeResultItem->GetText() << endl;
}
}
Release the Allocated Memory
if (barcodeResult)
barcodeResult->Release();
result->Release();
delete cvRouter, cvRouter = NULL;
Build and Run the Project
For Windows
- In Visual Studio, set the solution to build as
Release|x64. - Build the project to generate the program
DBRCPPSample.exe. - Copy ALL
*.dllfiles under[INSTALLATION FOLDER]/DynamsoftBarcodeReader/Dist/Lib/Windows/x64to the same folder as theDBRCPPSample.exe. - Copy the folder
[INSTALLATION FOLDER]/DynamsoftBarcodeReader/Dist/Templatesto the same folder as theDBRCPPSample.exe. - Run the program
DBRCPPSample.exe.
For Linux
- Open a terminal and navigate to the target directory where
DBRCPPSample.cppis located. -
Build the sample:
g++ -o DBRCPPSample DBRCPPSample.cpp -lDynamsoftCaptureVisionRouter -lDynamsoftLicense -lDynamsoftCore -lDynamsoftUtility -L ../Dist/Lib/Linux/x64 -Wl,-rpath=../Dist/Lib/Linux/x64 -std=c++11 -
Copy the preset template:
cp -r ../Dist/Templates ../Dist/Lib/Linux/x64/ -
Run the program:
./DBRCPPSample
Process Multiple Images
If you need to process multiple images, follow these steps:
These steps follow the step Initialize a Capture Vision Router Instance mentioned above.
Add an Image Source as the Input
The class CDirectoryFetcher is capable of converting a local directory to an image source. We will use it to connect multiple images to the image-processing engine.
Include additional DynamsoftUtility module.
// Add the following lines
using namespace dynamsoft::utility;
#ifdef _WIN64
#pragma comment(lib, "[INSTALLATION FOLDER]/DynamsoftBarcodeReader/Dist/Lib/Windows/x64/DynamsoftUtilityx64.lib")
#else
#pragma comment(lib, "[INSTALLATION FOLDER]/DynamsoftBarcodeReader/Dist/Lib/Windows/x86/DynamsoftUtilityx86.lib")
#endif
Set up a CDirectoryFetcher object to retrieve image data sources from a directory.
CDirectoryFetcher *fetcher = new CDirectoryFetcher;
fetcher->SetDirectory("[THE DIRECTORY THAT HOLDS THE IMAGES]");
cvRouter->SetInput(fetcher);
Add a Result Receiver as the Output
Create a class MyCapturedResultReceiver to implement the CCapturedResultReceiver interface, and get the barocde results in OnDecodedBarcodesReceived callback function.
class MyCapturedResultReceiver : public CCapturedResultReceiver {
void OnDecodedBarcodesReceived(CDecodedBarcodesResult* pResult) {
const CFileImageTag *tag = dynamic_cast<const CFileImageTag*>(pResult->GetOriginalImageTag());
cout << "File: " << tag->GetFilePath() << endl;
if (pResult->GetErrorCode() != EC_OK)
{
cout << "Error: " << pResult->GetErrorString() << endl;
}
int count = pResult->GetItemsCount();
cout << "Decoded " << count << " barcodes" << endl;
for (int i = 0; i < count; i++) {
const CBarcodeResultItem* barcodeResultItem = pResult->GetItem(i);
if (barcodeResultItem != NULL)
{
cout << "Result " << i + 1 << endl;
cout << "Barcode Format: " << barcodeResultItem->GetFormatString() << endl;
cout << "Barcode Text: " << barcodeResultItem->GetText() << endl;
}
}
cout << endl;
}
};
Create and register a MyCapturedResultReceiver object as the result receiver.
CCapturedResultReceiver *capturedReceiver = new MyCapturedResultReceiver;
cvRouter->AddResultReceiver(capturedReceiver);
Add an Object to Listen to the Status of the Image Source
Create a class MyImageSourceStateListener to implement the CImageSourceStateListenter interface, and call StopCapturing in OnImageSourceStateReceived callback function when the state is ISS_EXHAUSTED.
class MyImageSourceStateListener : public CImageSourceStateListener {
private:
CCaptureVisionRouter* m_router;
public:
MyImageSourceStateListener(CCaptureVisionRouter* router) {
m_router = router;
}
virtual void OnImageSourceStateReceived(ImageSourceState state)
{
if (state == ISS_EXHAUSTED)
m_router->StopCapturing();
}
};
Create and register a MyImageSourceStateListener object as the listener.
CImageSourceStateListener *listener = new MyImageSourceStateListener(cvRouter);
cvRouter->AddImageSourceStateListener(listener);
Start the Process
Call the method StartCapturing() to start processing all the images in the specified folder.
int errorCode = cvRouter->StartCapturing(CPresetTemplate::PT_READ_BARCODES, true, errorMsg, 512);
During the process, the callback function OnDecodedBarcodesReceived() is triggered each time an image finishes processing. After all images are processed, the listener function OnImageSourceStateReceived() will return the image source state as ISS_EXHAUSTED and the process is stopped with the method StopCapturing().
Release the Allocated Memory
delete cvRouter, cvRouter = NULL;
delete fetcher, fetcher = NULL;
delete listener, listener = NULL;
delete capturedReceiver, capturedReceiver = NULL;
Build and Run the Project Again
Refer to the steps in Build and Run the Project.