Raspberry Pi Barcode Reader with DBR 5.2

Dynamsoft finally released Barcode Reader SDK v5.2 for Raspberry Pi. It is time to make an update. In this post, I will show you how to install the SDK, as well as how to write a simple Raspberry Pi barcode app using C/C++ and Python.

Installation

Tarball

Download dbr-rpi-5.2.0.tar.gz.

Extract the package:

tar -xvf dbr-rpi-5.2.0.tar.gz

Create a symlink for libDynamsoftBarcodeReader.so:

sudo ln –s <Your PATH>/libDynamsoftBarcodeReader.so /usr/lib/libDynamsoftBarcodeReader.so

Command line tool

Add public key:

wget -O - http://labs.dynamsoft.com/debian/conf/dbr.gpg.key | sudo apt-key add -

Add source to /etc/apt/sources.list:

deb http://labs.dynamsoft.com/debian/ dbr main non-free

Install Dynamsoft Barcode Reader:

sudo apt-get update && install dbr

Walkthrough of Raspberry Pi Barcode SDK

Getting started with C/C++ sample

Compile the built-in sample code:

cd <Package-root>/samples/c/
make
./BarcodeReaderDemo ../../images/Codabar.jpg

Barcode extension for Python 2 and Python 3

Create setup.py:

from distutils.core import setup, Extension
import os, numpy

numpy_include = os.path.join(os.path.dirname(numpy.__file__), "core", "include", "numpy")
print(numpy_include)
module_dbr = Extension('dbr',
                        sources = ['dbr.c'], 
                        include_dirs=[numpy_include, '<Path- DynamsoftBarcodeReader.h>'],
                        libraries=['DynamsoftBarcodeReader'])

setup (name = 'DynamsoftBarcodeReader',
        version = '5.2',
        description = 'Python barcode extension',
        ext_modules = [module_dbr])

Here I don’t explain dbr.c. If you are interested in the source code, please read How to Port C/C++ Barcode Extension to Python 3.

Build and install Python extension:

sudo python setup.py build install

Now we can quickly create a Raspberry Pi barcode app:

import os.path
import dbr

def initLicense(license):
    dbr.initLicense(license)

def decodeFile(fileName):
    formats = 0x3FF | 0x2000000 | 0x8000000 | 0x4000000 # 1D, QRCODE, PDF417, DataMatrix
    results = dbr.decodeFile(fileName, formats)
    
    for result in results:
        print("barcode format: " + result[0])
        print("barcode value: " + result[1])

def decodeBuffer(image):
    formats = 0x3FF | 0x2000000 | 0x8000000 | 0x4000000 # 1D, QRCODE, PDF417, DataMatrix
    results = dbr.decodeBuffer(image, formats)
    
    for result in results:
        print("barcode format: " + result[0])
        print("barcode value: " + result[1])

if __name__ == "__main__":
    barcode_image = input("Enter the barcode file: ")
    if not os.path.isfile(barcode_image):
        print("It is not a valid file.")
    else:
        initLicense("t0068MgAAAHtCgAPxEdCJN1bsu9n6YfnWDoaW7YZomIZZke2m9KynRnKSqsuQyd7Cdgo6razlb7VU3IFaKeBgg9Rq069Uihc=")
        decodeFile(barcode_image)

Create Shell scripts for SDK distribution

If you are a Python developer, you have to build the barcode module yourselves. We can use Shell scripts to automate the process.

Check CPU architectures for ARMv6 and ARMv7:

#!/bin/bash
CPU=$(lscpu)

ARMV6=$(echo $CPU | grep "armv6")
ARMV7=$(echo $CPU | grep "armv7")

if [ -n "${ARMV6}" ]
then
    echo "this is armv6"
    sudo ln -s $PWD/lib/armv6l/libDynamsoftBarcodeReader.so /usr/lib/libDynamsoftBarcodeReader.so
fi

if [ -n "${ARMV7}" ]
then
    echo "this is armv7"
    sudo ln -s $PWD/lib/armv7l/libDynamsoftBarcodeReader.so /usr/lib/libDynamsoftBarcodeReader.so
fi

Install essential packages for new environment:

sudo apt-get install python-dev python-pip
sudo pip install numpy

Build and install the Python module:

sudo python ./lib/python/setup.py build install

Remove the Python module:

#!/bin/bash
DBR_LIBRARY=$(python -c "import dbr; print(dbr.__file__)")
sudo rm "$DBR_LIBRARY"
sudo rm /usr/lib/libDynamsoftBarcodeReader.so

Raspberry Pi barcode SDK

Source Code

https://github.com/dynamsoft-dbr/raspberrypi-python-barcode