树莓派(Debian系统)通过usb摄像头扫描识别QR二维码
2015-07-22 23:20
666 查看
树莓派(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文件:
[python] view
plaincopy
#!/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:
[python] view
plaincopy
#!/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的环境下是不能正常运行的。
首先,需要在树莓派上安装如下工具:
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文件:
[python] view
plaincopy
#!/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:
[python] view
plaincopy
#!/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的环境下是不能正常运行的。
相关文章推荐
- J - Erasing and Winning UVA 11491
- 安卓基础之按比例布局layout_weight和weightSum
- C++构造函数
- mysql1175错误
- java的值传递与引用传递试验
- C++面向对象编程
- OpenGL Shader in OpenCASCADE
- 变量一般不要定义在.h文件中
- java基础之产生随机数
- Tomcat使用技巧最新总结
- JavaScript之判断用户登录信息
- Backbone之旅——Model篇
- 探索MySQL高可用架构之MHA(9)
- 黑马程序员——第八天(String类)
- 分布式 ZooKeeper 缓存用法实例教程
- java基础之自动装箱和拆箱
- 从C语言开始搞iOS开发
- NSSearchPathForDirectoriesInDomains方法使用
- 数据库 将多条SQL语句查询的内容整合成为一个元组
- 43. Multiply Strings