zbar解码二维码和条形码示例

yipeiwu_com6年前Python基础

复制代码 代码如下:

#!/usr/bin/env python
# coding: u8
import os
import zbar
import Image
import urllib
import uuid
def qrRead(url):

uuid1 = uuid.uuid1()
filename=str(uuid1)+".jpg"
print uuid1
urllib.urlretrieve(url, filename)

# create a reader
scanner = zbar.ImageScanner()

# configure the reader
scanner.parse_config('enable')

# obtain image data
pil = Image.open(filename).convert('L')
width, height = pil.size
#pil.show()
raw = pil.tostring()

# wrap image data
image = zbar.Image(width, height, 'Y800', raw)

# scan the image for barcodes
scanner.scan(image)

tmpdata=''
# extract results
for symbol in image:
# do something useful with results
print symbol.type, '图片内容为:\n%s' % symbol.data
tmpdata=tmpdata+symbol.data

 
# clean up
del(image)
os.remove(filename)
return tmpdata
if __name__ == '__main__':
url = '//www.jb51.net' 
qrRead(url)


要安装 python-zbar 

检查启用了 universe 存储库。
检查 /etc/apt/sources.list 与 sudo,以确保您具有正确的权限使用您最喜爱的编辑器。
 
复制代码 代码如下:

sudo gedit /etc/apt/sources.list
 

确保包含 universe。

在发生任何更改后,您应该运行此命令以更新您的系统。
复制代码 代码如下:

sudo apt-get update

你现在可以安装这样的包。

安装 python-zbar
复制代码 代码如下:

sudo apt-get install python-zbar

这将安装 python-zbar 和它所依赖的任何其他包。

相关文章

python利用百度AI实现文字识别功能

python利用百度AI实现文字识别功能

本文为大家分享了python实现文字识别功能大全,供大家参考,具体内容如下 1.通用文字识别 # -*- coding: UTF-8 -*- from aip import AipO...

python定时复制远程文件夹中所有文件

本文实例为大家分享了python定时复制远程文件夹中文件的具体代码,供大家参考,具体内容如下 import os, shutil, sys import threading impo...

Python中类的定义、继承及使用对象实例详解

本文实例讲述了Python中类的定义、继承及使用对象的方法。分享给大家供大家参考。具体分析如下: Python编程中类的概念可以比作是某种类型集合的描述,如“人类”可以被看作一个类,然后...

使用python调用zxing库生成二维码图片详解

使用python调用zxing库生成二维码图片详解

(1)安装Jpype 用Python调用jar包需要安装jpype扩展,在Ubuntu上可以直接使用apt-get安装jpype扩展 $ sudo apt-get install...

flask框架中勾子函数的使用详解

在客户端和服务器交互的过程中,有些准备工作或扫尾工作需要处理,比如: 在请求开始时,建立数据库连接; 在请求开始时,根据需求进行权限校验; 在请求结束时,指定数据的交互格式...