Package: barcode-qr-code-sdk
Python Extension: Barcode and QR Code SDKThe project is a CPython binding toDynamsoft C/C++ Barcode Reader SDK. It aims to help developers buildPython barcode and QR code scanningapps onWindows,LinuxandmacOS. Besides desktop PCs, it can work well on embedded and IoT devices such asRaspberry PiandJetson Nano. You arefreeto customize the Python API for Dynamsoft Barcode Reader.About Dynamsoft Barcode ReaderDynamsoft C/C++ Barcode Reader SDK v9.4.0Get a30-day FREE trial licenseto activate the SDK.Supported Python EditionPython 3.xInstall Dependenciespipinstallopencv-pythonCommand-line Usage$scanbarcode<file-name>-l<license-key># Show the image with OpenCV$scanbarcode<file-name>-u1-l<license-key>How to Build the Python Barcode and QR Code ExtensionCreate a source distribution:pythonsetup.pysdistsetuptools:pythonsetup_setuptools.pybuild
pythonsetup_setuptools.pydevelop# Copy libraries to barcodeQrSDK folderscikit-build:pythonsetup.pybuild
pythonsetup.pydevelop# Copy libraries to barcodeQrSDK folderBuild wheel:pipwheel.--verbose# Orpythonsetup_setuptools.pybdist_wheel# Orpythonsetup.pybdist_wheelQuick StartConsole AppimportbarcodeQrSDK# set licensebarcodeQrSDK.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")reader=barcodeQrSDK.createInstance()results,elapsed_time=reader.decodeFile("test.png")forresultinresults:print(result.format)print(result.text)print(result.x1)print(result.y1)print(result.x2)print(result.y2)print(result.x3)print(result.y3)print(result.x4)print(result.y4)Video AppimportbarcodeQrSDKimportnumpyasnpimportcv2importjsong_results=Nonedefcallback(results,elapsed_time):globalg_resultsg_results=(results,elapsed_time)defrun():# set licensebarcodeQrSDK.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")# initialize barcode scannerscanner=barcodeQrSDK.createInstance()params=scanner.getParameters()# Convert string to JSON objectjson_obj=json.loads(params)# json_obj['ImageParameter']['ExpectedBarcodesCount'] = 999params=json.dumps(json_obj)ret=scanner.setParameters(params)scanner.addAsyncListener(callback)cap=cv2.VideoCapture(0)whileTrue:ret,image=cap.read()ifimageisnotNone:scanner.decodeMatAsync(image)ifg_results!=None:print('Elapsed time: '+str(g_results[1])+'ms')cv2.putText(image,'Elapsed time: '+str(g_results[1])+'ms',(10,30),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),2)forresulting_results[0]:x1=result.x1y1=result.y1x2=result.x2y2=result.y2x3=result.x3y3=result.y3x4=result.x4y4=result.y4cv2.drawContours(image,[np.int0([(x1,y1),(x2,y2),(x3,y3),(x4,y4)])],0,(0,255,0),2)cv2.putText(image,result.text,(x1,y1),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)cv2.imshow('Barcode QR Code Scanner',image)ch=cv2.waitKey(1)ifch==27:breakscanner.clearAsyncListener()if__name__=='__main__':run()MethodsbarcodeQrSDK.initLicense('YOUR-LICENSE-KEY')# set barcode SDK license globallybarcodeQrSDK.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")barcodeQrSDK.createInstance()# create a barcode reader instancereader=barcodeQrSDK.createInstance()decodeFile(filename)# decode barcode and QR code from an image fileresults,elapsed_time=reader.decodeFile("test.png")decodeMat(Mat image)# decode barcode and QR code from Matimage=cv2.imread("test.png")results=reader.decodeMat(image)forresultinresults:print(result.format)print(result.text)print(result.x1)print(result.y1)print(result.x2)print(result.y2)print(result.x3)print(result.y3)print(result.x4)print(result.y4)getParameters()# return JSON stringparams=reader.getParameters()setParameters(JSON string)# set barcode SDK parametersimportjsonjson_obj=json.loads(params)json_obj['ImageParameter']['DPMCodeReadingModes'][0]['Mode']='DPMCRM_GENERAL'json_obj['ImageParameter']['LocalizationModes'][0]['Mode']='LM_STATISTICS_MARKS'params=json.dumps(json_obj)ret=reader.setParameters(params)addAsyncListener(callback function)# start a native thread and register a Python function for receiving barcode QR code resultsdecodeMatAsync(<opencv mat data>)# decode barcode QR code from OpenCV Mat asynchronouslydefcallback(results,elapsed_time):print(results)importcv2image=cv2.imread("test.png")reader.addAsyncListener(callback)reader.decodeMatAsync(image)sleep(1)clearAsyncListener()# stop the native thread and clear the registered Python functiondecodeBytes(bytes, width, height, stride, imageformat)# 0: gray; 1: rgb888importcv2image=cv2.imread("test.png")results,elapsed_time=scanner.decodeBytes(image.tobytes(),image.shape[1],image.shape[0],image.strides[0],barcodeQrSDK.ImagePixelFormat.IPF_BGR_888)decodeBytesAsync# decode image byte array asynchronouslydefcallback(results,elapsed_time):print(results)importcv2image=cv2.imread("test.png")imagebytes=image.tobytes()scanner.decodeBytesAsync(image.tobytes(),image.shape[1],image.shape[0],image.strides[0],barcodeQrSDK.ImagePixelFormat.IPF_BGR_888)sleep(1)Online Documentation for Dynamsoft C/C++ Barcode SDKTo customize Python API based on C/C++, please refer to theonline documentation.Supported Barcode SymbologiesLinear Barcodes (1D)Code 39 (including Code 39 Extended)Code 93Code 128CodabarInterleaved 2 of 5EAN-8EAN-13UPC-AUPC-EIndustrial 2 of 52D Barcodes:QR Code (including Micro QR Code)Data MatrixPDF417 (including Micro PDF417)Aztec CodeMaxiCode (mode 2-5)Patch CodeGS1 Composite Code