0
0
Iot-protocolsHow-ToBeginner · 4 min read

Barcode Scanning with Raspberry Pi Camera: Simple Guide

Use the Raspberry Pi camera with Python libraries like OpenCV and pyzbar to capture images and decode barcodes. Capture frames from the camera, then process them with pyzbar.decode() to read barcode data in real time.
📐

Syntax

To scan barcodes with a Raspberry Pi camera, you typically use Python code that:

  • Captures video frames from the camera using cv2.VideoCapture().
  • Processes each frame to detect barcodes using pyzbar.decode().
  • Extracts barcode data and displays or uses it.

Key functions:

  • cv2.VideoCapture(0): Opens the camera.
  • pyzbar.decode(image): Detects barcodes in the image.
  • cv2.imshow(): Shows the camera feed with barcode info.
python
import cv2
from pyzbar import pyzbar

# Open the Raspberry Pi camera
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    if not ret:
        break

    # Decode barcodes in the frame
    barcodes = pyzbar.decode(frame)

    for barcode in barcodes:
        x, y , w, h = barcode.rect
        # Draw rectangle around barcode
        cv2.rectangle(frame, (x,y), (x+w, y+h), (0,255,0), 2)

        barcode_data = barcode.data.decode('utf-8')
        barcode_type = barcode.type

        # Put barcode data text on frame
        cv2.putText(frame, f'{barcode_data} ({barcode_type})', (x, y - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)

    cv2.imshow('Barcode Scanner', frame)

    # Exit on pressing 'q'
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
💻

Example

This example shows how to open the Raspberry Pi camera, scan barcodes live, and display the barcode data on the video feed window. It uses OpenCV to capture video and pyzbar to decode barcodes.

python
import cv2
from pyzbar import pyzbar

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    if not ret:
        print('Failed to grab frame')
        break

    barcodes = pyzbar.decode(frame)

    for barcode in barcodes:
        x, y, w, h = barcode.rect
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

        barcode_data = barcode.data.decode('utf-8')
        barcode_type = barcode.type

        cv2.putText(frame, f'{barcode_data} ({barcode_type})', (x, y - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

    cv2.imshow('Barcode Scanner', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
Output
A window opens showing live camera feed with green rectangles around detected barcodes and their decoded data displayed above them. Press 'q' to quit.
⚠️

Common Pitfalls

  • Camera not opening: Make sure the Raspberry Pi camera is enabled in raspi-config and connected properly.
  • Missing libraries: Install opencv-python and pyzbar using pip3 install opencv-python pyzbar.
  • Incorrect camera index: If cv2.VideoCapture(0) doesn't work, try 1 or check device with v4l2-ctl --list-devices.
  • Lighting and focus: Poor lighting or blurry images reduce barcode detection accuracy.
  • Not decoding data: Remember to decode bytes to string with barcode.data.decode('utf-8').
python
## Wrong way (forgetting to decode bytes):
# barcode_data = barcode.data

## Right way:
barcode_data = barcode.data.decode('utf-8')
📊

Quick Reference

  • Enable Raspberry Pi camera with sudo raspi-config.
  • Install dependencies: pip3 install opencv-python pyzbar.
  • Use cv2.VideoCapture(0) to access camera.
  • Decode barcodes with pyzbar.decode(frame).
  • Display results with cv2.imshow().
  • Press 'q' to quit the program.

Key Takeaways

Use Python with OpenCV and pyzbar to scan barcodes from Raspberry Pi camera frames.
Enable the camera in Raspberry Pi settings before running the code.
Decode barcode bytes to strings using .decode('utf-8') to read data correctly.
Ensure good lighting and camera focus for reliable barcode detection.
Press 'q' to exit the live barcode scanning window safely.