python zbar


zbar 可以解析 qrcode

不過安裝過程可是艱辛

 

本地開發用mac,生產服務器用ubuntu。安裝方式不同。整理出以下安裝方式

 

Ubuntu 14.04.1 LTS (GNU/Linux 3.8.0-29-generic x86_64)
$ apt-get install libzbar-dev
$ pip install zbar
 
 
 
Ubuntu 10.04.4 LTS
apt-get install libzbar-dev
apt-get install python-gtk2-dev
tar -xvf zbar-0.10.tar
./configure  --without-qt
make
make install
pip install zbar
 
 
mac
brew update && brew upgrade
brew install zbar
不要pip install zbar。而是下載這個包,修正了mac下的segmentation fault的bug
python setup.py install
 
 
 
qrcode解碼例子
#!/usr/bin/python
import zbar
from PIL import Image
import urllib
import cStringIO

#圖片地址替換成你的qrcode圖片地址
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)

 

 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM