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

About Dynamsoft C++ Barcode and QR Code Reader SDK

SDK Download

v9.0

SDK License

Click here to get a trial license. You can set the license key as follows:

C

char errorBuf[512];
DBR_InitLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==", errorBuf, 512);     
void* barcodeReader = DBR_CreateInstance();          

C++

char errorBuf[512];
dynamsoft::dbr::CBarcodeReader::InitLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==", 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

Travis build for C/C++ barcode QR code reader

Here are the C/C++ coding steps:

  1. Include the header files:

     #include "DynamsoftBarcodeReader.h"
     #include "BarcodeReaderConfig.h"
    
  2. Set the license key and initialize the barcode reader object:

    C

     char errorBuf[512];
     DBR_InitLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==", errorBuf, 512);     
     void* barcodeReader = DBR_CreateInstance();          
    

    C++

     char errorBuf[512];
     dynamsoft::dbr::CBarcodeReader::InitLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==", errorBuf, 512);    
     CBarcodeReader* reader = new CBarcodeReader(); 
    
  3. Decode barcode and QR code from an image file:

    C

     int errorCode = DBR_DecodeFile(barcodeReader, "image-file", "");        
    

    C++

     int errorCode = reader->DecodeFile("image-file", "");
    
  4. Get the barcode and QR code results:

    C

     TextResultArray* pResults;
     DBR_GetAllTextResults(barcodeReader, &pResults);
    

    C++

     TextResultArray* pResults;
     reader->GetAllTextResults(&pResults);
    
  5. 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

Build and Run

mkdir build
cd build
cmake ..
cmake --build . --config release
./BarcodeReader [image-file] [optional: license-file] [optional: template-file]

C/C++ barcode QR code reader