Bird
0
0
Raspberry Piprogramming~5 mins

QR code reading in Raspberry Pi

Choose your learning style9 modes available
Introduction

QR code reading helps your Raspberry Pi understand information stored in QR codes quickly and easily.

You want to scan product information using a camera connected to your Raspberry Pi.
You need to read QR codes for a simple check-in system at an event.
You want to automate reading URLs or text from QR codes for a project.
You are building a Raspberry Pi robot that reads QR codes to navigate.
You want to decode QR codes from images stored on your Raspberry Pi.
Syntax
Raspberry Pi
import cv2
from pyzbar import pyzbar

# Load image or capture from camera
image = cv2.imread('qrcode.png')

# Decode QR codes
codes = pyzbar.decode(image)

for code in codes:
    print(code.data.decode('utf-8'))

Use pyzbar library to decode QR codes easily.

cv2.imread() loads an image file; you can also capture from a camera.

Examples
This reads and prints QR code data from a saved image.
Raspberry Pi
import cv2
from pyzbar import pyzbar

# Read QR code from image file
image = cv2.imread('mycode.png')
codes = pyzbar.decode(image)
for code in codes:
    print(code.data.decode('utf-8'))
This captures one frame from the camera and reads QR codes from it.
Raspberry Pi
import cv2
from pyzbar import pyzbar

# Capture from Raspberry Pi camera
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
if ret:
    codes = pyzbar.decode(frame)
    for code in codes:
        print(code.data.decode('utf-8'))
cap.release()
Sample Program

This program loads a QR code image named 'example_qr.png' and prints the text inside the QR code.

Raspberry Pi
import cv2
from pyzbar import pyzbar

# Load an example QR code image
image = cv2.imread('example_qr.png')

# Decode QR codes in the image
codes = pyzbar.decode(image)

# Print all decoded QR code data
for code in codes:
    print(f"QR Code data: {code.data.decode('utf-8')}")
OutputSuccess
Important Notes

Make sure to install pyzbar and opencv-python packages before running.

Camera access may require permissions and proper setup on Raspberry Pi.

QR code images should be clear and well-lit for best results.

Summary

QR code reading lets your Raspberry Pi understand data from QR codes using a camera or images.

Use pyzbar with opencv to decode QR codes easily.

Works for images and live camera captures.