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 aboutDynamsoft Barcode Reader Features
- Getting Started with Dynamsoft Barcode Reader SDK C++ Edition
Installation
If you haven’t downloaded the SDK yet,download theC/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 theSystem 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 namedDBRCPPSample.cpp and place it into the folder[INSTALLATION FOLDER]/DynamsoftBarcodeReader/Samples.
Include the Library
Add headers and libraries inDBRCPPSample.cpp.
#include<iostream>#include<string>#include"[INSTALLATION FOLDER]/DynamsoftBarcodeReader/Include/DynamsoftCaptureVisionRouter.h"usingnamespacestd;usingnamespacedynamsoft::license;usingnamespacedynamsoft::cvr;usingnamespacedynamsoft::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#endifInitialize a Capture Vision Router Instance
Initialize the License Key
charerrorMsg[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-dayoffline free trial license from theCustomer Portal.
Create a Capture Vision Router Instance
CCaptureVisionRouter*cvRouter=newCCaptureVisionRouter;Decode and Output Results
Decode barcodes from an image file.
stringimageFile="[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{intbarcodeResultItemCount=barcodeResult->GetItemsCount();cout<<"Decoded "<<barcodeResultItemCount<<" barcodes"<<endl;for(intj=0;j<barcodeResultItemCount;j++){constCBarcodeResultItem*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();deletecvRouter,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. - CopyALL
*.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++11Copy 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 stepInitialize a Capture Vision Router Instance mentioned above.
Add an Image Source as the Input
The classCDirectoryFetcher 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 additionalDynamsoftUtility module.
// Add the following linesusingnamespacedynamsoft::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")#endifSet up aCDirectoryFetcher object to retrieve image data sources from a directory.
CDirectoryFetcher*fetcher=newCDirectoryFetcher;fetcher->SetDirectory("[THE DIRECTORY THAT HOLDS THE IMAGES]");cvRouter->SetInput(fetcher);Add a Result Receiver as the Output
Create a classMyCapturedResultReceiver to implement theCCapturedResultReceiver interface, and get the barocde results inOnDecodedBarcodesReceived callback function.
classMyCapturedResultReceiver:publicCCapturedResultReceiver{voidOnDecodedBarcodesReceived(CDecodedBarcodesResult*pResult){constCFileImageTag*tag=dynamic_cast<constCFileImageTag*>(pResult->GetOriginalImageTag());cout<<"File: "<<tag->GetFilePath()<<endl;if(pResult->GetErrorCode()!=EC_OK){cout<<"Error: "<<pResult->GetErrorString()<<endl;}intcount=pResult->GetItemsCount();cout<<"Decoded "<<count<<" barcodes"<<endl;for(inti=0;i<count;i++){constCBarcodeResultItem*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 aMyCapturedResultReceiver object as the result receiver.
CCapturedResultReceiver*capturedReceiver=newMyCapturedResultReceiver;cvRouter->AddResultReceiver(capturedReceiver);Add an Object to Listen to the Status of the Image Source
Create a classMyImageSourceStateListener to implement theCImageSourceStateListenter interface, and call StopCapturing inOnImageSourceStateReceived callback function when the state isISS_EXHAUSTED.
classMyImageSourceStateListener:publicCImageSourceStateListener{private:CCaptureVisionRouter*m_router;public:MyImageSourceStateListener(CCaptureVisionRouter*router){m_router=router;}virtualvoidOnImageSourceStateReceived(ImageSourceStatestate){if(state==ISS_EXHAUSTED)m_router->StopCapturing();}};Create and register aMyImageSourceStateListener object as the listener.
CImageSourceStateListener*listener=newMyImageSourceStateListener(cvRouter);cvRouter->AddImageSourceStateListener(listener);Start the Process
Call the methodStartCapturing() to start processing all the images in the specified folder.
interrorCode=cvRouter->StartCapturing(CPresetTemplate::PT_READ_BARCODES,true,errorMsg,512);During the process, the callback functionOnDecodedBarcodesReceived() is triggered each time an image finishes processing. After all images are processed, the listener functionOnImageSourceStateReceived() will return the image source state asISS_EXHAUSTED and the process is stopped with the methodStopCapturing().
Release the Allocated Memory
deletecvRouter,cvRouter=NULL;deletefetcher,fetcher=NULL;deletelistener,listener=NULL;deletecapturedReceiver,capturedReceiver=NULL;Build and Run the Project Again
Refer to the steps inBuild and Run the Project.


