Building a C/C++ Barcode & QR Code Reader for Raspberry Pi with Dynamsoft SDK

Learn how to effortlessly set up a C/C++ barcode and QR code reader for Raspberry Pi using the Dynamsoft Barcode Reader SDK. This guide is optimized for Raspberry Pi 4 but is compatible with both ARM32 and ARM64 Raspberry Pi OS.

About Dynamsoft Barcode Reader SDK

Requirements

  • Ensure you have CMake installed. Update and install using:
    sudo apt update
    sudo apt install -y cmake
    

Building Barcode and QR Code Reader on Raspberry Pi

  1. Create a new C/C++ project with a CMakeLists.txt file.

    CMake project

  2. Configure the CMakeLists.txt file.

     cmake_minimum_required (VERSION 2.6)
     project (BarcodeReader)
    
     SET(CMAKE_CXX_FLAGS "-std=c++11 -O3 -Wl,-rpath=$ORIGIN")
     SET(CMAKE_INSTALL_RPATH "$ORIGIN")
     SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
    
     if(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()
     include_directories("${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR}/include/")
     add_executable(${PROJECT_NAME} BarcodeReader.cxx)
     target_link_libraries (${PROJECT_NAME} "DynamsoftBarcodeReader" pthread)
    
     if(CMAKE_SYSTEM_PROCESSOR STREQUAL armv7l OR ARM32_BUILD)
         install (DIRECTORY  "${PROJECT_SOURCE_DIR}/platforms/arm32/" DESTINATION ${PROJECT_SOURCE_DIR}/dist)
     elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64) 
         install (DIRECTORY  "${PROJECT_SOURCE_DIR}/platforms/aarch64/" DESTINATION ${PROJECT_SOURCE_DIR}/dist)
     endif()
    

    We use rpath to make the library search path to be the same as the executable. After running cmake --install ., the library files and executable files will be copied to the dist directory for distribution.

  3. In BarcodeReader.cxx, include the DynamsoftBarcodeReader.h file.

     #include "DynamsoftBarcodeReader.h"
    
  4. Set the license key for global access. You do not need to set the license key for each instance of the barcode reader.

     if (license)
     {
       char errorMsgBuffer[512];
       // Click https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform
       DBR_InitLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==", errorMsgBuffer, 512);
       printf("DBR_InitLicense: %s\n", errorMsgBuffer);
     }
    
  5. Initialize the barcode reader object:

     void *reader = DBR_CreateInstance();
    
  6. Configure the parameters of the barcode reader.

     char sError[512];
     PublicRuntimeSettings *runtimeSettings = new PublicRuntimeSettings();
     DBR_GetRuntimeSettings(reader, runtimeSettings);
     runtimeSettings->maxAlgorithmThreadCount = 1;
     runtimeSettings->barcodeFormatIds = BF_ALL;
     DBR_UpdateRuntimeSettings(reader, runtimeSettings, sError, 512);
     delete runtimeSettings;
    
  7. Call a decoding method based on your input source.
  8. Get the decoding results.

     TextResultArray *paryResult = NULL;
     DBR_GetAllTextResults(reader, &paryResult);
    
     if (paryResult->resultsCount == 0)
     {
       printf("No barcode found.\n");
       DBR_FreeTextResults(&paryResult);
       return -1;
     }
    
     printf("Thread id: %d. Total barcode(s) found: %d. Time cost: %d ms\n\n", thread_id, paryResult->resultsCount, timecost);
    
     for (int index = 0; index < paryResult->resultsCount; index++)
     {
       printf("Barcode %d:\n", index + 1);
       printf("    Type: %s\n", paryResult->results[index]->barcodeFormatString);
       printf("    Text: %s\n", paryResult->results[index]->barcodeText);
     }
    
     DBR_FreeTextResults(&paryResult);
    
     DBR_DestroyInstance(reader);
    
  9. Build the project and run the barcode and QR code reader in terminal.

     cmake --build . --config Release
     ./BarcodeReader ../images/AllSupportedBarcodeTypes.png 
    

    RaspberryPi barcode reader result

Source Code

https://github.com/yushulx/cmake-cpp-barcode-qrcode-mrz