Raspberry Pi recognition QR code

 

https://amazefcc233.com/archives/148/

ready

In the content given in this tutorial, you need to install the following tools:

    • sudo apt-get install python-imaging
    • sudo apt-get install qrencode
    • sudo apt-get install python-pygame
      If you want to recognize the QR code without the need to support Chinese, then you can use the one-key command:
    • sudo apt-get install zbar-tools
      If you want to support Chinese, then ... please follow the instructions in the tutorial to operate.

 

main.py

 

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

import qrcode

while (True):
    print ("1: qrcode created")
    print ("2: qrcode recognition")
    print (u "3: Exit")
    select = int (input (u "Please select:"))
    if select == 1:
        qrcode.generate ()
    elif select == 2:
        result=qrcode.lesen().strip()
        print (result)
    elif select == 3:
        print (u "Complete the program ...")
        break

  qrcode.py

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

strfile1 = "qrcode"

create def ():
    text=input(u"enter text QRCode: ")
    os.system("qrencode -o "+strfile1+".png '"+text+"'")
    print (u"QRCode in: "+strfile1+".png")

def read ():
    os.system("raspistill -t 1000 -w 320 -h 240 -o /home/pi/Desktop/cam/qrcode/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/Desktop/cam/qrcode/image.jpg", stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid)
    qrcodetext=zbarcam.stdout.readline().decode()
    if qrcodetext != "":
        #print (qrcodetext)
        pass
    else:
        print (u "qrcodetext is empty")

    # os.killpg (zbarcam.pid, signal.SIGTERM)
    print (u"zbarcam stopped successfully...")
    qrresult = f"QRCode: {qrcodetext}"
    return (qrresult)

  

No need to support recognition of Chinese QR code

As long as all the above preparations are successfully completed, then you can run the code. It stands to reason that there should be no problems.

 

 

Need to support recognition of Chinese QR code

Please think again before reading the following: Do I really need to support Chinese?
......
......
......
If the answer is still yes, then please read on. Otherwise, please use a convenient and fast way that does not need to support Chinese.
Because this part of the content will involve some content related to compilation and installation.

Install the front library

First, please run the following command to install the tool library

 
wget http://www.imagemagick.org/download/delegates/jpegsrc.v9b.tar.gz
tar -xzvf jpegsrc.v9b.tar.gz
cd jpeg.9b
./configure
make
sudo make install

  Then, it is time to install Imagemagick.

wget https://www.imagemagick.org/download/ImageMagick.tar.gz
# The folder name may be different. Please handle it flexibly.
cd ImageMagick-7.0.8-68
./configure 
make
sudo make install
sudo ldconfig /usr/local/lib
# Check if the installation is successful
convert -version

  And, we also need to configure a wave of soft links:

sudo ln -s /usr/local/include/ImageMagick-7 /usr/local/include/ImageMagick
sudo ln -s /usr/local/include/ImageMagick-7/MagickWand /usr/local/include/wand

  

Compile and install zbar

First, download a copy of the zbar source code and prepare the compiler:

sudo apt-get install python-gtk2-dev
sudo apt-get install libqt4-dev
wget http://downloads.sourceforge.net/project/zbar/zbar/0.10/zbar-0.10.tar.gz
tar -zxvf zbar-0.10.tar.gz

  Then, we need to edit the code so that it can support Chinese.

# Enter the directory
cd zbar.0.10 / zbar / qrcode /
vim qrdectxt.c

  Find the code shown in the picture (about line 64), and replace the code in the circled part of the red frame
(the content of the replacement is the Japanese code. If you want to recognize Japanese, please replace the other code, but do not replace utf- 8)

 

 

 

 Find the code as shown in the picture in the same file, and arrange the coding order
(It is recommended that Chinese be placed first)
snipaste_20191019_141214.png

Save and exit.
Then, you can start the compilation work!

# First return to your own directory (the directory is just an example, it may be different for different people)
cd /home/pi/Desktop/build/zbar-0.10
# The following code will ** not ** use the zbarcam command after installation. If you want to use zbarcam, please remove the --disable-video command and install the relevant libraries yourself
./configure --disable-video -without-qt -without-gtk -without-x -with-jpeg
make
sudo make install

  

 

 

Guess you like

Origin www.cnblogs.com/kekeoutlook/p/12723837.html