Quantcast
Channel: Barcode – Dynamsoft Developers
Viewing all articles
Browse latest Browse all 145

Things to Do with DBR 6.0 and Python Barcode Extension

$
0
0

Dynamsoft Barcode Reader 6.0 SDK is the best version ever. It dramatically improved barcode detecting speed and accuracy, especially for multiple barcodes per frame. Since there are some new APIs and parameters, I have to update both C++ and Python code for the Python barcode extension that was built with DBR 5.2 correspondingly.

Download

Setup.py for Building and Installing Python Barcode Extension

Import relevant modules.

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

from distutils.command.install import install

Dynamically get the header file directory of NumPy.

numpy_include = os.path.join(os.path.dirname(numpy.__file__), "core", "include", "numpy")

Set the library (.lib and .dll) paths of Dynamsoft Barcode Reader. To build the extension yourself, you have to replace following code with yours.

dbr_lib_dir = r'e:\Program Files (x86)\Dynamsoft\Barcode Reader 6.0\Components\C_C++\Lib'
dbr_dll = r'e:\Program Files (x86)\Dynamsoft\Barcode Reader 6.0\Components\C_C++\Redist\x86\DynamsoftBarcodeReaderx86.dll'

Specify parameters for Python extension.

module_dbr = Extension('dbr', sources=['dbr.c'], include_dirs=[
                       numpy_include], library_dirs=[dbr_lib_dir], libraries=['DBRx86'])

Create a customized class to extending installation command.

class CustomInstall(install):
      def run(self):
          install.run(self)
          import shutil
          from distutils.sysconfig import get_python_lib
          src = dbr_dll
          dst = get_python_lib()
          shutil.copy2(src, dst)

setup(name='dbr',
      version='6.0',
      description='Python barcode extension',
      author='Xiao Ling',
      author_email='xiao@dynamsoft.com',
      url='https://www.dynamsoft.com/Products/Dynamic-Barcode-Reader.aspx',
      license = 'https://www.dynamsoft.com/Products/barcode-reader-license-agreement.aspx',
      ext_modules=[module_dbr],
      long_description='Dynamsoft Barcode Reader is a software development toolkit which enables barcode recognition of Code 39, Code 129, QR Code, DataMatrix, PDF417.',
      platforms=['Windows'],
      cmdclass={'install': CustomInstall}
)

What does the customized class do? Distutils will build and install dbr.pyd to Python library directory but not include dependent DynamsoftBarcodeReaderx86.dll. I used to manually copy the file to the Python library directory, though, a smarter way is to trigger a post-install event and copy the file automatically.

Parameter Templates for Reading Barcode

Parameter template is a new feature of DBR 6.0. Instead of hardcoding parameters, we can now just modify a JSON file to make the application run differently without re-compile the source code.

barcode param template

Writing less code is cool.

python barcode dbr 6.0

Create a function to load the template.

static PyObject *
loadSettings(PyObject *self, PyObject *args)
{
    if (!createDBR()) 
    {
        return NULL;
    }

    char *pszSettingFile;
    if (!PyArg_ParseTuple(args, "s", &pszSettingFile)) {
        return NULL;
    }

    char szErrorMsg[256];
    int ret = DBR_LoadSettingsFromFile(hBarcode, pszSettingFile, szErrorMsg, 256);
    return Py_BuildValue("i", ret);
}

Create test.py. Put templates folder, which contains default.settings.json and custom.json, in the same directory. The default.settings.json file is for global settings. Usually, we do not need to edit it.

import os
import dbr
import cv2

def loadSettings(setting_file):
    dbr.loadSettings(setting_file)

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

def decodeFile(fileName):
    formats = 0x3FF | 0x2000000 | 0x8000000 | 0x4000000 # 1D, QRCODE, PDF417, DataMatrix
    results = dbr.decodeFile(fileName, formats, 'CUSTOM')
    
    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, 'CUSTOM')
    
    for result in results:
        print("barcode format: " + result[0])
        print("barcode value: " + result[1])

if __name__ == "__main__":
    import sys
    if sys.version_info < (3, 0):
        barcode_image = raw_input("Enter the barcode file: ")
    else:
        barcode_image = input("Enter the barcode file: ")
        
    if not os.path.isfile(barcode_image):
        print("It is not a valid file.")
    else:
        initLicense("t0068MgAAABt/IBmbdOLQj2EIDtPBkg8tPVp6wuFflHU0+y14UaUt5KpXdhAxlERuDYvJy7AOB514QK4H50mznL6NZtBjITQ=")
        setting_file = os.path.join(os.getcwd(), 'templates', 'default.settings.json')
        loadSettings(setting_file)
        decodeFile(barcode_image)
        image = cv2.imread(barcode_image, 1)
        decodeBuffer(image)

Source Code

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

The post Things to Do with DBR 6.0 and Python Barcode Extension appeared first on Code Pool.


Viewing all articles
Browse latest Browse all 145

Trending Articles