1. *pyStrich (原huBarcode): 僅用於生成條碼、二維碼
pyStrich is a Python module to generate 1D and 2D barcodes. Currently it supports:
- code39
- code128
- ean13
- datamatrix
- qrcode
1.1. 安裝
pip install pyStrich
1.2. 使用
from pystrich.ean13 import EAN13Encoder
encoder = EAN13Encoder("690123456789")
encoder.save("pyStrich.png")
from pystrich.datamatrix import DataMatrixEncoder
encoder = DataMatrixEncoder("This is a DataMatrix.")
encoder.save( "sample_barcodes/datamatrix_test.png" )
print(encoder.get_ascii())
from pystrich.code39 import Code39Encoder
from pystrich.code128 import Code128Encoder
from pystrich.datamatrix import DataMatrixEncoder
from pystrich.ean13 import EAN13Encoder
from pystrich.qrcode import QRCodeEncoder
2. pylibdmtx: 僅支持DM碼
2.1. 安裝
The libdmtx DLLs are included with the Windows Python wheels. On other operating systems, you will need to install the libdmtx shared library.
- Mac OS X:
brew install libdmtx
- Linux:
sudo apt-get install libdmtx0a
Install this Python wrapper; use the second form to install dependencies of the read_datamatrix
and write_datamatrix
command-line scripts:
pip install pylibdmtx
pip install pylibdmtx[scripts]
2.2. Windows error message
If you see an ugly ImportError when importing pylibdmtx on Windows you will most likely need the Visual C++ Redistributable Packages for Visual Studio 2013
.
Install vcredist_x64.exe if using 64-bit Python, vcredist_x86.exe if using 32-bit Python.
2.3. 使用
2.3.1. 生成DM碼
The encode function generates an image containing a Data Matrix barcode:
>>> from pylibdmtx.pylibdmtx import encode
>>> encoded = encode('hello world'.encode('utf8'))
>>> img = Image.frombytes('RGB', (encoded.width, encoded.height), encoded.pixels)
>>> img.save('dmtx.png')
>>> print(decode(Image.open('dmtx.png')))
2.3.2. 解碼(支持PIL與numpy)
>>> from pylibdmtx.pylibdmtx import decode
>>> from PIL import Image
>>> decode(Image.open('pylibdmtx/tests/datamatrix.png'))
[Decoded(data='Stegosaurus', rect=Rect(left=5, top=6, width=96, height=95)),
Decoded(data='Plesiosaurus', rect=Rect(left=298, top=6, width=95, height=95))]
3. qrcode: 僅適用於qrcode的生成
3.1. 安裝
pip install qrcode
3.2. 命令行
qr 'Some data' > test.png
3.3. API
import qrcode
img = qrcode.make("xinxingzhao")
img.save("xinxing.png")
3.4. 更多的設置
上面兩種方式都是按照qrcode默認的方式生成二維碼,如果我們希望生成不同尺寸的二維碼就需要使用QRCode類了。
import qrcode
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data('Some data')
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
-
version
表示二維碼的版本號,二維碼總共有1到40個版本,最小的版本號是1,對應的尺寸是21×21,每增加一個版本會增加4個尺寸。這里說的尺寸不是只生成圖片的大小,而是值二維碼的長寬被平均分為多少份。
-
error_correction
指的是糾錯容量,這就是為什么二維碼上面放一個小圖標也能掃出來,糾錯容量有四個級別,分別是:
- ERROR_CORRECT_L L級別,7%或更少的錯誤能修正
- ERROR_CORRECT_M M級別,15%或更少的錯誤能修正,也是qrcode的默認級別
- ERROR_CORRECT_Q Q級別,25%或更少的錯誤能修正
- ERROR_CORRECT_H H級別,30%或更少的錯誤能修正
-
box_size 指的是生成圖片的像素
-
border 表示二維碼的邊框寬度,4是最小值
4. pyBarcode: 適用於EAN13等一維碼
支持的碼制列表:
- EAN-8
- EAN-13
- UPC-A
- JAN
- ISBN-10
- ISBN-13
- ISSN
- Code 39
- Code 128
- PZN
缺點:沒有畫出EAN13的起始符和終止符。
5. zxing: Java的解碼庫
Google維護的讀碼庫,很多Android上的App就是用的它。
- 1D product
- UPC-A
- UPC-E
- EAN-8
- EAN-13
- UPC/EAN Extension 2/5
- 1D industrial
- Code 39
- Code 93
- Code 128
- Codabar
- ITF
- RSS-14
- RSS-Expanded
- 2D
- QR Code
- Data Matrix
- Aztec
- PDF 417
- MaxiCode
5.1. C++: zxing-cpp
In pure C++14, no third-party dependencies.
Ubuntu安裝: apt install libzxingcore-dev
5.2. QZXing for Qt/QML
Qt/QML wrapper library for the ZXing library.
include(QZXing/QZXing.pri)
#include "QZXing.h"
int main()
{
QImage imageToDecode("file.png");
QZXing decoder;
//mandatory settings
decoder.setDecoder( DecoderFormat_QR_CODE | DecoderFormat_EAN_13 );
//optional settings
//decoder.setSourceFilterType(QZXing::SourceFilter_ImageNormal | QZXing::SourceFilter_ImageInverted);
decoder.setSourceFilterType(QZXing::SourceFilter_ImageNormal);
decoder.setTryHarderBehaviour(QZXing::TryHarderBehaviour_ThoroughScanning | QZXing::TryHarderBehaviour_Rotate);
//trigger decode
QString result = decoder.decodeImage(imageToDecode);
}
5.3. python-zxing: 一個python的wrapper
很久就已經停止維護了……
5.4. pyzxing
能夠直接鏈接Java庫,也是很牛的存在。。。
6. zbar: C的解碼庫
據說效率很高,遠超zxing。IOS上的官方讀碼庫。不過支持的條碼類型不如zxing豐富:
- EAN-13
- UPC-A,
- UPC-E,
- EAN-8,
- Code 128,
- Code 39,
- Interleaved 2 of 5
- QR Code.
注意,不支持以下類型:
- Data Matrix
- Axtec
- PDF417
Debian安裝: sudo apt install libzbar-dev
6.1. zbar/python
Debian安裝: sudo apt install python3-zbar
import zbar
scanner = zbar.ImageScanner()
image = zbar.Image(width, height, 'Y800', raw_data)
scanner.scan(image)
for symbol in image:
print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data
6.2. pyzbar, 條碼(二維碼)識別
在Ubuntu或樹莓派上安裝Zbar
$ sudo apt-get install libzbar0
$ pip3 install pyzbar
使用Demo
from pyzbar import pyzbar
barcodes = pyzbar.decode(im_qr)
for barcode in barcodes:
# x, y, w, h = barcode.rect # 獲取條碼位置
barcodeData = barcode.data.decode("utf-8")
barcodeType = barcode.type
print("[INFO] Found {} barcode: {}".format(barcodeType, barcodeData))
通過Pillow或OpenCV取圖像
>>> from pyzbar.pyzbar import decode
>>> from PIL import Image
>>> decode(Image.open('pyzbar/tests/code128.png'))
[
Decoded(
data=b'Foramenifera', type='CODE128',
rect=Rect(left=37, top=550, width=324, height=76),
polygon=[
Point(x=37, y=551), Point(x=37, y=625), Point(x=361, y=626),
Point(x=361, y=550)
]
)
Decoded(
data=b'Rana temporaria', type='CODE128',
rect=Rect(left=4, top=0, width=390, height=76),
polygon=[
Point(x=4, y=1), Point(x=4, y=75), Point(x=394, y=76),
Point(x=394, y=0)
]
)
]
7. OpenCV::QRCodeDetector
在opencv4.0以后,已經集成了二維碼識讀模塊,因此,我們可以采用最新的opencv來實現二維碼檢測和識讀。二維碼檢測和識別主要分為3步:使用 QRCodeDetector()
函數創建二維碼檢測器;使用 detectAndDecode()
函數對圖像進行二維碼檢測和識別;將檢測結果輸出。
# 創建二維碼檢測器
qrDecoder = cv2.QRCodeDetector()
# 逐幀顯示
while cv2.getWindowProperty("USB Camera", 0) >= 0:
ret_val, img = cap.read()
# 二維碼檢測和識別
data,bbox,rectifiedImage = qrDecoder.detectAndDecode(img)
if data:
print("解碼數據 : {}".format(data))
n = len(bbox)
for j in range(n):
cv2.line(img, tuple(bbox[j][0]), tuple(bbox[(j+1) % n][0]), (255,0,0), 3)
else:
print("沒有檢測到二維碼")
8. tesserocr: OCR字符識別
8.1. 安裝
8.1.1. Linux平台
sudo apt install libleptonica-dev libtesseract-dev
pip3 install tesserocr
8.1.2. Windows
下載whl文件: github: tesserocr-windows_build
如出現:
運行錯誤:DLL加載錯誤
安裝Python-Tesserocr需要
VS2015運行庫
。
安裝whl文件: pip install xxx.whl