Skip to content

Commit

Permalink
Merge pull request #432 from OrangeSofter/feature_custom_scan_area_wi…
Browse files Browse the repository at this point in the history
…dth_height

Feature custom width and height of scanning area
  • Loading branch information
juliansteenbakker authored Oct 11, 2021
2 parents a98c78e + 81ff47c commit 981ab7a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ class QRView(private val context: Context, messenger: BinaryMessenger, private v
"getFlashInfo" -> getFlashInfo(result)
"getSystemFeatures" -> getSystemFeatures(result)
"changeScanArea" -> changeScanArea(
call.argument<Double>("cutOutSize")!!,
call.argument<Double>("scanAreaWidth")!!,
call.argument<Double>("scanAreaHeight")!!,
call.argument<Double>("cutOutBottomOffset")!!,
result,
)
Expand Down Expand Up @@ -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)
}

Expand Down
9 changes: 5 additions & 4 deletions ios/Classes/QRView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int>, result)
Expand Down Expand Up @@ -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)

Expand All @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions lib/src/qr_code_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
42 changes: 30 additions & 12 deletions lib/src/qr_scanner_overlay_shape.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:ui';
import 'dart:math';

import 'package:flutter/material.dart';

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 981ab7a

Please sign in to comment.