From b8911a7fc182f4b47e58bcbc5d6b7ff3fef6a9e7 Mon Sep 17 00:00:00 2001 From: Tihomirov Date: Fri, 30 Jul 2021 12:35:44 +0300 Subject: [PATCH] Created CustomFramingRectBarcodeView to support changing cutOutBottomOffset --- .../CustomFramingRectBarcodeView.java | 50 +++++++++++++++++++ .../net/touchcapture/qr/flutterqr/QRView.kt | 11 ++-- example/lib/main.dart | 12 +++-- 3 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 android/src/main/kotlin/net/touchcapture/qr/flutterqr/CustomFramingRectBarcodeView.java diff --git a/android/src/main/kotlin/net/touchcapture/qr/flutterqr/CustomFramingRectBarcodeView.java b/android/src/main/kotlin/net/touchcapture/qr/flutterqr/CustomFramingRectBarcodeView.java new file mode 100644 index 0000000..5203968 --- /dev/null +++ b/android/src/main/kotlin/net/touchcapture/qr/flutterqr/CustomFramingRectBarcodeView.java @@ -0,0 +1,50 @@ +package net.touchcapture.qr.flutterqr; + +import android.content.Context; +import android.graphics.Rect; +import android.util.AttributeSet; + +import com.journeyapps.barcodescanner.BarcodeView; +import com.journeyapps.barcodescanner.Size; + +public class CustomFramingRectBarcodeView extends BarcodeView { + + private static final int BOTTOM_OFFSET_NOT_SET_VALUE = -1; + + private int bottomOffset = BOTTOM_OFFSET_NOT_SET_VALUE; + + public CustomFramingRectBarcodeView(Context context) { + super(context); + } + + public CustomFramingRectBarcodeView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public CustomFramingRectBarcodeView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + @Override + protected Rect calculateFramingRect(Rect container, Rect surface) { + Rect containerArea = new Rect(container); + boolean intersects = containerArea.intersect(surface);//adjusts the containerArea (code from super.calculateFramingRect) + Rect scanAreaRect = super.calculateFramingRect(container, surface); + if (bottomOffset != BOTTOM_OFFSET_NOT_SET_VALUE) {//if the setFramingRect function was called, then we shift the scan area by Y + Rect scanAreaRectWithOffset = new Rect(scanAreaRect); + scanAreaRectWithOffset.bottom -= bottomOffset; + scanAreaRectWithOffset.top -= bottomOffset; + + boolean belongsToContainer = scanAreaRectWithOffset.intersect(containerArea); + if(belongsToContainer){ + return scanAreaRectWithOffset; + } + } + return scanAreaRect; + } + + public void setFramingRect(int rectWidth, int rectHeight, int bottomOffset) { + this.bottomOffset = bottomOffset; + this.setFramingRectSize(new Size(rectWidth, rectHeight)); + } +} diff --git a/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt b/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt index eb65444..68b41ad 100644 --- a/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt +++ b/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt @@ -13,8 +13,6 @@ import com.google.zxing.BarcodeFormat import com.google.zxing.ResultPoint import com.journeyapps.barcodescanner.BarcodeCallback import com.journeyapps.barcodescanner.BarcodeResult -import com.journeyapps.barcodescanner.BarcodeView -import com.journeyapps.barcodescanner.Size import io.flutter.plugin.common.BinaryMessenger import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel @@ -27,7 +25,7 @@ class QRView(private val context: Context, messenger: BinaryMessenger, private v private var isTorchOn: Boolean = false private var isPaused: Boolean = false - private var barcodeView: BarcodeView? = null + private var barcodeView: CustomFramingRectBarcodeView? = null private val channel: MethodChannel = MethodChannel(messenger, "net.touchcapture.qr.flutterqr/qrview_$id") private var permissionGranted: Boolean = false @@ -193,9 +191,10 @@ class QRView(private val context: Context, messenger: BinaryMessenger, private v return initBarCodeView().apply {}!! } - private fun initBarCodeView(): BarcodeView? { + private fun initBarCodeView(): CustomFramingRectBarcodeView? { if (barcodeView == null) { - barcodeView = BarcodeView(Shared.activity) + barcodeView = + CustomFramingRectBarcodeView(Shared.activity) if (params["cameraFacing"] as Int == 1) { barcodeView?.cameraSettings?.requestedCameraId = CameraInfo.CAMERA_FACING_FRONT } @@ -255,7 +254,7 @@ class QRView(private val context: Context, messenger: BinaryMessenger, private v } private fun setScanAreaSize(dpScanAreaWidth: Double, dpScanAreaHeight: Double) { - barcodeView?.framingRectSize = Size(convertDpToPixels(dpScanAreaWidth), convertDpToPixels(dpScanAreaHeight)) + barcodeView?.setFramingRect (convertDpToPixels(dpScanAreaWidth), convertDpToPixels(dpScanAreaHeight), convertDpToPixels(150.0)) } private fun convertDpToPixels(dp: Double) = (dp * context.resources.displayMetrics.density).toInt() diff --git a/example/lib/main.dart b/example/lib/main.dart index abf6ed0..5010e43 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -151,11 +151,13 @@ class _QRViewExampleState extends State { key: qrKey, onQRViewCreated: _onQRViewCreated, overlay: QrScannerOverlayShape( - borderColor: Colors.red, - borderRadius: 10, - borderLength: 30, - borderWidth: 10, - cutOutSize: scanArea), + borderColor: Colors.red, + borderRadius: 10, + borderLength: 30, + borderWidth: 10, + cutOutSize: /*scanArea*/100, + cutOutBottomOffset: 150, + ), onPermissionSet: (ctrl, p) => _onPermissionSet(context, ctrl, p), ); }