Dev Center
Table of contents

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

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.

You can download the entire source code from here.

Create a New Project

  • For Windows
  1. 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.

  2. 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/AllSupportedBarcodeTypes.png";
CCapturedResult* result = cvr->Capture(imageFile.c_str(), CPresetTemplate::PT_READ_BARCODES);
if (result->GetErrorCode() != 0) {
    cout << "Error: " << result->GetErrorCode() << "," << result->GetErrorString() << endl;
}
int capturedResultItemCount = result->GetItemsCount();
cout << "Decoded " << capturedResultItemCount << " barcodes" << endl;

for (int j = 0; j < capturedResultItemCount; j++) 
{
    const CCapturedResultItem* capturedResultItem = result->GetItem(j);
    /*
    * There can be multiple types of result items per image.
    * We check each of these items until we find the barcode result.
    */
    CapturedResultItemType type = capturedResultItem->GetType();
    if (type == CapturedResultItemType::CRIT_BARCODE)
    {
        const CBarcodeResultItem* barcodeResultItem = dynamic_cast<const CBarcodeResultItem*> (capturedResultItem);
        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
  1. In Visual Studio, set the solution to build as Release|x64.

  2. Build the project to generate program DBRCPPSample.exe.

  3. Copy ALL *.dll files under [INSTALLATION FOLDER]/Dynamsoft/Distributables/Lib/Windows/x64 to the same folder as the DBRCPPSample.exe (“[PROJECT FOLDER]\DBRCPPSample\x64\Release”).

  4. Copy [INSTALLATION FOLDER]/Dynamsoft/Distributables/DBR-PresetTemplates.json to the same folder as the DBRCPPSample.exe.

  5. 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.

You can download the entire source code from here.

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.

This page is compatible for:

Version 7.5.0

Is this page helpful?

YesYes NoNo

In this article:

latest version

  • Latest version
  • Version 10.x
    • Version 10.2.0
    • Version 10.0.20
    • Version 10.0.10
    • Version 10.0.0
  • Version 9.x
    • Version 9.6.40
    • Version 9.6.30
    • Version 9.6.20
    • Version 9.6.10
    • Version 9.6.0
    • Version 9.4.0
    • Version 9.2.0
    • Version 9.0.0
  • Version 8.x
    • Version 8.8.0
    • Version 8.6.0
    • Version 8.4.0
    • Version 8.2.0
    • Version 8.1.2
    • Version 8.1.0
    • Version 8.0.0
  • Version 7.x
    • Version 7.6.0
    • Version 7.5.0
Change +