Reading QR Code Using Raspberry Pi Camera

Utilize Raspberry Pi camera as a QR reader.

Introduction

A QR code (Quick Response code) is a type of matrix barcode. It is a machine-readable optical label that contains information about the item to which it is attached. The QR system became popular due to its fast readability and greater storage capacity compared to standard UPC barcodes. In this tutorial, I will share on how to use the Raspberry Pi camera as a scanner and a reader of QR code.

Video

This video shows how to scan and read QR code using Raspberry Pi camera.

Hardware Preparation

This is the list of items used in the video.

Sample Program

This is python 3 sample program to scan and read QR code using Raspberry Pi camera.

from gpiozero import LED, Button, Buzzer
import cv2
import re
led = LED(19)
sw1 = Button(21)
buzzer = Buzzer(26)
cap = cv2.VideoCapture(0)
detector = cv2.QRCodeDetector()
def sw1Pressed():
global sw1Press
sw1Press = True
sw1.when_pressed = sw1Pressed
sw1Press = False
print("Reading QR code using Raspberry Pi camera")
print("Press SW1 to scan.")
while True:
if sw1Press == True:
led.toggle()
_, img = cap.read()
data, bbox, _ = detector.detectAndDecode(img)
if bbox is not None:
for i in range(len(bbox)):
cv2.line(img, tuple(bbox[i][0]), tuple(bbox[(i+1) % len(bbox)][0]), color=(255,
0, 0), thickness=2)
cv2.putText(img, data, (int(bbox[0][0][0]), int(bbox[0][0][1]) 10), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 255, 0), 2)
if data:
sw1Press = False
buzzer.beep(0.1, 0.1, 1)
print("Data found: " + data)
led.off()
data = ""
cv2.imshow("code detector", img)
else:
cap.read()
cv2.destroyAllWindows()
if cv2.waitKey(1) == ord("q"):
break
led.off()
cap.release()
cv2.destroyAllWindows()

view raw
CameraQRScanner.py
hosted with ❤ by GitHub

Thank You

References:

Thanks for reading this tutorial. If you have any technical inquiries, please post at Cytron Technical Forum.

Please be reminded, this tutorial is prepared for you to try and learn.
You are encouraged to improve the code for better application.

2 thoughts on “Reading QR Code Using Raspberry Pi Camera”

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

BLTouch Installation for Ender 3 with 32-bit V4.2.2 Board
Pick and Send Random Meal’s Option and Locations through Telegram Bot Using Grove WiFi 8266 on micro:bit
DIY Automated Vacuum Cleaner Using REKA:BIT With Micro:bit
Rainbow Spark in Mini House using Maker Uno.
TinyML on Arduino using Edge Impulse
Tutorials of Cytron Technologies Scroll to Top