Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1e9a9bc1 authored by Weng Su's avatar Weng Su
Browse files

Support color reverse QR code

- If decoding the QR code fails, invert the colors and try decoding again.

Bug: 192668560
Bug: 295112770
Test: manual test
Change-Id: Ie73170b32637dc01e2ddd9f5c1fb1daf1d9f0648
parent 730631e0
Loading
Loading
Loading
Loading
+17 −10
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import androidx.annotation.VisibleForTesting;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
@@ -249,16 +250,10 @@ public class QrCamera extends Handler {
                    // Semaphore.acquire() blocking until permit is available, or the thread is
                    // interrupted.
                    imageGot.acquire();
                    Result qrCode = null;
                    try {
                        qrCode =
                                mReader.decodeWithState(
                                        new BinaryBitmap(new HybridBinarizer(mImage)));
                    } catch (ReaderException e) {
                        // No logging since every time the reader cannot decode the
                        // image, this ReaderException will be thrown.
                    } finally {
                        mReader.reset();
                    Result qrCode = decodeQrCode(mImage);
                    if (qrCode == null) {
                        // Check color inversion QR code
                        qrCode = decodeQrCode(mImage.invert());
                    }
                    if (qrCode != null) {
                        if (mScannerCallback.isValid(qrCode.getText())) {
@@ -272,6 +267,18 @@ public class QrCamera extends Handler {
            }
        }

        private Result decodeQrCode(LuminanceSource source) {
            try {
                return mReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(source)));
            } catch (ReaderException e) {
                // No logging since every time the reader cannot decode the
                // image, this ReaderException will be thrown.
            } finally {
                mReader.reset();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String qrCode) {
            if (qrCode != null) {