条码生成与解析

1. *pyStrich (原huBarcode)

github

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码

github

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的生成

github

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等一维码

github

支持的码制列表:

  • EAN-8
  • EAN-13
  • UPC-A
  • JAN
  • ISBN-10
  • ISBN-13
  • ISSN
  • Code 39
  • Code 128
  • PZN

缺点:没有画出EAN13的起始符和终止符。

5. zxing: Java的解码库

5.1. python-zxing: 一个python的wrapper

支持识别二维码?

6. zbar: C++的解码库

6.1. pyzbar

7. OpenCV::QRCodeDetector

猜你喜欢

转载自www.cnblogs.com/brt2/p/13204632.html