Building Command-line Barcode and QR Code Reader in C++
Dynamsoft Barcode Reader SDK supports development of barcode and QR code scanning applications for desktop, mobile and web platforms. The version 9.0 of the SDK is coming to all platforms in the following weeks. The C++ SDK is available for download today. As an enterprise-class C++ barcode and QR code scanning SDK, Dynamsoft Barcode Reader supports building desktop and server applications for Windows, Linux, macOS, Raspberry Pi and Jetson Nano. This article helps developers to build barcode and QR code reading applications in C and C++.
This article is Part 8 in a 15-Part Series.
- Part 1 - Building a C/C++ Barcode & QR Code Reader for Raspberry Pi with Dynamsoft SDK
- Part 2 - CMake: Build C++ Project for Windows, Linux and macOS
- Part 3 - Insight Into Dynamsoft Barcode SDK Decoding Performance
- Part 4 - Building ARM64 Barcode and QR Code Scanner on Nvidia Jetson Nano
- Part 5 - How to Decode QR Code on Mac with Apple Silicon
- Part 6 - How to Develop a Desktop GUI Barcode Reader with Qt and C/C++
- Part 7 - How to Build a Desktop Barcode Scanner with Webcam Support Using Qt QCamera
- Part 8 - Building Command-line Barcode and QR Code Reader in C++
- Part 9 - How to Build Linux ARM32 and Aarch64 Barcode QR Scanner in Docker Container
- Part 10 - How to Link MSVC DLLs with MinGW GCC in Windows
- Part 11 - Transforming Raspberry Pi 4 into a Barcode Scanner with a C++ App, USB Camera, and OLED Display
- Part 12 - Building Windows Desktop Barcode Reader with Win32 API and Dynamsoft C++ Barcode SDK
- Part 13 - How to Build a Command-Line Barcode Reader with Rust and Dynamsoft C++ Barcode SDK
- Part 14 - How to Decode Barcode and QR Code from WebP Images in C++ and Python
- Part 15 - Building a Desktop C++ Barcode Scanner with Slimmed-Down OpenCV and Webcam
About Dynamsoft C++ Barcode and QR Code Reader SDK
SDK Download
SDK License
Click here to get a trial license. You can set the license key as follows:
C
char errorBuf[512]; DBR_InitLicense("LICENSE-KEY", errorBuf, 512); void* barcodeReader = DBR_CreateInstance(); C++
char errorBuf[512]; dynamsoft::dbr::CBarcodeReader::InitLicense("LICENSE-KEY", errorBuf, 512); CBarcodeReader* reader = new CBarcodeReader(); Note: you must set the license key globally before initializing the barcode reader object.
A Command-line C++ Barcode and QR Code Reader for Multiple Platforms
To conveniently build our C/C++ application on different platforms, we create a CMake project. In the CMakeLists.txt file, we specify the target platform and the corresponding build configuration. The most complicated part is to distinguish x64, ARM32, and ARM64 Linux distributions via CMAKE_SYSTEM_PROCESSOR:
MESSAGE( STATUS "CPU architecture ${CMAKE_SYSTEM_PROCESSOR}" ) if(WINDOWS) if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") link_directories("${PROJECT_SOURCE_DIR}/platforms/win/bin/") else() link_directories("${PROJECT_SOURCE_DIR}/platforms/win/lib/") endif() elseif(LINUX) if (CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64) MESSAGE( STATUS "Link directory: ${PROJECT_SOURCE_DIR}/platforms/linux/" ) link_directories("${PROJECT_SOURCE_DIR}/platforms/linux/") elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL armv7l OR ARM32_BUILD) MESSAGE( STATUS "Link directory: ${PROJECT_SOURCE_DIR}/platforms/arm32/" ) link_directories("${PROJECT_SOURCE_DIR}/platforms/arm32/") elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64) MESSAGE( STATUS "Link directory: ${PROJECT_SOURCE_DIR}/platforms/aarch64/" ) link_directories("${PROJECT_SOURCE_DIR}/platforms/aarch64/") endif() elseif(MACOS) MESSAGE( STATUS "Link directory: ${PROJECT_SOURCE_DIR}/platforms/macos/" ) link_directories("${PROJECT_SOURCE_DIR}/platforms/macos/") endif() include_directories("${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR}/include/") Since the target platforms include Windows, Linux, macOS, Raspberry Pi, and Jetson Nano, if you do not have theses devices, you can run the test with Travis CI by adding a Travis configuration file to the project:
language: cpp jobs: include: - os: linux arch: arm64 env: OS=arm64_linux - os: linux arch: amd64 env: OS=amd64_linux - os: windows arch: amd64 env: OS=windows - os: osx arch: amd64 env: OS=osx - os: linux arch: arm64 env: OS=arm32_linux before_install: - sudo dpkg --add-architecture armhf - sudo apt-get update - sudo apt-get install crossbuild-essential-armhf libc6:armhf libstdc++6:armhf branches: only: - main script: - mkdir build - cd build - if [[ ${TRAVIS_OS_NAME} == "windows" ]]; then cmake -DCMAKE_GENERATOR_PLATFORM=x64 ..; else if [ $OS == "arm32_linux" ]; then export CC=arm-linux-gnueabihf-gcc; export CXX=arm-linux-gnueabihf-g++; export AR=arm-linux-gnueabihf-ar; cmake -DARM32_BUILD=TRUE ..; else cmake ..; fi fi - cmake --build . --config release 
Here are the C/C++ coding steps:
-
Include the header files:
#include "DynamsoftBarcodeReader.h" #include "BarcodeReaderConfig.h" -
Set the license key and initialize the barcode reader object:
C
char errorBuf[512]; DBR_InitLicense("LICENSE-KEY", errorBuf, 512); void* barcodeReader = DBR_CreateInstance();C++
char errorBuf[512]; dynamsoft::dbr::CBarcodeReader::InitLicense("LICENSE-KEY", errorBuf, 512); CBarcodeReader* reader = new CBarcodeReader(); -
Decode barcode and QR code from an image file:
C
int errorCode = DBR_DecodeFile(barcodeReader, "image-file", "");C++
int errorCode = reader->DecodeFile("image-file", ""); -
Get the barcode and QR code results:
C
TextResultArray* pResults; DBR_GetAllTextResults(barcodeReader, &pResults);C++
TextResultArray* pResults; reader->GetAllTextResults(&pResults); -
Extract detailed information:
for (int index = 0; index < pResults->resultsCount; index++) { printf("Barcode %d:\n", index + 1); printf(" Type: %s\n", pResults->results[index]->barcodeFormatString); printf(" Text: %s\n", pResults->results[index]->barcodeText); }
Source Code
https://github.com/yushulx/cmake-cpp-barcode-qrcode-mrz
Build and Run
mkdir build cd build cmake .. cmake --build . --config release ./BarcodeReader [image-file] [optional: license-file] [optional: template-file] 