Thanks for downloading Dynamsoft Barcode Reader Package!
Your download will start shortly. If your download does not begin, click here to retry.
Getting Started with C++ Language
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.
Requirements
- Operating System:
- Windows 7, 8, 10, 11, 2003, 2008, 2008 R2, 2012, 2016, 2019, 2022
- Linux x64: Ubuntu 14.04.4+ LTS, Debian 8+, etc
- Linux arm 32bit
- Linux arm 64bit
- MacOS 64bit: 10.12+ (not included in the trial package, contact us to get the SDK)
- Developing Tool
- Visual Studio 2008 or above
- G++ 5.4+
Note: Dynamsoft Barcode Reader provides both online and offline license options. The online license option might not work in an environment that doesn’t have network connection. In such case, you can get an offline trial license key via Customer Portal or by contacting us.
Installation
If you haven’t downloaded the SDK yet, download the C/C++ Package
now from Dynamsoft website and unpack the package into the directory of your choice.
For this tutorial, we unpack it to
[INSTALLATION FOLDER]
, change it to your unpacking path for the following content.
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 image file.
You can download the entire source code here.
Create a New Project
For Windows
-
Open Visual Studio. Go to File > New > Project, create a new Empty Project and set Project name as
DBRCPPSample
. -
Add a new source file named
DBRCPPSample.cpp
into the project.
For Linux/ARM/Mac
- Create a new source file named
DBRCPPSample.cpp
and place it into the folder[INSTALLATION FOLDER]/Samples
.
Include the Library
-
Add headers and libs in
DBRCPPSample.cpp
.#include<iostream> #include "[INSTALLATION FOLDER]/Include/DynamsoftBarcodeReader.h" using namespace std; using namespace dynamsoft::dbr; #if defined(_WIN64) || defined(_WIN32) #ifdef _WIN64 #pragma comment(lib, "[INSTALLATION FOLDER]/Lib/Windows/x64/DBRx64.lib") #else #pragma comment(lib, "[INSTALLATION FOLDER]/Lib/Windows/x86/DBRx86.lib") #endif #endif
Initialize a Barcode Reader Instance
-
Initialize the license key.
int errorCode = 0; char errorBuf[512]; errorCode = CBarcodeReader::InitLicense("<insert DBR license key here>", errorBuf, 512); if (errorCode != DBR_OK) { // Add your code for license error processing; cout << errorBuf << endl; }
Please replace
<insert DBR license key here>
with a valid DBR licensekey. There are two ways to obtain one:- Search
InitLicense
and find the license from[INSTALLATION FOLDER]/Samples/BarcodeReaderDemo/BarcodeReaderDemo.cpp
. - Request a trial license from Customer Portal.
- Search
-
Create an instance of Dynamsoft Barcode Reader.
CBarcodeReader* dbr = new CBarcodeReader();
However, please note that if you are using a concurrent instance license, please use the new APIs
GetInstance
to initialize the barcode reader instance and thenRecycle
to allow for better concurrent instance management by the library.CBarcodeReader* dbr = CBarcodeReader::GetInstance(); // If no instance is available right away, the application will wait until one becomes available if(dbr != NULL) { // Add your code here to call decoding method, process barcode results and so on // ... // Recycle the instance to make it idle for other concurrent tasks dbr->Recycle(); }
Configure the Barcode Scanning Behavior
-
Set barcode format and count to read.
char szErrorMsg[512]; PublicRuntimeSettings settings; dbr->GetRuntimeSettings(&settings); settings.barcodeFormatIds = BF_PDF417; settings.barcodeFormatIds_2 = BF2_DOTCODE; settings.expectedBarcodesCount = 32; dbr->UpdateRuntimeSettings(&settings, szErrorMsg, 512);
The barcode formats to enable is highly application-specific. We recommend that you only enable the barcode formats your application requires. Check out Barcode Format Enumeration for full supported barcode formats.
If you know exactly the barcode count you want to read, specify
expectedBarcodesCount
to speed up the process and improve the accuracy.The Barcode Reader SDK comes with a large array of runtime settings to optimize the performance of the library. To learn about all the runtime settings, please visit the RuntimeSettings API page. To learn more about the cases and situations in which the settings can help, please visit the Explore Features page.
Decode and Output Results
-
Decode barcodes from an image file.
int errorCode = -1; errorCode = dbr->DecodeFile("[INSTALLATION FOLDER]/Images/AllSupportedBarcodeTypes.png", ""); if(errorCode != DBR_OK) cout << CBarcodeReader::GetErrorString(errorCode) << endl;
For the error handling mechanism, the SDK returns Error Code for each function and provides a function
GetErrorString
to get the readable message. You should add codes for error handling based on your needs. Check out Error Code for full supported error codes. -
Get and output barcode results.
TextResultArray* pResult = NULL; dbr->GetAllTextResults(&pResult); if (pResult != NULL && pResult->resultsCount > 0) { cout << pResult->resultsCount <<" total barcode(s) found."<< endl; for (int iIndex = 0; iIndex < pResult->resultsCount; iIndex++) { cout << "Result " << iIndex + 1 << endl; cout << "Barcode Format: " << pResult->results[iIndex]->barcodeFormatString << endl; cout << "Barcode Text: " << pResult->results[iIndex]->barcodeText << endl; } } cin.ignore();
The SDK returns multiple barcode information, including barcode count, barcode format, barcode text, location, barcode raw data, etc. Check out TextResult for full supported result data.
Release Allocated Memory
-
Release the allocated memory for the barcode results.
if(pResult != NULL) CBarcodeReader::FreeTextResults(&pResult);
-
Release the allocated memory for the instance.
if(dbr != NULL) delete dbr;
However, please note that if you are using a concurrent instance license, please use the new APIs
Recycle
to allow for better concurrent instance management by the library.if(dbr != NULL) dbr->Recycle();
Note:
Please change all[INSTALLATION FOLDER]
in above code snippet to your unpacking path.
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]\Lib\Windows\x64
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/ARM/Mac
-
Open a terminal and change to the target directory where
DBRCPPSample.cpp
located in. Build the sample:g++ -o DBRCPPSample DBRCPPSample.cpp -lDynamsoftBarcodeReader -L ../Lib/Linux -Wl,-rpath=../Lib/Linux -std=c++11
Please replace
Linux
toARM32
orARM64
based on your platform. -
Run the program
DBRCPPSample
../DBRCPPSample
You can download the entire source code here.
Next Steps
- Learn How to Upgrade to Latest Version
- Explore SDK Features
- See how the SDK works in Popular Use Cases
- Check out the Official Samples and Demo