# OpenCV C++ on Android fails to decode QR which Opencv python does

**URL:** <https://forum.opencv.org/t/opencv-c-on-android-fails-to-decode-qr-which-opencv-python-does/10858>\
**Category:** Android/Java\
**Tags:** qrcode\
**Created:** [November 7, 2022, 1:05pm UTC](https://forum.opencv.org/t/opencv-c-on-android-fails-to-decode-qr-which-opencv-python-does/10858 "2022-11-07T13:05:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Netanel\_Hadad](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/netanel_hadad/32/6435_2.png) [@Netanel\_Hadad](https://forum.opencv.org/u/Netanel_Hadad)\
**Post date:** [November 7, 2022, 1:05pm UTC](https://forum.opencv.org/t/opencv-c-on-android-fails-to-decode-qr-which-opencv-python-does/10858/1 "2022-11-07T13:05:05Z")

</div>

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](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/7/76ed5772da1cf3bc30c440d04cc52a68ea99998b.jpeg)](https://i.stack.imgur.com/Wdta8.jpg)

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

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [November 7, 2022, 4:10pm UTC](https://forum.opencv.org/t/opencv-c-on-android-fails-to-decode-qr-which-opencv-python-does/10858/2 "2022-11-07T16:10:05Z")

</div>

crosspost:

> <https://stackoverflow.com/questions/74344590/opencv-c-on-android-fails-to-decode-qr-which-opencv-python-does>

---

<div class="post-metadata">

**Author:** ![Netanel\_Hadad](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/netanel_hadad/32/6435_2.png) [@Netanel\_Hadad](https://forum.opencv.org/u/Netanel_Hadad)\
**Post date:** [November 8, 2022, 6:28am UTC](https://forum.opencv.org/t/opencv-c-on-android-fails-to-decode-qr-which-opencv-python-does/10858/3 "2022-11-08T06:28:49Z")

</div>

Is it a problem to ask in both communities?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [November 8, 2022, 9:18pm UTC](https://forum.opencv.org/t/opencv-c-on-android-fails-to-decode-qr-which-opencv-python-does/10858/4 "2022-11-08T21:18:30Z")

</div>

declaring crossposts is important. if you don’t do that, people waste time investigating something that was already solved, or suggest approaches (again) that were already discussed. do you see?

---

<div class="post-metadata">

**Author:** ![Netanel\_Hadad](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/netanel_hadad/32/6435_2.png) [@Netanel\_Hadad](https://forum.opencv.org/u/Netanel_Hadad)\
**Post date:** [November 9, 2022, 8:28am UTC](https://forum.opencv.org/t/opencv-c-on-android-fails-to-decode-qr-which-opencv-python-does/10858/5 "2022-11-09T08:28:05Z")

</div>

I see, thanks!  
Anyway nobody responded yet.

בתאריך יום ג׳, 8 בנוב׳ 2022 ב-23:28 מאת Christoph Rackwitz via OpenCV \<[notifications@opencv.discoursemail.com](mailto:notifications@opencv.discoursemail.com)\>:
