Issue with QR Code reading with OpenCV or Pyzbar

1.1k Views Asked by At

I'm trying to decode a QR Code using Python, or any other language to be honest. I am also familiar with Javascript or PHP, but Python seemed to be the most appropriate one for this task.

This is part of a bigger piece of code that I am writing for a little challenge. I need to extract a password from the QR Code. I've tried using a QR Code reader on my phone and I can get the password so I can confirm that there is no issue with the QR Code itself.

Here is the QRCode: QRCode

And the string to retrieve is "The key is /qrcod_OMevpf".

So far I've tried using two different python libraries. Open CV and Pyzbar, with the following codes:

OpenCV

    image = cv2.imread(imgAbsolutePath)
    qrCodeDetector = cv2.QRCodeDetector()
    decodedText, points, _ = qrCodeDetector.detectAndDecode(image)
    if points is not None:
    # QR Code detected handling code
        print("QR code detected")
        print(decodedText)    
    
    else:
        print("QR code not detected")

Which prints "QR code detected" and then an empty string.

Pyzbar

qr = decode(Image.open('result.png'), symbols=[ZBarSymbol.QRCODE])
print(qr)

Which prints "[]"

Do you know why these don't work or can you suggest any other libraries that works ? Thanks

2

There are 2 best solutions below

0
Emilien On

I finally got it to work using zxing :

from zxing import BarCodeReader

def decode_qr_code(image_path):
    reader = BarCodeReader()
    barcode = reader.decode(image_path)
    return barcode.parsed

qr_code = decode_qr_code("result.png")
print(qr_code)
0
neg-c On

The QR code data seems to be flipped, try this:

image = cv2.imread(imgAbsolutePath)
mirrored_image = cv2.flip(image, 1)   // mirror flip
qrCodeDetector = cv2.QRCodeDetector()
decodedText, points, _ = qrCodeDetector.detectAndDecode(mirrored_image)
if points is not None:
# QR Code detected handling code
    print("QR code detected")
    print(decodedText)    

else:
    print("QR code not detected")

NOTE: There is currently someone working on a fix on OpenCV.