0

I've generated a QR code and want to decode it by Opencv Android SDK 4.6.0.

The QR data is just a JSON string, generated by JS: {"url":"rdp://52.170.146.204","mX":480,"mY":410,"mIn":true,"o":50,"w":2250,"h":1349,"s":300,"id":"ef4ac969"}

enter image description here

When I try to decode it by python (same CV version 4.6.0) everything works well:

def read_qr_code(filename):
    try:
        img = cv2.imread(filename)
        detect = cv2.QRCodeDetector()
        value, points, straight_qrcode = detect.detectAndDecode(img)
        return value
    except:
        print("Error loading image")
        return

print("cv version:" + cv2.__version__)
value = read_qr_code("C:\qr\screenshot.jpg")
print("QR value:" + value)

cv version:4.6.0 QR value: "url":"rdp://52.170.146.204","mX":480,"mY":410,"mIn":true,"o":50,"w":2250,"h":1349,"s":300,"id":"ef4ac969"}

When I try to convert the same QR using OpenCv Android SDK, it's not detected:

int read_qr(string filename)
{
    string image_path = filename;

    Mat getImage = imread(image_path);
    if(getImage.empty())
    {
        cout << "Could not read the image: " << image_path << endl;
        return -1;
    }

    QRCodeDetector qrDet = QRCodeDetector();

    Mat points, rectImage;
    std::string data = qrDet.detectAndDecode(getImage, points, rectImage);
    if (data.length() > 0)
    {
        cout << "Data after decoding: " << data << endl;
    }
    else
    {
        cout << "QR Code not detected" << endl;
        return -1;
    }

    return 0;
}

QR Code not detected

I didn't find any information about the problem, and it doesn't seem like there should be a difference between the python and c++ implementations

When I change the values in the Json a bit, it seems that sometimes it works. I didn't find a certain pattern like a length or specific character, but when I change the QR sometimes it works.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • Unrelated: You can use `filename` directly, there is no need for `image_path` to be created here. Along the same lines, you don't use `points` and `rectImage`, you can just remove them (these are optional parameters) – Fareanor Nov 07 '22 at 10:16
  • are you _sure_ you are using the exact same image file that contains the QR code? no cropping, no scaling, no "screenshot", no other shenanigans? – Christoph Rackwitz Nov 07 '22 at 11:07
  • Yes. it's exactly the same file. – Netanel Hadad Nov 07 '22 at 12:11
  • the data in `getImage`, please check some basic properties like size, type, number of channels, ... does that match what you see on the python side? – Christoph Rackwitz Nov 07 '22 at 12:53
  • @ChristophRackwitz Python: height: 676, width: 694, channels: 3, size: 1407432, dtype: uint8. C++: height: 676, width: 694, channels: 3, size: 676 x 694, dtype: 16 The difference is only in the dtype(I don't know about the size in c++) – Netanel Hadad Nov 07 '22 at 14:09
  • @ChristophRackwitz according to the table in the line, the type is *CV_8UC(3)*: https://stackoverflow.com/questions/10167534/how-to-find-out-what-type-of-a-mat-object-is-with-mattype-in-opencv – Netanel Hadad Nov 07 '22 at 14:39
  • good, so those things check out. 3-channel uint8 is encoded as "16" in OpenCV. `CV_8UC3 == 16`. -- I don't have more ideas yet. see if this helps: https://learnopencv.com/opencv-qr-code-scanner-c-and-python/ – Christoph Rackwitz Nov 07 '22 at 15:46
  • crosspost: https://forum.opencv.org/t/opencv-c-on-android-fails-to-decode-qr-which-opencv-python-does/10858 – Christoph Rackwitz Nov 07 '22 at 16:10

0 Answers0