QR Code Scanner (Python)
A real-time QR code scanner that uses your webcam to detect and read QR codes. When a code is found, it's printed to the console and highlighted with a green outline on the live video feed.
How It Works
Opens your default webcam using OpenCV. Continuously captures frames and scans each one for QR codes using pyzbar. When a QR code is detected:
Its decoded data is printed to the console. A green polygon is drawn around it on the video window.
Press q at any time to quit and close the webcam.
Project Structure
Put everything in a single folder, for example:
qr-scanner/ ├── qr_scanner.py # the script you shared ├── README.md # this file └── requirements.txt # dependency list (see below)
There's no need for extra folders or asset files — this script is self-contained and only needs the webcam.
Requirements
Python 3.7+ A working webcam The following Python packages:
opencv-python numpy pyzbar
System dependency (important)
pyzbar relies on the ZBar shared library, which isn't installed automatically by pip on all systems:
Windows: Usually works out of the box with pip install pyzbar (bundles the DLL). macOS: brew install zbar Linux (Debian/Ubuntu): sudo apt-get install libzbar0
Installation
bashpip install opencv-python numpy pyzbar
Or, using a requirements.txt:
opencv-python numpy pyzbar
bashpip install -r requirements.txt
Running
bashpython qr_scanner.py
A window titled "QR Scanner" will open showing your webcam feed. Point a QR code at the camera — when it's recognized, its content is printed in the terminal and outlined in green on screen. Press q to exit.
Sample Output
📸 QR Scanner Started. Press 'q' to quit. ✅ QR Code Found: https://example.com ✅ QR Code Found: https://example.com
(Note: the same URL is printed repeatedly on every frame while the code stays in view — see "Known Limitations" below.)
Known Limitations
Repeated console prints. As long as a QR code stays in frame, its data is printed on every loop iteration (every frame), which can flood the console. Consider tracking already-seen codes and only printing new ones. No error handling for missing webcam / missing ZBar library. If the camera index 0 doesn't exist, or ZBar isn't installed on the system, the script will fail with a raw exception instead of a clean message. Fixed camera index. cv2.VideoCapture(0) always uses the first detected camera; if you have multiple cameras, you may need to change the index (1, 2, etc.). No image saving/logging. Detected codes are only printed to the console, not saved to a file.
Possible Improvements
De-duplicate detections so each unique code is only printed once (or once per session). Save scanned results with timestamps to a log file (e.g. CSV). Add a try/except around webcam initialization for a friendlier error message if no camera is found. Support scanning from an image file or a video file, not just a live webcam feed. Add a bounding box label showing the decoded text directly on the video frame, not just the outline.
License
Feel free to use, modify, and share this project for learning purposes.