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猫交流学习群 里的 M 同学提了个问题。这个问题挺有意思,经初次讨论,我们认为它无解。 然而,我认为它很有价值,应该继续思考怎么解决,所以就在私密的知识星球上记录...

python有证书的加密解密实现方法

本文实例讲述了python有证书的加密解密实现方法。分享给大家供大家参考。具体实现方法如下: 最近在做python的加解密工作,同时加完密的串能在php上能解出来,网上也找了一些靠谱的资...

python 图片二值化处理(处理后为纯黑白的图片)

python 图片二值化处理(处理后为纯黑白的图片)

先随便招一张图片test.jpg做案例 然后对图片进行处理 # 图片二值化 from PIL import Image img = Image.open('test.jpg')...

tensorflow创建变量以及根据名称查找变量

环境:Ubuntu14.04,tensorflow=1.4(bazel源码安装),Anaconda python=3.6 声明变量主要有两种方法:tf.Variable和 tf.get_...

Python箱型图处理离群点的例子

Python箱型图处理离群点的例子

首先我们简单地区分一下离群点(outlier)以及异常值(anomaly): 离群点: 异常值: 个人觉着异常值和离群点是两个不同的概念,当然大家在数据预处理时对于这两个概念不做细致...