|
| 1 | +--- |
| 2 | +layout: default-layout |
| 3 | +title: User Guide - Dynamsoft Barcode Reader SDK C++ Edition |
| 4 | +description: This is the user guide of Dynamsoft Barcode Reader SDK C++ Edition. |
| 5 | +keywords: user guide, c++ |
| 6 | +needAutoGenerateSidebar: true |
| 7 | +needGenerateH3Content: true |
| 8 | +noTitleIndex: true |
| 9 | +--- |
| 10 | + |
| 11 | +# Getting Started with Dynamsoft Barcode Reader SDK C++ Edition |
| 12 | + |
| 13 | +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. |
| 14 | + |
| 15 | +> Read more on [Dynamsoft Barcode Reader Features](https://www.dynamsoft.com/barcode-reader/features/) |
| 16 | +
|
| 17 | +- [Getting Started with Dynamsoft Barcode Reader SDK C++ Edition](#getting-started-with-dynamsoft-barcode-reader-sdk-c-edition) |
| 18 | + - [Installation](#installation) |
| 19 | + - [Build Your First Application](#build-your-first-application) |
| 20 | + - [Create a New Project](#create-a-new-project) |
| 21 | + - [Include the Library](#include-the-library) |
| 22 | + - [Initialize a Capture Vision Router Instance](#initialize-a-capture-vision-router-instance) |
| 23 | + - [Decode and Output Results](#decode-and-output-results) |
| 24 | + - [Release the Allocated Memory](#release-the-allocated-memory) |
| 25 | + - [Build and Run the Project](#build-and-run-the-project) |
| 26 | + - [Process Multiple Images](#process-multiple-images) |
| 27 | + - [Add an Image Source as the Input](#add-an-image-source-as-the-input) |
| 28 | + - [Add a Result Receiver as the Output](#add-a-result-receiver-as-the-output) |
| 29 | + - [Add an Object to Listen to the Status of the Image Source](#add-an-object-to-listen-to-the-status-of-the-image-source) |
| 30 | + - [Start the Process](#start-the-process) |
| 31 | + - [Release the Allocated Memory](#release-the-allocated-memory-1) |
| 32 | + - [Build and Run the Project Again](#build-and-run-the-project-again) |
| 33 | + |
| 34 | +## Installation |
| 35 | + |
| 36 | +If you haven't downloaded the SDK yet, <a href="https://www.dynamsoft.com/barcode-reader/downloads/?utm_source=docs" target="_blank">download the `C/C++ Package`</a> now and unpack it into a directory of your choice. |
| 37 | + |
| 38 | +> 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. |
| 39 | +
|
| 40 | +> To find out whether your environment is supported, please read the [System Requirements]({{site.dbr_cpp}}index.html#system-requirements). |
| 41 | +
|
| 42 | +## Build Your First Application |
| 43 | + |
| 44 | +Let's start by creating a console application which demonstrates how to use the minimum code to read barcodes from an picture of it. |
| 45 | + |
| 46 | +> You can <a href="https://github.com/Dynamsoft/barcode-reader-c-cpp-samples/tree/main/Samples/HelloWorld/ReadAnImage" target="_blank">download the entire source code from here</a>. |
| 47 | +
|
| 48 | +### Create a New Project |
| 49 | + |
| 50 | +- For Windows |
| 51 | + |
| 52 | +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`. |
| 53 | + |
| 54 | +2. Add a new source file named `DBRCPPSample.cpp` into the project. |
| 55 | + |
| 56 | +- For Linux |
| 57 | + |
| 58 | + Create a new source file named `DBRCPPSample.cpp` and place it into the folder `[INSTALLATION FOLDER]/Dynamsoft/Samples`. |
| 59 | + |
| 60 | +### Include the Library |
| 61 | + |
| 62 | +Add headers and libs in `DBRCPPSample.cpp`. |
| 63 | + |
| 64 | +```cpp |
| 65 | +#include <iostream> |
| 66 | +#include <string> |
| 67 | +#include "[INSTALLATION FOLDER]/Dynamsoft/Include/DynamsoftCaptureVisionRouter.h" |
| 68 | + |
| 69 | +using namespace std; |
| 70 | +using namespace dynamsoft::license; |
| 71 | +using namespace dynamsoft::cvr; |
| 72 | +using namespace dynamsoft::dbr; |
| 73 | + |
| 74 | +#if defined(_WIN64) || defined(_WIN32) |
| 75 | + #ifdef _WIN64 |
| 76 | + #pragma comment(lib, "[INSTALLATION FOLDER]/Dynamsoft/Distributables/Lib/Windows/x64/DynamsoftLicensex64.lib") |
| 77 | + #pragma comment(lib, "[INSTALLATION FOLDER]/Dynamsoft/Distributables/Lib/Windows/x64/DynamsoftCaptureVisionRouterx64.lib") |
| 78 | + #else |
| 79 | + #pragma comment(lib, "[INSTALLATION FOLDER]/Dynamsoft/Distributables/Lib/Windows/x86/DynamsoftLicensex86.lib") |
| 80 | + #pragma comment(lib, "[INSTALLATION FOLDER]/Dynamsoft/Distributables/Lib/Windows/x86/DynamsoftCaptureVisionRouterx86.lib") |
| 81 | + #endif |
| 82 | +#endif |
| 83 | +``` |
| 84 | +
|
| 85 | +### Initialize a Capture Vision Router Instance |
| 86 | +
|
| 87 | +Initialize the license key. |
| 88 | +
|
| 89 | +```cpp |
| 90 | +char errorMsg[512]; |
| 91 | +CLicenseManager::InitLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", errorMsg, 512); |
| 92 | +``` |
| 93 | + |
| 94 | +> 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 <a href="https://www.dynamsoft.com/customer/license/trialLicense?utm_source=guide&product=dbr&package=c_cpp" target="_blank">Customer Portal</a>. |
| 95 | + |
| 96 | +Create an instance of Capture Vision Router. |
| 97 | + |
| 98 | +```cpp |
| 99 | +CCaptureVisionRouter* cvr = new CCaptureVisionRouter; |
| 100 | +``` |
| 101 | + |
| 102 | +### Decode and Output Results |
| 103 | + |
| 104 | +Decode barcodes from an image file. |
| 105 | + |
| 106 | +```cpp |
| 107 | +string imageFile = "[INSTALLATION FOLDER]/Dynamsoft/Resources/BarcodeReader/Images/GeneralBarcodes.png"; |
| 108 | +CCapturedResult* result = cvr->Capture(imageFile.c_str(), CPresetTemplate::PT_READ_BARCODES); |
| 109 | +if (result->GetErrorCode() != 0) { |
| 110 | + cout << "Error: " << result->GetErrorCode() << "," << result->GetErrorString() << endl; |
| 111 | +} |
| 112 | +CDecodedBarcodesResult *barcodeResult = result->GetDecodedBarcodesResult(); |
| 113 | +if (barcodeResult == nullptr || barcodeResult->GetItemsCount() == 0) |
| 114 | +{ |
| 115 | + cout << "No barcode found." << endl; |
| 116 | +} |
| 117 | +else |
| 118 | +{ |
| 119 | + int barcodeResultItemCount = barcodeResult->GetItemsCount(); |
| 120 | + cout << "Decoded " << barcodeResultItemCount << " barcodes" << endl; |
| 121 | + for (int j = 0; j < barcodeResultItemCount; j++) |
| 122 | + { |
| 123 | + const CBarcodeResultItem *barcodeResultItem = barcodeResult->GetItem(j); |
| 124 | + cout << "Result " << j + 1 << endl; |
| 125 | + cout << "Barcode Format: " << barcodeResultItem->GetFormatString() << endl; |
| 126 | + cout << "Barcode Text: " << barcodeResultItem->GetText() << endl; |
| 127 | + } |
| 128 | +} |
| 129 | +``` |
| 130 | +
|
| 131 | +> Note: |
| 132 | +> |
| 133 | +> Please change all `[INSTALLATION FOLDER]` in above code snippet to your unpacking path. |
| 134 | +
|
| 135 | +### Release the Allocated Memory |
| 136 | +
|
| 137 | +```cpp |
| 138 | +result->Release(); |
| 139 | +delete cvr, cvr = NULL; |
| 140 | +``` |
| 141 | + |
| 142 | +### Build and Run the Project |
| 143 | + |
| 144 | +- For Windows |
| 145 | + |
| 146 | +1. In Visual Studio, set the solution to build as Release\|x64. |
| 147 | + |
| 148 | +2. Build the project to generate program `DBRCPPSample.exe`. |
| 149 | + |
| 150 | +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"). |
| 151 | + |
| 152 | +4. Copy `[INSTALLATION FOLDER]/Dynamsoft/Distributables/DBR-PresetTemplates.json` to the same folder as the `DBRCPPSample.exe`. |
| 153 | + |
| 154 | +5. Run the program `DBRCPPSample.exe`. |
| 155 | + |
| 156 | +> The SDK supports both x86 and x64, please set the platform based on your needs. |
| 157 | +
|
| 158 | +- For Linux |
| 159 | + |
| 160 | + Open a terminal and change to the target directory where `DBRCPPSample.cpp` is located. Build the sample: |
| 161 | + |
| 162 | + ```bash |
| 163 | + g++ -o DBRCPPSample DBRCPPSample.cpp -lDynamsoftCaptureVisionRouter -lDynamsoftLicense -L ../Distributables/Lib/Linux/x64 -Wl,-rpath=../Distributables/Lib/Linux/x64 -std=c++11 |
| 164 | + cp ../Distributables/DBR-PresetTemplates.json ../Distributables/Lib/Linux/x64 |
| 165 | + ``` |
| 166 | + |
| 167 | + Run the program `DBRCPPSample`. |
| 168 | + |
| 169 | + ```bash |
| 170 | + ./DBRCPPSample |
| 171 | + ``` |
| 172 | + |
| 173 | +## Process Multiple Images |
| 174 | + |
| 175 | +If, instead of processing one single image, you need to process many images at once, you can follow these steps: |
| 176 | + |
| 177 | +> These steps follow the step [Initialize a Capture Vision Router Instance](#initialize-a-capture-vision-router-instance) mentioned above. |
| 178 | + |
| 179 | +> You can <a href="https://github.com/Dynamsoft/barcode-reader-c-cpp-samples/tree/main/Samples/HelloWorld/ReadMultipleImages" target="_blank">download the entire source code from here</a>. |
| 180 | + |
| 181 | +### Add an Image Source as the Input |
| 182 | + |
| 183 | +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. |
| 184 | + |
| 185 | +Include additional `DynamsoftUtility` and `DynamsoftCore` module. |
| 186 | + |
| 187 | +```cpp |
| 188 | +// Add the following lines |
| 189 | +using namespace dynamsoft::utility; |
| 190 | +#ifdef _WIN64 |
| 191 | +#pragma comment(lib, "[INSTALLATION FOLDER]/Dynamsoft/Distributables/Lib/Windows/x64/DynamsoftCorex64.lib") |
| 192 | +#pragma comment(lib, "[INSTALLATION FOLDER]/Dynamsoft/Distributables/Lib/Windows/x64/DynamsoftUtilityx64.lib") |
| 193 | +#else |
| 194 | +#pragma comment(lib, "[INSTALLATION FOLDER]/Dynamsoft/Distributables/Lib/Windows/x86/DynamsoftCorex86.lib") |
| 195 | +#pragma comment(lib, "[INSTALLATION FOLDER]/Dynamsoft/Distributables/Lib/Windows/x86/DynamsoftUtilityx86.lib") |
| 196 | +#endif |
| 197 | +``` |
| 198 | + |
| 199 | +Set up a `CDirectoryFetcher` object to retrieve image data sources from a directory. |
| 200 | + |
| 201 | +```cpp |
| 202 | +CDirectoryFetcher *fetcher = new CDirectoryFetcher; |
| 203 | +fetcher->SetDirectory("[THE DIRECTORY THAT HOLDS THE IMAGES]"); |
| 204 | +cvr->SetInput(fetcher); |
| 205 | +``` |
| 206 | + |
| 207 | +### Add a Result Receiver as the Output |
| 208 | + |
| 209 | +Create a class `MyCapturedResultReceiver` to implement the `CCapturedResultReceiver` interface, and get the barocde results in `OnDecodedBarcodesReceived` callback function |
| 210 | + |
| 211 | +```cpp |
| 212 | +class MyCapturedResultReceiver : public CCapturedResultReceiver { |
| 213 | + void OnDecodedBarcodesReceived(CDecodedBarcodesResult* pResult) { |
| 214 | + const CFileImageTag *tag = dynamic_cast<const CFileImageTag*>(pResult->GetOriginalImageTag()); |
| 215 | + cout << "File: " << tag->GetFilePath() << endl; |
| 216 | + if (pResult->GetErrorCode() != EC_OK) |
| 217 | + { |
| 218 | + cout << "Error: " << pResult->GetErrorString() << endl; |
| 219 | + } |
| 220 | + else |
| 221 | + { |
| 222 | + int count = pResult->GetItemsCount(); |
| 223 | + cout << "Decoded " << count << " barcodes" << endl; |
| 224 | + for (int i = 0; i < count; i++) { |
| 225 | + const CBarcodeResultItem* barcodeResultItem = pResult->GetItem(i); |
| 226 | + if (barcodeResultItem != NULL) |
| 227 | + { |
| 228 | + cout << "Result " << i + 1 << endl; |
| 229 | + cout << "Barcode Format: " << barcodeResultItem->GetFormatString() << endl; |
| 230 | + cout << "Barcode Text: " << barcodeResultItem->GetText() << endl; |
| 231 | + } |
| 232 | + } |
| 233 | + } |
| 234 | + cout << endl; |
| 235 | + } |
| 236 | +}; |
| 237 | +``` |
| 238 | +
|
| 239 | +Create and register a `MyCapturedResultReceiver` object as the result receiver. |
| 240 | +
|
| 241 | +```cpp |
| 242 | +CCapturedResultReceiver *capturedReceiver = new MyCapturedResultReceiver; |
| 243 | +cvr->AddResultReceiver(capturedReceiver); |
| 244 | +``` |
| 245 | +
|
| 246 | +### Add an Object to Listen to the Status of the Image Source |
| 247 | +
|
| 248 | +Create a class `MyImageSourceStateListener` to implement the `CImageSourceStateListenter` interface, and call StopCapturing in `OnImageSourceStateReceived` callback function when the state is `ISS_EXHAUSTED`. |
| 249 | +
|
| 250 | +```cpp |
| 251 | +class MyImageSourceStateListener : public CImageSourceStateListener { |
| 252 | +private: |
| 253 | + CCaptureVisionRouter* m_router; |
| 254 | +public: |
| 255 | + MyImageSourceStateListener(CCaptureVisionRouter* router) { |
| 256 | + m_router = router; |
| 257 | + } |
| 258 | + virtual void OnImageSourceStateReceived(ImageSourceState state) |
| 259 | + { |
| 260 | + if (state == ISS_EXHAUSTED) |
| 261 | + m_router->StopCapturing(); |
| 262 | + } |
| 263 | +}; |
| 264 | +``` |
| 265 | +
|
| 266 | +Create and register a `MyImageSourceStateListener` object as the listener. |
| 267 | +
|
| 268 | +```cpp |
| 269 | +CImageSourceStateListener *listener = new MyImageSourceStateListener(cvr); |
| 270 | +cvr->AddImageSourceStateListener(listener); |
| 271 | +``` |
| 272 | +
|
| 273 | +### Start the Process |
| 274 | +
|
| 275 | +Call the method `StartCapturing()` to start processing all the images in the specified folder. |
| 276 | +
|
| 277 | +```cpp |
| 278 | +int errorCode = cvr->StartCapturing(CPresetTemplate::PT_READ_BARCODES, true, errorMsg, 512); |
| 279 | +``` |
| 280 | +
|
| 281 | +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()`. |
| 282 | +
|
| 283 | +### Release the Allocated Memory |
| 284 | +
|
| 285 | +```cpp |
| 286 | +delete cvr, cvr = NULL; |
| 287 | +delete fetcher, fetcher = NULL; |
| 288 | +delete listener, listener = NULL; |
| 289 | +delete capturedReceiver, capturedReceiver = NULL; |
| 290 | +``` |
| 291 | +
|
| 292 | +### Build and Run the Project Again |
| 293 | +
|
| 294 | +Please refer to [Build and Run the Project](#build-and-run-the-project). |
| 295 | +
|
0 commit comments