Skip to content Skip to sidebar Skip to footer

Scan Qrcode With Inverted Colors Using Vision Api

After struggling a few hours on making my app detect this QRCode: I realized that the problem was the in the QRCode appearance. After inverting the colors, the detection was workin

Solution 1:

I improved googles example app "barcode-reader" to detect both inverted colored barcodes and regular ones.

here is a link to googles example app:

https://github.com/googlesamples/android-vision/tree/master/visionSamples/barcode-reader

I did so by editing "CameraSource" class, package: "com.google.android.gms.samples.vision.barcodereader.ui.camera".

I added a parameter: private boolean isInverted = false;

and changed function void setNextFrame(byte[] data, Camera camera):

void setNextFrame(byte[] data, Camera camera) {
            synchronized (mLock) {
                if (mPendingFrameData != null) {
                    camera.addCallbackBuffer(mPendingFrameData.array());
                    mPendingFrameData = null;
                }

                if (!mBytesToByteBuffer.containsKey(data)) {
                    Log.d(TAG,
                            "Skipping frame.  Could not find ByteBuffer associated with the image " +
                                    "data from the camera.");
                    return;
                }

                mPendingTimeMillis = SystemClock.elapsedRealtime() - mStartTimeMillis;
                mPendingFrameId++;
                if (!isInverted){
                    for (int y = 0; y < data.length; y++) {
                        data[y] = (byte) ~data[y];
                    }
                    isInverted = true;
                } else {
                    isInverted = false;
                }
                mPendingFrameData = mBytesToByteBuffer.get(data);

                // Notify the processor thread if it is waiting on the next frame (see below).
                mLock.notifyAll();
            }
        }

Solution 2:

I think this is still an open issue, please see link for details. One workaround for this as stated by a developer:

Right, the barcode API generally doesn't support color-inverted codes. There's no parameter or option to control this at the moment. Though some APIs support them, I don't believe it's a common feature.

For a workaround, you could preprocess the colors in the bitmap before passing them to the barcode API (perhaps inverting colors on alternate frames).

Hope this helps.

Post a Comment for "Scan Qrcode With Inverted Colors Using Vision Api"