zoukankan      html  css  js  c++  java
  • 条码生成与解析

    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. OpenCV::QRCodeDetector

    7. zbar: C++的解码库

    7.1. 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))
    

    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

  • 相关阅读:
    嵌入优酷视频
    简单字符串处理
    随机生成一个大写字母
    生成二维码
    【计算机网络(谢希仁)-读书笔记】5.2 用户数据报协议UDP
    【计算机网络(谢希仁)-读书笔记】5.1 运输层协议概述
    【计算机网络(谢希仁)-读书笔记】4.6 IP多播
    【计算机网络(谢希仁)-读书笔记】4.5 因特网的路由选择协议
    【计算机网络(谢希仁)-读书笔记】4.4 网际控制报文协议ICMP
    【计算机网络(谢希仁)-读书笔记】4.3 划分子网和构造超网
  • 原文地址:https://www.cnblogs.com/brt2/p/13204632.html
Copyright © 2011-2022 走看看