标签:des blog http os ar sp 文件 2014 on
树莓派(Debian系统)自带Python开发环境IDLE(Python 2.7.3),接上摄像头,就能通过Python实行对QR code的创建和识别:
首先,需要在树莓派上安装如下工具:
sudo apt-get install python-imaging
sudo apt-get install zbar-tools
sudo apt-get install qrencode
sudo apt-get install python-pygame
然后创建qrcode.py文件:
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
'''
创建和读取 QR-Codes
'''
import os, signal, subprocess
strfile1 = "qrcode"
def erzeugen():
text=raw_input(u"输入文本QRCode在: ")
os.system("qrencode -o "+strfile1+".png '"+text+"'")
print u"QRCode 在: "+strfile1+".png"
def lesen():
zbarcam=subprocess.Popen("zbarcam --raw --nodisplay /dev/video0", stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid)
print u"zbarcam 成功启动..."
i=0
while i<5:
## while True:
qrcodetext=zbarcam.stdout.readline()
if qrcodetext!="":
print qrcodetext
i=i+1
## print u"成功"
## break
os.killpg(zbarcam.pid, signal.SIGTERM) # 关闭进程
print u"zbarcam 成功停止"
return u"QRCode: "+qrcodetext
再创建主程序main.py:
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
'''
主程序
'''
import qrcode
while (True):
print u"1: qrcode 创建"
print u"2: qrcode 识别"
print u"3: 退出"
select=int(raw_input(u"请选择: "))
if select == 1:
qrcode.erzeugen()
elif select == 2:
result=qrcode.lesen().strip()
print result
elif select == 3:
print u"完成程序..."
break
以上测试通过,能够正常创建和识别QR二维码及普通一维码。
另外,从https://pypi.python.org/pypi/zbar/可以下载zbar 0.10包,里面自带有对QR code识别的Sample,不过,我在树莓派Python 2.7.3的环境下是不能正常运行的。
在树莓派(Debian系统)上通过usb摄像头扫描识别QR二维码
标签:des blog http os ar sp 文件 2014 on
原文地址:http://blog.csdn.net/burgess_liu/article/details/40397803