When creating a barcode reader app with a free barcode SDK, you may come up with ZXing instantly. ZXing is an open-source barcode reading and decoding library implemented in Java. There are some third-party projects, either ports or bindings, available for other programming languages. So if you want to create a Python barcode reader, you can use Python ZXing, which is a Python wrapper for invoking ZXing Java class. If you care about the performance, we can use Python ZBar, a Python wrapper for the ZBar C++ code, as a comparison.
Making Python ZXing Work
Get the source code of ZXing:
git clone https://github.com/zxing/zxing.git
Build the project using Maven:
cd zxing
mvn install
Generate a .jar file with dependencies:
cd javasemvn package assembly:single -DskipTests
On Windows 10, you may fail to decode a barcode image using the command as follows:
java -cp D:\zxing\javase\target\javase-3.4.1-SNAPSHOT-jar-with-dependencies.jar com.google.zxing.client.j2se.CommandLineRunner D:\dataset\20499525_2.jpg
If you change the backward slash contained in the file path to forward slash, you will get different error information:
java -cp D:\zxing\javase\target\javase-3.4.1-SNAPSHOT-jar-with-dependencies.jar com.google.zxing.client.j2se.CommandLineRunner D:/dataset/20499525_2.jpg
How to fix the unknown protocol error? The workaround is to use the URI scheme file:///D:/dataset/20499525_2.jpg:
java -cp D:\zxing\javase\target\javase-3.4.1-SNAPSHOT-jar-with-dependencies.jar com.google.zxing.client.j2se.CommandLineRunner file:///D:/dataset/20499525_2.jpg
Search for the keyword “Python” on the homepage of ZXing repository.
Visit the python-zxing project to get the source code:
git clone https://github.com/oostendo/python-zxing.git
Edit the line in zxing\__init__.py file. Replace the libs with the .jar file built from the ZXing source code:
libs = ["javase/target/javase-3.4.1-SNAPSHOT-jar-with-dependencies.jar"]
Build and install the Python module:
python setup.py build install
Create an app.py file:
The barcode result returned from ZXing contains a trailing newline, so we can use rstrip() to remove it.
Run the program:
> python app.py -i D:\dataset\20499525_2.jpgZXing: 20499525
ZBar: The Alternative to ZXing
Not like ZXing, installing ZBar is much easier:
pip install pyzbar
Add the code snippet of using ZBar to app.py file:
import pyzbar.pyzbar as zbarfrom PIL import Imagebarcode = zbar.decode(Image.open(image))print('ZBar: {}'.format(barcode[0].data.decode("utf-8")))
Rerun the Python program and we can see both of them decode barcode correctly:
> python3 app.py -i D:\dataset\20499525_2.jpgZXing: 20499525ZBar: 20499525
We can take a further look at the time cost of decoding barcode:
Although they both return the correct results, Python ZBar is much faster than ZXing:
> python3 app.py -i D:\dataset\20499525_2.jpgZXing: 20499525. Elapsed time: 2170msZBar: 20499525. Elapsed time: 207ms
The reason is Python ZXing uses subprocess module to get barcode results from Java process:
In my next article, I will use Python code to compare the recognition rate among ZXing, ZBar, and Dynamsoft Barcode Reader based on the public dataset https://drive.google.com/uc?id=1uThXXH8HiHAw6KlpdgcimBSbrvi0Mksf&export=download.
Source Code
Originally published at https://www.codepool.biz on February 5, 2020.