Detect and Decode QR Code in Image using OpenCV

Detect and Decode QR Code in Image using OpenCV

QR code is a two-dimensional barcode which stores encoded data. It can be a website URL, contact details, location coordinates, email address, plain text, etc. QR code can store more data than a linear barcode of equal size.

This tutorial provides an example how to detect and decode a QR code in an image using OpenCV.

We create an object of class QRCodeDetector. QR code is detected and decoded by using the detectAndDecode method. It allows getting decoded data and an array of vertices of the found QR code.

Python
C++
Java
import cv2 img = cv2.imread('test.jpg') decoder = cv2.QRCodeDetector() data, points, _ = decoder.detectAndDecode(img) if points is not None: print('Decoded data: ' + data) points = points[0] for i in range(len(points)): pt1 = [int(val) for val in points[i]] pt2 = [int(val) for val in points[(i + 1) % 4]] cv2.line(img, pt1, pt2, color=(255, 0, 0), thickness=3) cv2.imshow('Detected QR code', img) cv2.waitKey(0) cv2.destroyAllWindows()

If a QR code was found, we print the decoded data and draw a bounding box around the detected QR Code.

Decoded data: https://lindevs.com
Detected QR Code

The 2 Comments Found

  1. Avatar
    johnofleek Reply

    Brilliant - many thanks
    I tried a few others peoples attempts that didn't look quite right / work.
    Saved me reading the cv2 manual :)

Leave a Comment

Cancel reply

Your email address will not be published.