Dynamsoft Barcode Reader Documentation for C++ Edition
Dynamsoft Barcode Reader (DBR) SDK C++ Edition is a barcode reading tool designed specifically for C++ developers.
Leveraging the efficiency and flexibility of the C++ language, DBR C++ Edition provides rich APIs for developers to easily integrate barcode reading functionality into your c++ applications. Please take a look at API Reference to see details.
In addition, DBR C++ Edition provides cross-platform support, running on multiple operating systems such as Windows, Linux, and Mac, to meet the needs of different development environments
As one of the functional products of the Dynamsoft Capture Vision (DCV) framework, DBR is also designed to seamlessly integrate with other DCV components and provide developers with a comprehensive set of image processing tools, which cover image capturing, content understanding, result parsing, and interactive workflow. If you are building such a capturing and processing solution, take a look at Introduction to Dynamsoft Capture Vision.
Furthermore, DBR provides a powerful parameter system in order to cope with various scenarios. Read Parameter Reference for details.
Getting Started with DBR C++ Edition
The best way to start with the Dynamsoft Barcode Reader C++ Edition, is following the User Guide to build your first barcode reading application. Please note that the system requirements is as below:
System Requirements
- Windows:
- Supported Versions: Windows 7 and higher, or Windows Server 2003 and higher
- Architecture: x64 and x86
- Development Environment: Visual Studio 2012 or higher.
- Linux:
- Supported Distributions: Ubuntu 14.04.4+ LTS, Debian 8+, CentOS 6+
- Architectures: x64 (arm 32-bit and arm 64-bit coming soon)
- Minimum GLIBC Version: GLIBC_2.18 or higher
- Compiler: G++ 5.4 or higher
Dynamsoft Barcode API HighlightsHigh-speed barcode recognition algorithms for 1D and 2D barcodes with cross-platform support for Windows, macOS, and Linux. Allows developers to almost instantly embed barcode reading functionality into their web and desktop applications with a large offering of APIs. |
Getting Started with Dynamsoft Barcode Reader SDK C++ Edition
In this guide, you will learn step by step on how to build a barcode reading application with Dynamsoft Barcode Reader SDK using C++ language.
Read more on 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 which demonstrates how to use the minimum code to read barcodes from an picture of it.
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.cpp
into the project.
-
For Linux
Create a new source file named
DBRCPPSample.cpp
and place it into the folder[INSTALLATION FOLDER]/Dynamsoft/Samples
.
Include the Library
Add headers and libs in DBRCPPSample.cpp
.
#include <iostream>
#include <string>
#include "[INSTALLATION FOLDER]/Dynamsoft/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]/Dynamsoft/Distributables/Lib/Windows/x64/DynamsoftLicensex64.lib")
#pragma comment(lib, "[INSTALLATION FOLDER]/Dynamsoft/Distributables/Lib/Windows/x64/DynamsoftCaptureVisionRouterx64.lib")
#else
#pragma comment(lib, "[INSTALLATION FOLDER]/Dynamsoft/Distributables/Lib/Windows/x86/DynamsoftLicensex86.lib")
#pragma comment(lib, "[INSTALLATION FOLDER]/Dynamsoft/Distributables/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. When it expires, you can request a 30-day free trial license from the Customer Portal.
Create an instance of Capture Vision Router.
CCaptureVisionRouter* cvr = new CCaptureVisionRouter;
Decode and Output Results
Decode barcodes from an image file.
string imageFile = "[INSTALLATION FOLDER]/Dynamsoft/Resources/BarcodeReader/Images/GeneralBarcodes.png";
CCapturedResult* result = cvr->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;
}
}
Note:
Please change all
[INSTALLATION FOLDER]
in above code snippet to your unpacking path.
Release the Allocated Memory
result->Release();
delete cvr, cvr = NULL;
Build and Run the Project
- For Windows
-
In Visual Studio, set the solution to build as Release|x64.
-
Build the project to generate program
DBRCPPSample.exe
. -
Copy ALL
*.dll
files under[INSTALLATION FOLDER]/Dynamsoft/Distributables/Lib/Windows/x64
to the same folder as theDBRCPPSample.exe
(“[PROJECT FOLDER]\DBRCPPSample\x64\Release”). -
Copy
[INSTALLATION FOLDER]/Dynamsoft/Distributables/DBR-PresetTemplates.json
to the same folder as theDBRCPPSample.exe
. -
Run the program
DBRCPPSample.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
DBRCPPSample.cpp
is located. Build the sample:g++ -o DBRCPPSample DBRCPPSample.cpp -lDynamsoftCaptureVisionRouter -lDynamsoftLicense -L ../Distributables/Lib/Linux/x64 -Wl,-rpath=../Distributables/Lib/Linux/x64 -std=c++11 cp ../Distributables/DBR-PresetTemplates.json ../Distributables/Lib/Linux/x64
Run the program
DBRCPPSample
../DBRCPPSample
Process Multiple Images
If, instead of processing one single image, you need to process many images at once, you can 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
and DynamsoftCore
module.
// Add the following lines
using namespace dynamsoft::utility;
#ifdef _WIN64
#pragma comment(lib, "[INSTALLATION FOLDER]/Dynamsoft/Distributables/Lib/Windows/x64/DynamsoftCorex64.lib")
#pragma comment(lib, "[INSTALLATION FOLDER]/Dynamsoft/Distributables/Lib/Windows/x64/DynamsoftUtilityx64.lib")
#else
#pragma comment(lib, "[INSTALLATION FOLDER]/Dynamsoft/Distributables/Lib/Windows/x86/DynamsoftCorex86.lib")
#pragma comment(lib, "[INSTALLATION FOLDER]/Dynamsoft/Distributables/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]");
cvr->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;
}
else
{
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;
cvr->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(cvr);
cvr->AddImageSourceStateListener(listener);
Start the Process
Call the method StartCapturing()
to start processing all the images in the specified folder.
int errorCode = cvr->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 cvr, cvr = NULL;
delete fetcher, fetcher = NULL;
delete listener, listener = NULL;
delete capturedReceiver, capturedReceiver = NULL;
Build and Run the Project Again
Please refer to Build and Run the Project.