kramer65
kramer65

Reputation: 53873

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

On my Ubuntu server I've installed the python-qrtools package (which makes use of zbar) using sudo apt-get install python-qrtools to decode images of QR-codes and bar-codes in Python like this:

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

This works perfectly fine.

I now want to store this data and regenerate the image at a later point in time. But the problem is that I don't know whether the original image was a QR-code or some type of bar-code. I checked all properties of the qr object, but none of them seems to give me the type of Encoding Style (QR/bar/other).

In this SO thread it is described that ZBar does give back the type of Encoding Style, but it only gives an example in Objective-C, plus I'm not sure if this is actually an answer to what I am looking for.

Does anybody know how I can find out the type of Encoding Style (so QR-code/BAR-code/other) in Python (preferably using the python-qrtools package)? And if not in Python, are there any Linux command line tools which can find this out? All tips are welcome!

Upvotes: 4

Views: 1094

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180411

Looking at the source of qrtools, I cannot see any way to get the type but there is a zbar python lib, based on the scan_image example, the code below seems to do what you want:

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

Using a random barcode I grabbed from the net:

decoded UPCA symbol "123456789012"

Using this qr code outputs:

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

Upvotes: 5

Related Questions