Detect and Decode a QRCode Image Using cv2.QRCodeDetector() in Python OpenCV

In this tutorial, we will introduce how to detect and decode a qrcode image in python opencv, we will use cv2.QRCodeDetector() to implement it.

Best Practice to Detect and Decode a QRCode Image Using cv2.QRCodeDetector() in Python OpenCV

1.Read an qrcode image

import cv2

image = cv2.imread('C:/Users/N/Desktop/testQRCode.png')

2.Create qrCodeDetector using cv2.QRCodeDetector()

qrCodeDetector = cv2.QRCodeDetector()

3.Detect and decode qrcode image

decodedText, points, _ = qrCodeDetector.detectAndDecode(image)

if points is not None:
    nrOfPoints = len(points)
    for i in range(nrOfPoints):
        nextPointIndex = (i+1) % nrOfPoints
        cv2.line(image, tuple(points[i][0]), tuple(points[nextPointIndex][0]), (255,0,0), 5)

    print(decodedText)    
    cv2.imshow("Image", image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
else:
    print("QR code not detected")

Here decodedText is the content in qrcode image.

Detect and Decode a QRCode Image Using cv2.QRCodeDetector() in Python OpenCV