查看: 12429|回复: 1
收起左侧

在树莓派上通过usb摄像头扫描识别QR code (二维码)

某树莓派玩家  发表于 2015-6-3 22:50:43 |阅读模式
树莓派自带Python开发环境IDLE(Python 2.7.3),接上摄像头,就能通过Python实行对QR code的创建和识别:

本文所使用系统为raspbian,基于debian为raspberry pi 优化的。

首先,需要在树莓派上安装如下工具:

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文件:

sudo nano 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二维码及普通一维码。
回复

使用道具

2018-4-2 18:53:17 | 显示全部楼层
这用不了啊,识别根本没成功过啊
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注我们,了解更多

官方微信

服务时间:10:00-16:00

13714503811

公司地址:深圳市龙岗区南湾街道东门头路8号

Copyright © 2012-2020 Powered by 树莓派论坛 2019.4  粤ICP备15075382号-1
快速回复 返回列表 返回顶部