Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature custom width and height of scanning area #432

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 3 additions & 2 deletions lib/src/qr_code_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ class QRViewController {
await channel.invokeMethod('setDimensions', {
'width': renderBox.size.width,
'height': renderBox.size.height,
'scanArea': overlay?.cutOutSize ?? 0,
'scanArea': overlay?.cutOutWidth ?? 0, //Todo
OrangeSofter marked this conversation as resolved.
Show resolved Hide resolved
'scanAreaOffset': overlay?.cutOutBottomOffset ?? 0
});
return true;
Expand All @@ -346,7 +346,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