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 87501582..0369775e 100644 --- a/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt +++ b/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt @@ -89,7 +89,8 @@ class QRView(private val context: Context, messenger: BinaryMessenger, private v "getFlashInfo" -> getFlashInfo(result) "getSystemFeatures" -> getSystemFeatures(result) "changeScanArea" -> changeScanArea( - call.argument("cutOutSize")!!, + call.argument("scanAreaWidth")!!, + call.argument("scanAreaHeight")!!, call.argument("cutOutBottomOffset")!!, result, ) @@ -253,11 +254,12 @@ class QRView(private val context: Context, messenger: BinaryMessenger, private v } private fun changeScanArea( - cutOutSize: Double, + dpScanAreaWidth: Double, + dpScanAreaHeight: Double, cutOutBottomOffset: Double, result: MethodChannel.Result ) { - setScanAreaSize(cutOutSize, cutOutSize, cutOutBottomOffset) + setScanAreaSize(dpScanAreaWidth, dpScanAreaHeight, cutOutBottomOffset) result.success(true) } diff --git a/ios/Classes/QRView.swift b/ios/Classes/QRView.swift index 21661eee..e91caf88 100644 --- a/ios/Classes/QRView.swift +++ b/ios/Classes/QRView.swift @@ -56,7 +56,8 @@ public class QRView:NSObject,FlutterPlatformView { self?.setDimensions(result, width: arguments["width"] ?? 0, height: arguments["height"] ?? 0, - scanArea: arguments["scanArea"] ?? 0, + scanAreaWidth: arguments["scanAreaWidth"] ?? 0, + scanAreaHeight: arguments["scanAreaHeight"] ?? 0, scanAreaOffset: arguments["scanAreaOffset"] ?? 0) case "startScan": self?.startScan(call.arguments as! Array, result) @@ -84,7 +85,7 @@ public class QRView:NSObject,FlutterPlatformView { return previewView } - func setDimensions(_ result: @escaping FlutterResult, width: Double, height: Double, scanArea: Double, scanAreaOffset: Double) { + func setDimensions(_ result: @escaping FlutterResult, width: Double, height: Double, scanAreaWidth: Double, scanAreaHeight: Double, scanAreaOffset: Double) { // Then set the size of the preview area. previewView.frame = CGRect(x: 0, y: 0, width: width, height: height) @@ -103,9 +104,9 @@ public class QRView:NSObject,FlutterPlatformView { } // Set scanArea if provided. - if (scanArea != 0) { + if (scanAreaWidth != 0 && scanAreaHeight != 0) { scanner?.didStartScanningBlock = { - self.scanner?.scanRect = CGRect(x: Double(midX) - (scanArea / 2), y: Double(midY) - (scanArea / 2), width: scanArea, height: scanArea) + self.scanner?.scanRect = CGRect(x: Double(midX) - (scanAreaWidth / 2), y: Double(midY) - (scanAreaHeight / 2), width: scanAreaWidth, height: scanAreaHeight) // Set offset if provided. if (scanAreaOffset != 0) { diff --git a/lib/src/qr_code_scanner.dart b/lib/src/qr_code_scanner.dart index 22876eea..238529c9 100644 --- a/lib/src/qr_code_scanner.dart +++ b/lib/src/qr_code_scanner.dart @@ -334,7 +334,8 @@ class QRViewController { await channel.invokeMethod('setDimensions', { 'width': renderBox.size.width, 'height': renderBox.size.height, - 'scanArea': overlay?.cutOutSize ?? 0, + 'scanAreaWidth': overlay?.cutOutWidth ?? 0, + 'scanAreaHeight': overlay?.cutOutHeight ?? 0, 'scanAreaOffset': overlay?.cutOutBottomOffset ?? 0 }); return true; @@ -346,7 +347,8 @@ class QRViewController { return false; } await channel.invokeMethod('changeScanArea', { - 'cutOutSize': overlay.cutOutSize, + 'scanAreaWidth': overlay.cutOutWidth, + 'scanAreaHeight': overlay.cutOutHeight, 'cutOutBottomOffset': overlay.cutOutBottomOffset }); return true; diff --git a/lib/src/qr_scanner_overlay_shape.dart b/lib/src/qr_scanner_overlay_shape.dart index 1b11dfb4..0d41090c 100644 --- a/lib/src/qr_scanner_overlay_shape.dart +++ b/lib/src/qr_scanner_overlay_shape.dart @@ -1,4 +1,5 @@ import 'dart:ui'; +import 'dart:math'; import 'package:flutter/material.dart'; @@ -9,17 +10,30 @@ class QrScannerOverlayShape extends ShapeBorder { this.overlayColor = const Color.fromRGBO(0, 0, 0, 80), this.borderRadius = 0, this.borderLength = 40, - this.cutOutSize = 250, + double? cutOutSize, + double? cutOutWidth, + double? cutOutHeight, this.cutOutBottomOffset = 0, - }) : assert(borderLength <= cutOutSize / 2 + borderWidth * 2, - "Border can't be larger than ${cutOutSize / 2 + borderWidth * 2}"); + }) : cutOutWidth = cutOutWidth ?? cutOutSize ?? 250, + cutOutHeight = cutOutHeight ?? cutOutSize ?? 250 { + assert( + borderLength <= + min(this.cutOutWidth, this.cutOutHeight) / 2 + borderWidth * 2, + "Border can't be larger than ${min(this.cutOutWidth, this.cutOutHeight) / 2 + borderWidth * 2}", + ); + assert( + (cutOutSize != null && cutOutWidth == null && cutOutHeight == null) || + (cutOutSize == null && cutOutWidth != null && cutOutHeight != null), + 'Use only cutOutWidth and cutOutHeight or only cutOutSize'); + } final Color borderColor; final double borderWidth; final Color overlayColor; final double borderRadius; final double borderLength; - final double cutOutSize; + final double cutOutWidth; + final double cutOutHeight; final double cutOutBottomOffset; @override @@ -62,10 +76,14 @@ class QrScannerOverlayShape extends ShapeBorder { final borderWidthSize = width / 2; final height = rect.height; final borderOffset = borderWidth / 2; - final _borderLength = borderLength > cutOutSize / 2 + borderWidth * 2 - ? borderWidthSize / 2 - : borderLength; - final _cutOutSize = cutOutSize < width ? cutOutSize : width - borderOffset; + final _borderLength = + borderLength > min(cutOutHeight, cutOutHeight) / 2 + borderWidth * 2 + ? borderWidthSize / 2 + : borderLength; + final _cutOutWidth = + cutOutWidth < width ? cutOutWidth : width - borderOffset; + final _cutOutHeight = + cutOutHeight < height ? cutOutHeight : height - borderOffset; final backgroundPaint = Paint() ..color = overlayColor @@ -82,14 +100,14 @@ class QrScannerOverlayShape extends ShapeBorder { ..blendMode = BlendMode.dstOut; final cutOutRect = Rect.fromLTWH( - rect.left + width / 2 - _cutOutSize / 2 + borderOffset, + rect.left + width / 2 - _cutOutWidth / 2 + borderOffset, -cutOutBottomOffset + rect.top + height / 2 - - _cutOutSize / 2 + + _cutOutHeight / 2 + borderOffset, - _cutOutSize - borderOffset * 2, - _cutOutSize - borderOffset * 2, + _cutOutWidth - borderOffset * 2, + _cutOutHeight - borderOffset * 2, ); canvas