Setting up a C++ Barcode Scanner on Raspberry Pi

Turn your Raspberry Pi into a barcode scanner using C++, our Linux SDK, and a few simple commands. Here’s our step-by-step tutorial to get you started.

Kevin October 30, 2024 5 mins read
app store

Introduction: Scanning barcodes with C++ on Linux devices

Having a Barcode Scanner SDK compatible with Linux devices opens up many possibilities for processing visual data on servers, embedded devices, and even drones. You can use one device to scan as many barcodes as you like (e.g., in a warehouse), send the barcodes’ values to the central server, and then process the data there. Or you can use one device for both steps. With Linux, virtually anything is possible.

To guide you through the setup process, we’ll run the C++ version of our Linux SDK example app on a Raspberry Pi. When we’re done, the values of the scanned barcodes will be displayed in the terminal.

To achieve this, we’re going to follow these steps:

  1. Download and extract the example app
  2. Paste the license key into main.cpp
  3. Build the barcode scanner project
  4. Run the barcode scanner executable

⚠️ Our Linux SDK is currently in closed beta. To receive the necessary license key, API token, and version number, please send us an email at: beta@scanbot.io

We’ll be using a Raspberry Pi 5 running Raspberry Pi OS (we also have some tweaks you can use on an NVIDIA Jetson Nano). The Linux SDK also works on other distributions with GLIBC 2.27 or higher. Just make sure that your device has either an ARM64 or x86_64 architecture and enough internal storage for OpenCV. To test the live detection feature, your device also needs a camera.

Step 1: Download and extract the example app

Our example app includes everything you need for processing barcodes both from image files and from a live camera feed. Just download or clone our GitHub repository and unzip the files if necessary.

The directory structure is as follows:

scanbot-sdk-example-linux/
├── Readme.md
└── examples/
    ├── c++/
    │   ├── README.md
    │   ├── live-recognition/
    │   │   ├── CMakeLists.txt
    │   │   ├── Readme.md
    │   │   ├── cmake/
    │   │   │   └── FindScanbotSDK.cmake
    │   │   └── src/
    │   │       └── main.cpp
    │   └── minimal/
    │       ├── CMakeLists.txt
    │       ├── Readme.md
    │       ├── cmake/
    │       │   └── FindScanbotSDK.cmake
    │       └── src/
    │           └── main.cpp
    ├── example.png
    ├── java/
    │   ├── ...
    │   ...
    └── python/
        ├── ...
        ...

For the next two steps, you need to know three things:

  • The current Scanbot SDK version number
  • Your API token for the Scanbot SDK
  • Your Scanbot SDK license key

If you haven’t yet received these yet, please contact us at beta@scanbot.io.

Step 2: Paste the license key into main.cpp

Open main.cpp in scanbot-sdk-example-linux/examples/c++/live-recognition/src and paste your license key:

const std::string LICENSE_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

Save the main.cpp file.

Next, we’re going to build the barcode scanner project using the Scanbot SDK version number and API token.

Step 3: Build the barcode scanner project

Create a build folder in scanbot-sdk-example-linux/examples/c++/live-recognition/ and navigate into it:

mkdir build
cd build

Then generate the build files for our project while specifying the Scanbot SDK version and API token:

cmake -DSCANBOTSDK_VERSION=XX.XX -DSCANBOTSDK_API_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ..

And build the barcode scanner project:

make

💡 If you want to run both the minimal and the live recognition experiments, repeat steps 2 and 3 for the other folder.

Step 4: Run the barcode scanner executable

We can now run the detect_barcodes executable to scan some barcodes!

The following steps differ depending on whether you want to run minimal or live recognition.

For minimal (barcode recognition on an image file)

Run the following command inside the minimal folder. If you’d like to use your own file for testing, just replace the image file path.

./detect_barcodes ../../../example.png

Your output should look similar to this:

Initializing Scanbot SDK...
License Status: LicenseStatus.OK
Found 3 barcodes
	Barcode(text='https://qrcodes.pro/lwKmGZ', format=<BarcodeFormat.QR_CODE: 11>, raw_bytes=b'https://qrcodes.pro/lwKmGZ', points=[135, 175, 256, 175, 256, 296, 135, 296])
	Barcode(text='https://qrcodes.pro/eQnvl5', format=<BarcodeFormat.QR_CODE: 11>, raw_bytes=b'https://qrcodes.pro/eQnvl5', points=[325, 175, 446, 174, 446, 296, 325, 296])
	Barcode(text='https://qrcodes.pro/XPAkG3', format=<BarcodeFormat.QR_CODE: 11>, raw_bytes=b'https://qrcodes.pro/XPAkG3', points=[515, 174, 636, 175, 636, 297, 515, 296])

🎉 Congratulations, you successfully scanned barcodes from an image file!

For live recognition (barcode reading with a camera)

You’ll need to run different commands depending on your setup.

Using a USB camera

In the following command, replace 0 with your camera’s device node number, then run it from the live recognition folder.

./detect_barcodes -i 0

Using a CSI camera with libcamera on Raspberry Pi OS:

./detect_barcodes -i libcamera

Using a CSI camera on an NVIDIA Jetson Nano

./detect_barcodes -i jetson_csi

All you have to do then is to hold barcodes to the camera. The SDK will scan them automatically and print out their data. The SDK will continue to scan barcodes until you terminate the script using Ctrl+C.

🎉 Congratulations, you successfully scanned barcodes from a live camera feed!

Optional flags

You can add additional flags to your command to customize the output:

  • --use-display: Display a window that shows the recognized barcodes in a live view. The recognized barcodes are indicated by a green border overlay.
  • --use-tensorrt: Enable TensorRT acceleration (Jetson only).

💡 The frames per second you can achieve with --use-display during live detection depend on the power of your hardware. Hence, running the script on a Jetson Nano will result in smoother playback than on a Raspberry Pi.

Framerate comparison between Raspberry Pi and NVIDIA Jetson Nano

Learn more about the Linux Barcode Scanner SDK

The Linux Barcode Scanner SDK is currently in closed beta, with the SDK itself and a trial license available on request via beta@scanbot.io.

Our customer Cypher Robotics uses the Linux SDK in its automated inventory cycle counting system to scan barcoded items autonomously in a warehouse environment.

If you have a use case you would like to discuss, feel free to reach out to us.