python實現樹莓派生成並識別二維碼


python實現樹莓派生成並識別二維碼

參考來源:http://blog.csdn.net/Burgess_Liu/article/details/40397803

設備及環境

  • 樹莓派2代
  • 官方系統Raspbian
  • 官方樹莓派攝像頭模塊

設備連接

攝像頭模塊插入到距離網卡口最近的那個接口,板上有Camera的字樣,看清楚正反面。

啟用攝像頭

  1. sudo raspi-config
  2. 選項:Camera
  3. 選項:Enable
  4. 選項:Finish
  5. 選項:Reboot

關鍵代碼

安裝依賴環境:

sudo apt-get install python-imaging
sudo apt-get install zbar-tools
sudo apt-get install qrencode
sudo apt-get install python-pygame

python代碼:

qrcode.py

#!/usr/bin/env python
#-*- coding: UTF-8 -*-
import os, signal, subprocess

strfile1 = "qrcode"

def erzeugen():
    text=raw_input(u"enter text QRCode: ")
    os.system("qrencode -o "+strfile1+".png '"+text+"'")
    print u"QRCode in: "+strfile1+".png"
    
def lesen():
    os.system("raspistill -w 320 -h 240 -o /home/pi/cameraqr/image.jpg")
    print u"raspistill finished"
    #zbarcam=subprocess.Popen("zbarcam --raw --nodisplay /dev/video0", stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid)
    #print u"zbarcam started successfully..."
    #qrcodetext=zbarcam.stdout.readline()
    zbarcam=subprocess.Popen("zbarimg --raw /home/pi/cameraqr/image.jpg", stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid)
    qrcodetext=zbarcam.stdout.readline()
    if qrcodetext!="":
        print qrcodetext
    else:
        print u"qrcodetext is empty"
        
    #os.killpg(zbarcam.pid, signal.SIGTERM)
    print u"zbarcam stopped successfully..."
    return u"QRCode:  "+qrcodetext

main.py

#!/usr/bin/env python
#-*- coding: UTF-8 -*-

import qrcode

while (True):
    print u"1: qrcode create"
    print u"2: qrcode identify"
    print u"3: exit"
    select=int(raw_input(u"please choose: "))
    if select == 1:
        qrcode.erzeugen()
    elif select == 2:
        result=qrcode.lesen().strip()
        print result
    elif select == 3:
        print u"programme completed..."
        break

使用方法

選項1:生成文本二維碼,輸入文本回車即可,路徑在當前路徑,文件名: qrcode.png

選項2:識別二維碼:攝像頭對准二維碼即可(無法識別的提示,請擺准攝像頭再次運行選項2)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM