Update to Python interface
We’ve made some changes to the recommended way of interfacing Python on Windows to take account of Python 3 and Windows 64-bit.
The first change is in the loading of the DLL file itself..
if platform.machine().endswith(’64’):
bardecodeLibrary = os.getcwd() + ‘../../../SoftekBarcode64DLL.dll’
a=CDLL(bardecodeLibrary)
else:
bardecodeLibrary = os.getcwd() + ‘../../../SoftekBarcodeDLL.dll’
a=WinDLL(bardecodeLibrary)
Note the use of CDLL on 64-bit but WinDLL on 32-bit.
The second important change saves on some repetition later on…
# Create an instance of the bardecode toolkit
a.mtCreateBarcodeInstance.restype = ctypes.c_void_p
hBarcode = c_void_p(a.mtCreateBarcodeInstance())
…so we ensure that hBarcode is a c_void_p (as you would have probably assumed given the line above). This means you can just use it as normal without having to cast it each time.
So the entire sample Python script now reads as…
import os, platform
import ctypes
from ctypes import *
# Load the correct DLL file, note the use of CDLL on 64-bit and WinDLL on 32-bit
if platform.machine().endswith(’64’):
bardecodeLibrary = os.getcwd() + ‘../../../SoftekBarcode64DLL.dll’
a=CDLL(bardecodeLibrary)
else:
bardecodeLibrary = os.getcwd() + ‘../../../SoftekBarcodeDLL.dll’
a=WinDLL(bardecodeLibrary)
# Set the name of the image to decode
inputFile = ‘file.tif’
# Create an instance of the bardecode toolkit
a.mtCreateBarcodeInstance.restype = ctypes.c_void_p
hBarcode = c_void_p(a.mtCreateBarcodeInstance())
# Set a license key
a.mtSetLicenseKey(hBarcode, ‘YOUR LICENSE KEY’.encode(“utf-8”))
# Set the ReadQRCode property
a.mtSetReadQRCode(hBarcode, True)
# Scan the input file for barcodes
nBarcodes = a.mtScanBarCode(hBarcode, inputFile.encode(“utf-8”))
# Collect the output
for x in range(1, nBarcodes + 1):
a.mtGetBarString.restype = ctypes.c_char_p
barcodeValue = a.mtGetBarString(hBarcode, x)
print (barcodeValue.decode(“utf-8”))
a.mtDestroyBarcodeInstance(hBarcode)