diff --git a/example/android/gradle.properties b/example/android/gradle.properties index a673820..4d3226a 100644 --- a/example/android/gradle.properties +++ b/example/android/gradle.properties @@ -1,4 +1,3 @@ org.gradle.jvmargs=-Xmx1536M android.useAndroidX=true -android.enableJetifier=true -android.enableR8=true +android.enableJetifier=true \ No newline at end of file diff --git a/ios/Classes/QRView.swift b/ios/Classes/QRView.swift index a564c1c..af9be2c 100644 --- a/ios/Classes/QRView.swift +++ b/ios/Classes/QRView.swift @@ -46,7 +46,11 @@ public class QRView:NSObject,FlutterPlatformView { switch(call.method){ case "setDimensions": let arguments = call.arguments as! Dictionary - self?.setDimensions(result, width: arguments["width"] ?? 0, height: arguments["height"] ?? 0, scanArea: arguments["scanArea"] ?? 0) + self?.setDimensions(result, + width: arguments["width"] ?? 0, + height: arguments["height"] ?? 0, + scanArea: arguments["scanArea"] ?? 0, + scanAreaOffset: arguments["scanAreaOffset"] ?? 0) case "startScan": self?.startScan(call.arguments as! Array, result) case "flipCamera": @@ -71,7 +75,7 @@ public class QRView:NSObject,FlutterPlatformView { return previewView } - func setDimensions(_ result: @escaping FlutterResult, width: Double, height: Double, scanArea: Double) -> Void { + func setDimensions(_ result: @escaping FlutterResult, width: Double, height: Double, scanArea: Double, scanAreaOffset: Double) -> Void { MTBBarcodeScanner.requestCameraPermission(success: { permissionGranted in if permissionGranted { // self?.channel.invokeMethod("onPermissionSet", arguments: true) @@ -89,6 +93,11 @@ public class QRView:NSObject,FlutterPlatformView { } if (scanArea != 0) { sc.scanRect = CGRect(x: Double(midX) - (scanArea / 2), y: Double(midY) - (scanArea / 2), width: scanArea, height: scanArea) + + // Set offset if provided. + if (scanAreaOffset != 0) { + sc.scanRect = sc.scanRect.offsetBy(dx: 0, dy: CGFloat(scanAreaOffset)) + } } } else { // Create a scanner view if it doesn't exist yet. @@ -97,6 +106,11 @@ public class QRView:NSObject,FlutterPlatformView { if (scanArea != 0) { self.scanner?.didStartScanningBlock = { self.scanner?.scanRect = CGRect(x: Double(midX) - (scanArea / 2), y: Double(midY) - (scanArea / 2), width: scanArea, height: scanArea) + + // Set offset if provided. + if (scanAreaOffset != 0) { + self.scanner?.scanRect = (self.scanner?.scanRect.offsetBy(dx: 0, dy: CGFloat(scanAreaOffset)))! + } } } } diff --git a/lib/src/qr_code_scanner.dart b/lib/src/qr_code_scanner.dart index 0ef137e..a63fc98 100644 --- a/lib/src/qr_code_scanner.dart +++ b/lib/src/qr_code_scanner.dart @@ -73,8 +73,7 @@ class _QRViewState extends State { bool onNotification(notification) { Future.microtask(() => { QRViewController.updateDimensions(widget.key, _channel, - scanArea: - widget.overlay != null ? widget.overlay.cutOutSize : 0.0) + overlay: widget.overlay) }); return false; } @@ -122,18 +121,12 @@ class _QRViewState extends State { } void _onPlatformViewCreated(int id) { - // We pass the cutout size so that the scanner respects the scan area. - var cutOutSize = 0.0; - if (widget.overlay != null) { - cutOutSize = widget.overlay.cutOutSize; - } - _channel = MethodChannel('net.touchcapture.qr.flutterqr/qrview_$id'); // Start scan after creation of the view - final controller = QRViewController._( - _channel, widget.key, cutOutSize, widget.onPermissionSet) - .._startScan(widget.key, cutOutSize, widget.formatsAllowed); + final controller = + QRViewController._(_channel, widget.key, widget.onPermissionSet) + .._startScan(widget.key, widget.overlay, widget.formatsAllowed); // Initialize the controller for controlling the QRView if (widget.onQRViewCreated != null) { @@ -157,7 +150,7 @@ class _QrCameraSettings { } class QRViewController { - QRViewController._(MethodChannel channel, GlobalKey qrKey, double scanArea, + QRViewController._(MethodChannel channel, GlobalKey qrKey, PermissionSetCallback onPermissionSet) : _channel = channel { _channel.setMethodCallHandler((call) async { @@ -208,12 +201,11 @@ class QRViewController { bool get hasPermissions => _hasPermissions; /// Starts the barcode scanner - Future _startScan(GlobalKey key, double cutOutSize, + Future _startScan(GlobalKey key, QrScannerOverlayShape overlay, List barcodeFormats) async { // We need to update the dimension before the scan is started. try { - await QRViewController.updateDimensions(key, _channel, - scanArea: cutOutSize); + await QRViewController.updateDimensions(key, _channel, overlay: overlay); return await _channel.invokeMethod( 'startScan', barcodeFormats?.map((e) => e.asInt())?.toList() ?? []); } on PlatformException catch (e) { @@ -295,14 +287,15 @@ class QRViewController { /// Updates the view dimensions for iOS. static Future updateDimensions(GlobalKey key, MethodChannel channel, - {double scanArea}) async { + {QrScannerOverlayShape overlay}) async { if (defaultTargetPlatform == TargetPlatform.iOS) { final RenderBox renderBox = key.currentContext.findRenderObject(); try { await channel.invokeMethod('setDimensions', { 'width': renderBox.size.width, 'height': renderBox.size.height, - 'scanArea': scanArea ?? 0 + 'scanArea': overlay.cutOutSize ?? 0, + 'scanAreaOffset': overlay.cutOutBottomOffset ?? 0 }); } on PlatformException catch (e) { throw CameraException(e.code, e.message);