Zbar parse the qrcode

2019-09-0111:01:36 Comment

Python can use zbar lib parse the qrcode information, There shows how to install the zbar lib and python using code.

1、Install

apt-get install libzbar-dev
apt-get install python-gtk2-dev
wget http://iweb.dl.sourceforge.net/project/zbar/zbar/0.10/zbar-0.10.tar.bz2
bzip2 -d zbar-0.10.tar.bz2
tar -xvf zbar-0.10.tar
./configure  --without-qt
make
make install
pip install zbar

2、Python code

#!/usr/bin/python
import zbar
from PIL import Image
import urllib
import cStringIO

#jpeg url replace to your qrcode url
URL = ('http://example.qiniudn.com/msgimagepicc4WJ-4iTk8.jpeg')
# create a reader
scanner = zbar.ImageScanner()
# configure the reader
scanner.parse_config('enable')
# obtain image data
imgfile = cStringIO.StringIO(urllib.urlopen(URL).read())
pil = Image.open(imgfile).convert('L')

width, height = pil.size
raw = pil.tostring()
# wrap image data
image = zbar.Image(width, height, 'Y800', raw)
# scan the image for barcodes
scanner.scan(image)
# extract results
for symbol in image:
    # do something useful with results
    print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data
# clean up
del(image)

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: