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

yipeiwu_com5年前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装饰器实例大详解

一.作用域 在python中,作用域分为两种:全局作用域和局部作用域。  全局作用域是定义在文件级别的变量,函数名。而局部作用域,则是定义函数内部。  关于作用域,我们要理解两点:   ...

Python解决走迷宫问题算法示例

本文实例讲述了Python解决走迷宫问题算法。分享给大家供大家参考,具体如下: 问题: 输入n * m 的二维数组 表示一个迷宫 数字0表示障碍 1表示能通行 移动到相邻单元格用1步 思...

python3 unicode列表转换为中文的实例

python3 unicode列表转换为中文的实例

查了很多很多的资料无果,果然知乎牛逼,完美解决。 爬取网站时,最终得到list内容,编码为unicode,想让其转换为汉字并输出。 需要提取的为下图中unicode部分: 保存为列表...

python自动化测试无法启动谷歌浏览器问题

python自动化测试无法启动谷歌浏览器问题

前言 大家在使用python做web端自动化时会出现各种各样的问题,下面我会告诉大家selenium无法启动浏览器的问题 检查是否安装selenium成功 我们可以通过查看seleniu...

python3.7.0的安装步骤

python3.7.0的安装步骤

如何安装Python的操作步骤: 1.第一步先去python的官方网站下载python的安装包 地址: https://www.python.org/downloads/ 根据自己的系...