Skip to content

Instantly share code, notes, and snippets.

Last active August 1, 2023 15:04
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Scan barcodes and QR codes using Raspberry Pi Zero W and Raspberry Pi Camera V3
- Raspberry Pi Zero W
- Raspberry Pi Camera Module 3 - 12MP with Auto Focus lens
- Raspberry Pi Zero/W/WH 15cm Camera Cable
Increase SWAP size to 2048
sudo apt-get update
sudo apt-get install python3-opencv
sudo apt-get install libqt5gui5 libqt5test5 python3-sip python3-pyqt5 libjasper-dev libatlas-base-dev -y
pip3 install opencv-contrib-python==
sudo modprobe bcm2835-v4l2
pip3 install pyzbar
sudo reboot
import cv2
from pyzbar.pyzbar import decode
from picamera2 import MappedArray, Picamera2, Preview
from libcamera import controls
from libcamera import Transform
colour = (0, 255, 0)
scale = 1
thickness = 2
def draw_barcodes(request):
with MappedArray(request, "main") as m:
for b in barcodes:
if b.polygon:
x = min([p.x for p in b.polygon])
y = min([p.y for p in b.polygon]) - 30
cv2.putText(m.array,'utf-8'), (x, y), font, scale, colour, thickness)
picam2 = Picamera2()
config = picam2.create_preview_configuration(main={"size": (640, 480)}, transform=Transform(hflip=True, vflip=True))
barcodes = []
picam2.post_callback = draw_barcodes
picam2.set_controls({"AfMode": controls.AfModeEnum.Manual, "LensPosition": 8.0})
while True:
rgb = picam2.capture_array("main")
barcodes = decode(rgb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment