树莓派(Debian系统)通过usb摄像头扫描识别QR二维码

来源:互联网 发布:淘宝店招牌图片素材 编辑:程序博客网 时间:2024/06/05 18:28

树莓派(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在CODE上查看代码片派生到我的代码片
  1. #!/usr/bin/env python  
  2. #-*- coding: UTF-8 -*-  
  3. ''''' 
  4.  
  5. 创建和读取 QR-Codes 
  6. '''  
  7. import os, signal, subprocess  
  8.   
  9. strfile1 = "qrcode"  
  10.   
  11. def erzeugen():  
  12.     text=raw_input(u"输入文本QRCode在: ")  
  13.     os.system("qrencode -o "+strfile1+".png '"+text+"'")  
  14.     print u"QRCode 在: "+strfile1+".png"  
  15.       
  16. def lesen():  
  17.     zbarcam=subprocess.Popen("zbarcam --raw --nodisplay /dev/video0", stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid)  
  18.     print u"zbarcam 成功启动..."  
  19.     i=0  
  20.     while i<5:  
  21. ##    while True:  
  22.         qrcodetext=zbarcam.stdout.readline()  
  23.         if qrcodetext!="":  
  24.             print qrcodetext  
  25.             i=i+1  
  26. ##            print u"成功"  
  27. ##            break  
  28.           
  29.     os.killpg(zbarcam.pid, signal.SIGTERM)  # 关闭进程  
  30.     print u"zbarcam 成功停止"  
  31.     return u"QRCode:  "+qrcodetext  

再创建主程序main.py:

[python] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #!/usr/bin/env python  
  2. #-*- coding: UTF-8 -*-  
  3. ''''' 
  4. 主程序 
  5. '''  
  6.   
  7. import qrcode  
  8.   
  9. while (True):  
  10.     print u"1: qrcode 创建"  
  11.     print u"2: qrcode 识别"  
  12.     print u"3: 退出"  
  13.     select=int(raw_input(u"请选择: "))  
  14.     if select == 1:  
  15.         qrcode.erzeugen()  
  16.     elif select == 2:  
  17.         result=qrcode.lesen().strip()  
  18.         print result  
  19.     elif select == 3:  
  20.         print u"完成程序..."  
  21.         break  

以上测试通过,能够正常创建和识别QR二维码及普通一维码。
另外,从https://pypi.python.org/pypi/zbar/可以下载zbar 0.10包,里面自带有对QR code识别的Sample,不过,我在树莓派Python 2.7.3的环境下是不能正常运行的。

1 0
原创粉丝点击