This tutorial will teach you how to scan QR codes using the Raspberry Pi module and the official Raspberry Pi camera. To attach the camera to the Raspberry pi, we use a dedicated header is available onboard. You can also use a USB camera with Raspberry Pi. The disadvantage is USB interface is slow. Also, the Raspberry Pi USB port version is 2.0, which is slow compared to 3.0. A few extra settings and the configuration are also required to tell Raspberry that a USB camera is attached with its USB port. The official camera is easy to go with, and all support files are already installed.
Before beginning, I will assume you have already connected the camera with Pi.
First, we must enable the camera in the Raspberry config file. The easy way is to do it graphically. Go to main menu>Preferences>Raspberry Pi Configuration next window, then go to interfaces against camera select the enable button.
Now it’s time to update and install the camera packages necessary to make the camera live. Picamera is a lightweight package. It offers many functions, and above all, it’s easy to play with. I installed this package. Open the command prompt from Raspberry pi and type the following commands.
$ sudo apt-get update
$ sudo apt-get install python-picamera python3-picamera
Note: Both versions of Python 2 and 3 packages will be installed at once with the above command. It depends on you what version of python you are working with. I am working with python 3.
Another package required is zbarlight. This package decodes the QR code present in the image. To installed zbarlight, type the following commands in the command prompt.
$ apt-get install libzbar0 libzbar-dev
$ pip3 install zbarlight
Image processing package named PIL is preinstalled in the Raspberry pi. In case it is not present, you can install it with
pip install –no-index -f https://dist.plone.org/thirdparty/ -U PIL
In the code, first imported the packages at the top.
- PiCamera package to work with Raspberry Pi camera
- Time>sleep for delay
- PIL for image processing
- Zbarlight to decode and find the QR code present in the processed image.
Next, an instance of PiCamera is created named camera. Camera.start_preview() opens the camera feed, and you can see the camera view on an LCD screen to which Raspberry pi is connected. Sleep(5) control remains in sleep for 5 seconds. You can see the camera feed in these 5 seconds. Camera.capture() captures the image and store it at the location specified in the function arguments. Camera.stop_preview() ends the camera feed means no camera display on the screen.
Now starts the image decode and QR code extraction part. A variable with the name file_path assigned path to the image. With open statement opens the image as an image file. ‘rb’ means the file under consideration is not a text file. Image variable is holding the file, which is opened by PIL(Image.open(image_file)) image processing library. Image.load() loads the image. Image vectors are now present in the image variable. Vectors are then passed to zbarlight to extract the QR code from vectors. Extracted QR code is stored in the codes variable and at the end print statement prints the extracted text on the console.
Testing
I generated a QR code using an online website on my mobile. QR input text is on the left-hand side, and the encoded QR image is on the right-hand side.
I executed the code on Raspberry pi and placed the mobile in front of the camera (You can see the video on the following link https://www.facebook.com/electronicsgru/videos/548460509194441).
Decoded message is
I used Thonny default python ide in Raspberry Pi. You can run the python file from the command prompt. I suggest using any python ide. Ide will let you know any errors and type in case if some error arises.
Let’s Diy this project: Where to purchase parts?
Raspberry Pi (Mouser Electronics)
Raspberry pPi camera (Mouser Electronics)
You may also like:
Filed Under: Electronic Projects, Raspberry pi
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.