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.
This article is Part 1 in a 16-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 - How to Port Visual Studio C++ Project to Linux with CMake
- Part 4 - Insight Into Dynamsoft Barcode SDK Decoding Performance
- Part 5 - Building ARM64 Barcode and QR Code Scanner on Nvidia Jetson Nano
- Part 6 - How to Decode QR Code on Mac with Apple Silicon
- Part 7 - How to Develop a Desktop GUI Barcode Reader with Qt and C/C++
- Part 8 - How to Build a Desktop Barcode Scanner with Webcam Support Using Qt QCamera
- Part 9 - Building Command-line Barcode and QR Code Reader in C++
- Part 10 - How to Build Linux ARM32 and Aarch64 Barcode QR Scanner in Docker Container
- Part 11 - How to Link MSVC DLLs with MinGW GCC in Windows
- Part 12 - Transforming Raspberry Pi 4 into a Barcode Scanner with a C++ App, USB Camera, and OLED Display
- Part 13 - Building Windows Desktop Barcode Reader with Win32 API and Dynamsoft C++ Barcode SDK
- Part 14 - How to Build a Command-Line Barcode Reader with Rust and C++ Barcode SDK
- Part 15 - How to Decode Barcode and QR Code from WebP Images in C++ and Python
- Part 16 - Building a Desktop C++ Barcode Scanner with Slimmed-Down OpenCV and Webcam
About Dynamsoft Barcode Reader SDK
- Support for ARM32 and ARM64.
- Seamless integration with C/C++ projects on Raspberry Pi.
-
Download Dynamsoft C++ Barcode SDK v9.x
- Activate the SDK with a 30-day free trial license.
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
-
Create a new C/C++ project with a
CMakeLists.txt
file. -
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 runningcmake --install .
, the library files and executable files will be copied to thedist
directory for distribution. -
In
BarcodeReader.cxx
, include theDynamsoftBarcodeReader.h
file.#include "DynamsoftBarcodeReader.h"
-
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); }
-
Initialize the barcode reader object:
void *reader = DBR_CreateInstance();
-
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;
- Call a decoding method based on your input source.
- DecodeFile(): Decode barcodes from a specified image file.
- DecodeFileInMemory(): Decode barcodes from an image file in memory.
- DecodeBuffer(): Decode barcodes from raw buffer.
-
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);
-
Build the project and run the barcode and QR code reader in terminal.
cmake --build . --config Release ./BarcodeReader ../images/AllSupportedBarcodeTypes.png