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

Commit bf0122a0 authored by Mohammed Althaf T's avatar Mohammed Althaf T 😊
Browse files

Merge branch '2586-main-qr_update' into 'main'

Camera: Update QR implementation

See merge request !83
parents a7f1303e 2ec6fecc
Loading
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -101,6 +101,15 @@ android {
        }
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }

    kotlinOptions {
        jvmTarget = '11'
    }

    // needed to use android.test package (ActivityInstrumentationTestCase2 etc) when targetting sdk 28 (Android 9) -
    // see https://developer.android.com/training/testing/set-up-project
    useLibrary 'android.test.runner'
@@ -133,7 +142,8 @@ dependencies {
    implementation 'androidx.exifinterface:exifinterface:1.3.7'

    implementation 'androidx.camera:camera-core:1.2.3'
    implementation 'com.google.zxing:core:3.5.2'
    implementation 'com.google.zxing:core:3.5.3'
    implementation 'io.github.zxing-cpp:android:2.2.0'

    testImplementation 'junit:junit:4.13.2'

+1 −6
Original line number Diff line number Diff line
@@ -296,12 +296,7 @@ public class MainActivity extends AppCompatActivity implements PreferenceFragmen

        //QRCode
        job = JobKt.Job(null);
        coroutineScope = new CoroutineScope() {
            @Override
            public CoroutineContext getCoroutineContext() {
                return Dispatchers.getMain().plus(job);
            }
        };
        coroutineScope = () -> Dispatchers.getMain().plus(job);
        qrImageAnalyzer = new QrImageAnalyzer(this, coroutineScope);

        setContentView(R.layout.activity_main);
+0 −42
Original line number Diff line number Diff line
/*
 * SPDX-FileCopyrightText: 2022 The LineageOS Project
 * SPDX-License-Identifier: Apache-2.0
 */

package net.sourceforge.opencamera.ext

import androidx.camera.core.ImageProxy
import com.google.zxing.PlanarYUVLuminanceSource

private fun rotateYUVLuminancePlane(data: ByteArray, width: Int, height: Int): ByteArray {
    val yuv = ByteArray(width * height)
    // Rotate the Y luma
    var i = 0
    for (x in 0 until width) {
        for (y in height - 1 downTo 0) {
            yuv[i] = data[y * width + x]
            i++
        }
    }
    return yuv
}

internal val ImageProxy.planarYUVLuminanceSource: PlanarYUVLuminanceSource
    get() {
        val plane = planes[0]
        val buffer = plane.buffer
        var bytes = ByteArray(buffer.remaining())
        buffer.get(bytes)

        var width = width
        var height = height

        if (imageInfo.rotationDegrees == 90 || imageInfo.rotationDegrees == 270) {
            bytes = rotateYUVLuminancePlane(bytes, width, height)
            width = height.also { height = width }
        }

        return PlanarYUVLuminanceSource(
            bytes, width, height, 0, 0, width, height, true
        )
    }
+11 −1
Original line number Diff line number Diff line
@@ -38,6 +38,13 @@ public class OverlayQRCodeView extends View {

    private void init() {
        qrcode = ContextCompat.getDrawable(this.getContext(), R.drawable.scan_area);

        // Ensure the view recalculates the position based on the current orientation
        addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft,
                                   oldTop, oldRight, oldBottom) -> updateQRCodeBounds());
    }

    private void updateQRCodeBounds() {
        DisplayMetrics displayMetrics = this.getContext().getResources().getDisplayMetrics();
        final int screenWidth = displayMetrics.widthPixels;
        final int screenHeight = displayMetrics.heightPixels;
@@ -51,7 +58,10 @@ public class OverlayQRCodeView extends View {
            isValid = false;
        } else {
            qrcode.setBounds(left, top, right, bottom);
            isValid = true;
        }

        invalidate(); // Redraw the view
    }

    @Override
+5 −34
Original line number Diff line number Diff line
@@ -101,14 +101,6 @@ import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.RGBLuminanceSource;

/** This class was originally named due to encapsulating the camera preview,
 *  but in practice it's grown to more than this, and includes most of the
@@ -1135,31 +1127,10 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
            }
            TextureView textureView = (TextureView) this.cameraSurface;
            Bitmap bitmap = textureView.getBitmap(preview_bitmap);

            if (bitmap!=null) {
                int width = bitmap.getWidth();
                int height = bitmap.getHeight();
                int[] pixels = new int[width * height];
                bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

                LuminanceSource source = new RGBLuminanceSource(width, height, pixels);
                BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
                Reader reader = new MultiFormatReader();

                try {
                    Result result = reader.decode(binaryBitmap);
                    String qrcodeContent = result.getText();
            MainActivity mActivity = (MainActivity) this.getContext();
                    if (MyDebug.LOG)
                        Log.d(TAG, "Find QRCode qrcodeContent="+qrcodeContent );
                    if (result.getBarcodeFormat() == BarcodeFormat.QR_CODE) {
                        mActivity.qrImageAnalyzer.showQrDialog(result);
                    }
                } catch (Exception e) {
                    // K1ZFP TODO Error 2
                }
            } else {
                // K1ZFP TODO Error 1
            int rotation = getDisplayRotationDegrees(false);
            if (bitmap != null) {
                mActivity.qrImageAnalyzer.readImage(bitmap, rotation);
            }
        } else if (!previewBitmapWasEnabled) {
            disablePreviewBitmap();
Loading