We’re going to see today how to scan Qr code using OpenCV.
Below you can see the code read the Qr code from an image or a real-time video.
For this project, we will obviously need our OpenCV library then NumPy and pyzbar. If you need to install pyzbar, I recommend that you go directly to the project page to follow the instructions.
Scanning Qr Code read from an image
Let’s start by importing all the libraries into our python file. As you can see from the code below and in my first example I imported an image.
import cv2
import numpy as np
import pyzbar.pyzbar as pyzbar
image = cv2.imread("pysource_qrcode.png")
This is the qr code I used

Now I simply take the data with processing through the library and print the results.
decodedObjects = pyzbar.decode(image)
for obj in decodedObjects:
print("Type:", obj.type)
print("Data: ", obj.data, "\n")
We just did Scanning Qr Code – Opencv with Python for the single image.
Scanning Qr Code – Opencv with Python from webcam
For Scanning Qr Code – Opencv with Python with the webcam the procedure is similar to that used for the single image. Instead of the image path we use the function cv2.VideoCapture (0).
As we already know the video stream of the image, on OpenCV, is recognized as a sequence of images, so it will be necessary to insert the processing inside a While loop. In the code below everything will be clearer.
cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_PLAIN
while True:
_, frame = cap.read()
decodedObjects = pyzbar.decode(frame)
for obj in decodedObjects:
#print("Data", obj.data)
cv2.putText(frame, str(obj.data), (50, 50), font, 2,
(255, 0, 0), 3)
Conclusion
In this article I explained, in a very simple way, how to decode a qr code with opencv and python. You can download the complete and immediately usable code from the download link.

Hi there, I’m the founder of Pysource.
I’m a Computer Vision Consultant, developer and Course instructor.
I help Companies and Developers to build efficient computer vision software.
what is the purpose of each of these programs?
Hi, wich version of python are you using?
How can i connect the IP Camera?
Simply change that line: cap = cv2.VideoCapture(0) to e.g
cap = cv2.VideoCapture(‘rtsp://192.168.1.64/1’).
How to Stop scanning when QR found
Simply you can add break on the loop of the objects detected.
for obj in decodedObjects:
print(“Type:”, obj.type)
print(“Data: “, obj.data, “\n”)
break
sir please provides modules also like cv2 because when i am going to run this program it throws error of no cv2 module found so give me solution on this
how did cv2, it is not available now.
You need to install the modeule first,
pip install opencv-python
ERROR: Could not find a version that satisfies the requirement opencv-python (from versions: none)
ERROR: No matching distribution found for opencv-pythoh
I use latest version (python-3.8.0)
upgrade your pip version
Hi. How can i stop scanning with cammera. I tried break but it fails. Also would like to open scaned qr internet link, but as i said i cant stop the loop of scaning. Help. Please.
Ctrl + C on the cmd portion
decodedObjects = pyzbar.decode(image)
File “C:\Users\roman\AppData\Roaming\Python\Python35\site-packages\pyzbar\pyzbar.py”, line 181, in decode
pixels, width, height = _pixel_data(image)
File “C:\Users\roman\AppData\Roaming\Python\Python35\site-packages\pyzbar\pyzbar.py”, line 147, in _pixel_data
pixels, width, height = image
TypeError: ‘NoneType’ object is not iterable
I have a mistake , when try to scan
Hello,
numpy package is not used in this code.
how can i make a full frame of webcam code
I am unable to use this code in Django project why.camera was not opening
Hello, I’ve tried to found the answer somewhere else with no success. How can you resize the frame, mine is to high & too large.
Thanks
You need to install it using pip install opencv-python
FileNotFoundError: Could not find module ‘C:\Users\badri\PycharmProjects\pb1\venv\lib\site-packages\pyzbar\libzbar-64.dll’ (or one of its dependencies). Try using the full path with constructor syntax.
anyone know’s how to slove this
Did you fix it?, I have same problem now.