使用python做二位码的解析。非常的方便,需要安装zbar,我使用的是python2.6,Windows Xp,下载的是zbar-0.10.win32-py2.6.exe可以到http://pypi.python.org/pypi/zbar/上面下载,这个安装包比较方便,源码安装比较麻烦,还需要安装Mingw,我在虚拟机下面是python2.7试一下安装没有成功。还需要安装zbar-0.10-setup.exe。安装完如下所示可以直接使用。将zar安装目录下面 的dll文件复制到Python26\Lib\site-packages下,就可以使用python的zbar模块了。
简单图片扫描:
#!/usr/bin/python from sys import argv import zbar import Image if len(argv) < 2: exit(1) # create a reader scanner = zbar.ImageScanner() # configure the reader scanner.parse_config('enable') # obtain image data pil = Image.open(argv[1]).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)视频检测:
#!/usr/bin/python from sys import argv import zebra # create a Processor proc = zbar.Processor() # configure the Processor proc.parse_config('enable') # initialize the Processor device = '/dev/video0' if len(argv) > 1: device = argv[1] proc.init(device) # setup a callback def my_handler(proc, image, closure): # extract results for symbol in image: if not symbol.count: # do something useful with results print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data proc.set_data_handler(my_handler) # enable the preview window proc.visible = True # initiate scanning proc.active = True try: proc.user_wait() except zbar.WindowClosed: pass以上内容都可以在 http://sourceforge.net/apps/mediawiki/zbar/index.php?title=ZBar_Wiki找到详细信息。(这边记录为了以后方便学习)
try: utf8Data = symbol.data.decode("gbk") except UnicodeDecodeError: try: utf8Data = symbol.data.decode("utf-8").encode("gbk") except: utf8Data=symbol.data.decode('utf-8').encode('sjis').decode('utf-8')分别对自己使用wxpython+qrcode生的二维码和 http://tool.anzhuoxiazai.com/在线工具生成的二位码进行解析。没有问题。