如何使用 python-qrtools/ZBar 获取 QR/bar-code 的编码样式类型?

How to get type of Encoding Style of QR/bar-code with python-qrtools/ZBar?

在我的 Ubuntu 服务器上,我安装了 python-qrtools package (which makes use of zbar) 使用 sudo apt-get install python-qrtools 来解码 Python 中的二维码和条形码图像,如下所示:

>>> qr = qrtools.QR()
>>> qr.decode('the_qrcode_or_barcode_image.jpg')
True
>>> print qr.data
Hello! :)

这很好用。

我现在想存储此数据并在稍后的某个时间点重新生成图像。但问题是我不知道原始图像是二维码还是某种条形码。我检查了 qr 对象的所有属性,但其中 none 似乎给了我编码样式的类型 (QR/bar/other).

this SO thread中描述了ZBar确实返回了Encoding Style的类型,但是在Objective-C中它只是给出了一个例子,而且我不确定这是否真的是一个答案到我要找的东西。

有谁知道我如何在 Python 中找出编码样式的类型(所以 QR-code/BAR-code/other)(最好使用 python-qrtools 包)?如果不在 Python 中,是否有任何 Linux 命令行工具可以找到它?欢迎所有提示!

查看 qrtools 的源代码,我看不到任何获取类型的方法,但有一个 zbar python lib, based on the scan_image example,下面的代码似乎可以满足您的要求:

import zbar
import Image

scanner = zbar.ImageScanner()

scanner.parse_config('enable')

img = Image.open("br.png").convert('L')
width, height = img.size
stream = zbar.Image(width, height, 'Y800', img.tostring())

scanner.scan(stream)

for symbol in stream:
    print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data

使用我从 net 中抓取的随机条形码:

decoded UPCA symbol "123456789012"

使用这个 qr code 输出:

 decoded QRCODE symbol "http://www.reichmann-racing.de"