Comparing Barcode Scanning in Python: ZXing vs. ZBar vs. Dynamsoft Barcode Reader
In the world of Python barcode scanning, developers have several options to choose from, including ZXing, ZBar, and the Dynamsoft Barcode Reader. Each of these libraries offers unique strengths, catering to different needs and environments. ZXing and ZBar are popular open-source options, known for their community-driven development and flexibility. On the other hand, Dynamsoft Barcode Reader provides a robust, enterprise-grade solution with advanced features and official support. This article delves into the performance, ease of use, and compatibility of these libraries, providing you with the insights needed to select the best barcode scanning tool for your Python projects.
This article is Part 9 in a 11-Part Series.
- Part 1 - Detecting and Decoding QR Codes in Python with YOLO and Dynamsoft Barcode Reader
- Part 2 - How to a GUI Barcode Reader with Qt PySide6 on Raspberry Pi
- Part 3 - Advanced GUI Python Barcode and QR Code Reader for Windows, Linux, macOS and Rasberry Pi OS
- Part 4 - Advanced QR Code Recognition: Handling Inverted Colors, Perspective Distortion, and Grayscale Images
- Part 5 - Scanning QR Code from Desktop Screen with Qt and Python Barcode SDK
- Part 6 - Building an Online Barcode and QR Code Scanning App with Python Django
- Part 7 - Real-Time Barcode and QR Code Scanning with Webcam, OpenCV, and Python
- Part 8 - How to Build Flet Chat App with Barcode and Gemini APIs
- Part 9 - Comparing Barcode Scanning in Python: ZXing vs. ZBar vs. Dynamsoft Barcode Reader
- Part 10 - Python Ctypes: Invoking C/C++ Shared Library and Native Threading
- Part 11 - A Guide to Running ARM32 and ARM64 Python Barcode Readers in Docker Containers
Overview of Barcode Scanning Libraries
ZXing (Zebra Crossing)
ZXing is a widely-used open-source barcode scanning library that originated from Google. It supports a variety of barcode formats, including 1D and 2D codes like QR codes, and is primarily written in Java, though it has been ported to other languages, including Python. ZXing is known for its flexibility and wide community support, making it a popular choice for developers working on general barcode scanning projects.
ZBar
ZBar is another open-source barcode scanning library, written in C, that supports 1D and 2D barcode formats, including QR codes. ZBar is particularly known for its efficiency and lightweight nature, making it a great choice for applications where performance is critical. It also has Python bindings, which allow for easy integration into Python projects. ZBar’s simplicity and focus on core functionality make it a reliable choice for many developers.
Dynamsoft Barcode Reader
Dynamsoft Barcode Reader is a commercial, enterprise-grade solution that offers advanced features and high-performance barcode scanning capabilities. It supports a wide range of barcode formats and is available across multiple platforms, including Windows, Linux, macOS, Android, and iOS. With official support and frequent updates, Dynamsoft Barcode Reader is ideal for developers needing robust and reliable barcode scanning in mission-critical applications.
Prerequisites
- Obtain a Dynamsoft Barcode Reader Trial License.
- Download image dataset: https://drive.google.com/uc?id=1uThXXH8HiHAw6KlpdgcimBSbrvi0Mksf&export=download.
-
Install
dbr
,pyzbar
,zxing-cpp
,opencv-python
, andnumpy
Python packages:pip install dbr pyzbar zxing-cpp opencv-python numpy
Python Barcode Detection with ZXing, ZBar, and Dynamsoft Barcode Reader
The following code snippet demonstrates how to decode barcodes using ZXing, ZBar, and Dynamsoft Barcode Reader in Python.
Python ZXing
import zxingcpp
import cv2
import numpy as np
def zxing_decode(filename):
start = time.time()
img = cv2.imread(filename)
zxing_results = zxingcpp.read_barcodes(img)
elapsed_time = time.time() - start
if zxing_results != None:
for result in zxing_results:
print('ZXing: {}. Elapsed time: {}ms'.format(
result.text, int(elapsed_time * 1000)))
cv2.drawContours(
img, [np.intp([(result.position.top_left.x, result.position.top_left.y), (result.position.top_right.x, result.position.top_right.y), (result.position.bottom_right.x, result.position.bottom_right.y), (result.position.bottom_left.x, result.position.bottom_left.y)
])], 0, (0, 255, 0), 2)
cv2.imshow('ZXing', img)
return zxing_results
else:
print('ZXing failed to decode {}'.format(filename))
return None
Python ZBar
import pyzbar.pyzbar as zbar
import cv2
import numpy as np
def zbar_decode(filename):
img = cv2.imread(filename)
start = time.time()
zbar_results = zbar.decode(Image.open(filename))
elapsed_time = time.time() - start
if len(zbar_results) > 0:
for zbar_result in zbar_results:
print('ZBar: {}. Elapsed time: {}ms'.format(
zbar_result.data.decode("utf-8"), int(elapsed_time * 1000)))
cv2.drawContours(
img, [np.intp([zbar_result.polygon[0], zbar_result.polygon[1], zbar_result.polygon[2], zbar_result.polygon[3]
])], 0, (0, 255, 0), 2)
cv2.imshow('zbar', img)
return zbar_results
else:
print('ZBar failed to decode {}'.format(filename))
return None
Python Dynamsoft Barcode Reader
from dbr import *
import cv2
import numpy as np
BarcodeReader.init_license(
'LICENSE-KEY')
dbr_reader = BarcodeReader()
def dbr_decode(dbr_reader, filename):
img = cv2.imread(filename)
try:
start = time.time()
dbr_results = dbr_reader.decode_file(filename)
elapsed_time = time.time() - start
if dbr_results != None:
for text_result in dbr_results:
# print(textResult["BarcodeFormatString"])
print('Dynamsoft Barcode Reader: {}. Elapsed time: {}ms'.format(
text_result.barcode_text, int(elapsed_time * 1000)))
points = text_result.localization_result.localization_points
cv2.drawContours(
img, [np.intp([points[0], points[1], points[2], points[3]])], 0, (0, 255, 0), 2)
cv2.imshow('DBR', img)
return dbr_results
else:
print("DBR failed to decode {}".format(filename))
except Exception as err:
print("DBR failed to decode {}".format(filename))
return None
Using openpyxl to Generate Barcode Detection Report
Openpyxl is a Python library that allows you to read and write Excel files. You can use it to generate a report of barcode detection results, including the barcode text and detection accuracy.
-
Install the
openpyxl
Python package:pip install openpyxl
-
Create an Excel workbook and write barcode detection results to it:
from openpyxl import Workbook def dataset(directory=None, dbr_reader=None): if directory != None: print(directory) files = os.listdir(directory) files = [f for f in files if f.endswith('.jpg') or f.endswith('.png')] total_count = len(files) if total_count == 0: print('No image files') return # Create a .xlsx file datafile = 'benchmark.xlsx' wb = data.get_workbook(datafile) index = 2 print('Total count of barcode image files: {}'.format(total_count)) zbar_count = 0 dbr_count = 0 zxing_count = 0 for filename in files: file_path = os.path.join(directory, filename) expected_result = filename.split('_')[0] r1 = '' r2 = '' r3 = '' # ZBar zbar_results = zbar_decode(file_path) if zbar_results != None: for zbar_result in zbar_results: zbar_text = zbar_result.data.decode("utf-8") r1 = zbar_text if r1 == expected_result: zbar_count += 1 break else: print('Fail to decode {}'.format(filename)) # DBR if dbr_reader != None: textResults = dbr_decode(dbr_reader, file_path) if textResults != None: for textResult in textResults: r2 = textResult.barcode_text if r2 == expected_result: dbr_count += 1 break else: print("DBR failed to decode {}".format(filename)) # ZXing print('ZXing decoding {}'.format(filename)) zxing_results = zxing_decode(file_path) if zxing_results != None: for result in zxing_results: r3 = result.text if r3 == expected_result: zxing_count += 1 break else: print('ZXing failed to decode {}'.format(filename)) # Add results to .xlsx file data.update_row(wb, index, filename, expected_result, r1, r2, r3) index += 1 r1 = 0 r2 = 0 r3 = 0 zbar_rate = zbar_count * 100 / total_count r1 = '{0:.2f}%'.format(zbar_rate) print('ZBar recognition rate: {0:.2f}%'.format(zbar_rate)) if dbr_reader != None: dbr_rate = dbr_count * 100 / total_count r2 = '{0:.2f}%'.format(dbr_rate) print('DBR recognition rate: {0:.2f}%'.format(dbr_rate)) zxing_rate = zxing_count * 100 / total_count r3 = '{0:.2f}%'.format(zxing_rate) print('ZXing recognition rate: {0:.2f}%'.format(zxing_rate)) data.set_recognition_rate(wb, index, r1, r2, r3) # Save data to .xlsx file data.save_workbook(wb, datafile)
-
Run the script and check the generated Excel file for barcode detection results.
Source Code
https://github.com/yushulx/python-barcode-qrcode-sdk/tree/main/examples/official/9.x/zxing_zbar