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中常见的数据预处理方法,以下通过sklearn的preprocessing模块来介绍; 1. 标准化(Standardization or Mean R...

python中遍历文件的3个方法

今天写一个在windows下批量修改文件名的python脚本,用到文件的遍历。用python进行文件遍历有多种方法,这里列举并说明一下。 os.path.walk() 这是一个传统的用法...

Python中列表和元组的使用方法和区别详解

一、二者区别 列表: 1.可以增加列表内容 append 2.可以统计某个列表段在整个列表中出现的次数 count 3.可以插入一个字符串,并把整个字符串的每个字母拆分当作一个列...

关于Pycharm无法debug问题的总结

问题描述:在Pycharm中写python时可以运行程序却突然不能debug。出现debug提示——pydev debugger: process XXXX is connec...

python监控网卡流量并使用graphite绘图的示例

复制代码 代码如下:#!/usr/bin/env pythonimport sys,timefrom socket import socketdef read_interface(in_...