Showing
3 changed files
with
43 additions
and
21 deletions
| @@ -26,7 +26,6 @@ class _BarcodeScannerWithScanWindowState | @@ -26,7 +26,6 @@ class _BarcodeScannerWithScanWindowState | ||
| 26 | 26 | ||
| 27 | @override | 27 | @override |
| 28 | Widget build(BuildContext context) { | 28 | Widget build(BuildContext context) { |
| 29 | - final query = MediaQuery.of(context); | ||
| 30 | final scanWindow = Rect.fromCenter( | 29 | final scanWindow = Rect.fromCenter( |
| 31 | center: MediaQuery.of(context).size.center(Offset.zero), | 30 | center: MediaQuery.of(context).size.center(Offset.zero), |
| 32 | width: 200, | 31 | width: 200, |
| @@ -54,7 +53,13 @@ class _BarcodeScannerWithScanWindowState | @@ -54,7 +53,13 @@ class _BarcodeScannerWithScanWindowState | ||
| 54 | barcode?.corners != null && | 53 | barcode?.corners != null && |
| 55 | arguments != null) | 54 | arguments != null) |
| 56 | CustomPaint( | 55 | CustomPaint( |
| 57 | - painter: BarcodeOverlay(barcode!, arguments!, BoxFit.contain, MediaQuery.of(context).devicePixelRatio, capture!), | 56 | + painter: BarcodeOverlay( |
| 57 | + barcode!, | ||
| 58 | + arguments!, | ||
| 59 | + BoxFit.contain, | ||
| 60 | + MediaQuery.of(context).devicePixelRatio, | ||
| 61 | + capture!, | ||
| 62 | + ), | ||
| 58 | ), | 63 | ), |
| 59 | CustomPaint( | 64 | CustomPaint( |
| 60 | painter: ScannerOverlay(scanWindow), | 65 | painter: ScannerOverlay(scanWindow), |
| @@ -126,7 +131,13 @@ class ScannerOverlay extends CustomPainter { | @@ -126,7 +131,13 @@ class ScannerOverlay extends CustomPainter { | ||
| 126 | } | 131 | } |
| 127 | 132 | ||
| 128 | class BarcodeOverlay extends CustomPainter { | 133 | class BarcodeOverlay extends CustomPainter { |
| 129 | - BarcodeOverlay(this.barcode, this.arguments, this.boxFit, this.devicePixelRatio, this.capture); | 134 | + BarcodeOverlay( |
| 135 | + this.barcode, | ||
| 136 | + this.arguments, | ||
| 137 | + this.boxFit, | ||
| 138 | + this.devicePixelRatio, | ||
| 139 | + this.capture, | ||
| 140 | + ); | ||
| 130 | 141 | ||
| 131 | final BarcodeCapture capture; | 142 | final BarcodeCapture capture; |
| 132 | final Barcode barcode; | 143 | final Barcode barcode; |
| @@ -153,13 +164,21 @@ class BarcodeOverlay extends CustomPainter { | @@ -153,13 +164,21 @@ class BarcodeOverlay extends CustomPainter { | ||
| 153 | horizontalPadding = 0; | 164 | horizontalPadding = 0; |
| 154 | } | 165 | } |
| 155 | 166 | ||
| 156 | - final ratioWidth = (Platform.isIOS ? capture.width! : arguments.size.width)/ adjustedSize.destination.width; | ||
| 157 | - final ratioHeight = (Platform.isIOS ? capture.height! : arguments.size.height) / adjustedSize.destination.height; | 167 | + final ratioWidth = |
| 168 | + (Platform.isIOS ? capture.width! : arguments.size.width) / | ||
| 169 | + adjustedSize.destination.width; | ||
| 170 | + final ratioHeight = | ||
| 171 | + (Platform.isIOS ? capture.height! : arguments.size.height) / | ||
| 172 | + adjustedSize.destination.height; | ||
| 158 | 173 | ||
| 159 | final List<Offset> adjustedOffset = []; | 174 | final List<Offset> adjustedOffset = []; |
| 160 | for (final offset in barcode.corners!) { | 175 | for (final offset in barcode.corners!) { |
| 161 | - adjustedOffset.add(Offset(offset.dx / ratioWidth + horizontalPadding, | ||
| 162 | - offset.dy / ratioHeight + verticalPadding)); | 176 | + adjustedOffset.add( |
| 177 | + Offset( | ||
| 178 | + offset.dx / ratioWidth + horizontalPadding, | ||
| 179 | + offset.dy / ratioHeight + verticalPadding, | ||
| 180 | + ), | ||
| 181 | + ); | ||
| 163 | } | 182 | } |
| 164 | final cutoutPath = Path()..addPolygon(adjustedOffset, true); | 183 | final cutoutPath = Path()..addPolygon(adjustedOffset, true); |
| 165 | 184 |
| 1 | import 'dart:async'; | 1 | import 'dart:async'; |
| 2 | 2 | ||
| 3 | import 'package:flutter/foundation.dart'; | 3 | import 'package:flutter/foundation.dart'; |
| 4 | -import 'package:flutter/material.dart' hide applyBoxFit; | ||
| 5 | import 'package:flutter/material.dart'; | 4 | import 'package:flutter/material.dart'; |
| 6 | import 'package:mobile_scanner/src/mobile_scanner_controller.dart'; | 5 | import 'package:mobile_scanner/src/mobile_scanner_controller.dart'; |
| 7 | import 'package:mobile_scanner/src/objects/barcode_capture.dart'; | 6 | import 'package:mobile_scanner/src/objects/barcode_capture.dart'; |
| @@ -135,17 +134,18 @@ class _MobileScannerState extends State<MobileScanner> | @@ -135,17 +134,18 @@ class _MobileScannerState extends State<MobileScanner> | ||
| 135 | /// since the textures size and the actuall image (on the texture size) might not be the same, we also need to | 134 | /// since the textures size and the actuall image (on the texture size) might not be the same, we also need to |
| 136 | /// calculate the scanWindow in terms of percentages of the texture, not pixels. | 135 | /// calculate the scanWindow in terms of percentages of the texture, not pixels. |
| 137 | Rect calculateScanWindowRelativeToTextureInPercentage( | 136 | Rect calculateScanWindowRelativeToTextureInPercentage( |
| 138 | - BoxFit fit, | ||
| 139 | - Rect scanWindow, | ||
| 140 | - Size textureSize, | ||
| 141 | - Size widgetSize, | ||
| 142 | - ) { | 137 | + BoxFit fit, |
| 138 | + Rect scanWindow, | ||
| 139 | + Size textureSize, | ||
| 140 | + Size widgetSize, | ||
| 141 | + ) { | ||
| 143 | /// map the texture size to get its new size after fitted to screen | 142 | /// map the texture size to get its new size after fitted to screen |
| 144 | final fittedTextureSize = applyBoxFit(fit, textureSize, widgetSize); | 143 | final fittedTextureSize = applyBoxFit(fit, textureSize, widgetSize); |
| 145 | 144 | ||
| 146 | /// create a new rectangle that represents the texture on the screen | 145 | /// create a new rectangle that represents the texture on the screen |
| 147 | final minX = widgetSize.width / 2 - fittedTextureSize.destination.width / 2; | 146 | final minX = widgetSize.width / 2 - fittedTextureSize.destination.width / 2; |
| 148 | - final minY = widgetSize.height / 2 - fittedTextureSize.destination.height / 2; | 147 | + final minY = |
| 148 | + widgetSize.height / 2 - fittedTextureSize.destination.height / 2; | ||
| 149 | final textureWindow = Offset(minX, minY) & fittedTextureSize.destination; | 149 | final textureWindow = Offset(minX, minY) & fittedTextureSize.destination; |
| 150 | 150 | ||
| 151 | /// create a new scan window and with only the area of the rect intersecting the texture window | 151 | /// create a new scan window and with only the area of the rect intersecting the texture window |
| @@ -161,10 +161,14 @@ class _MobileScannerState extends State<MobileScanner> | @@ -161,10 +161,14 @@ class _MobileScannerState extends State<MobileScanner> | ||
| 161 | final windowInTexture = Rect.fromLTWH(newLeft, newTop, newWidth, newHeight); | 161 | final windowInTexture = Rect.fromLTWH(newLeft, newTop, newWidth, newHeight); |
| 162 | 162 | ||
| 163 | /// get the scanWindow as a percentage of the texture | 163 | /// get the scanWindow as a percentage of the texture |
| 164 | - final percentageLeft = windowInTexture.left / fittedTextureSize.destination.width; | ||
| 165 | - final percentageTop = windowInTexture.top / fittedTextureSize.destination.height; | ||
| 166 | - final percentageRight = windowInTexture.right / fittedTextureSize.destination.width; | ||
| 167 | - final percentagebottom = windowInTexture.bottom / fittedTextureSize.destination.height; | 164 | + final percentageLeft = |
| 165 | + windowInTexture.left / fittedTextureSize.destination.width; | ||
| 166 | + final percentageTop = | ||
| 167 | + windowInTexture.top / fittedTextureSize.destination.height; | ||
| 168 | + final percentageRight = | ||
| 169 | + windowInTexture.right / fittedTextureSize.destination.width; | ||
| 170 | + final percentagebottom = | ||
| 171 | + windowInTexture.bottom / fittedTextureSize.destination.height; | ||
| 168 | 172 | ||
| 169 | /// this rectangle can be send to native code and used to cut out a rectangle of the scan image | 173 | /// this rectangle can be send to native code and used to cut out a rectangle of the scan image |
| 170 | return Rect.fromLTRB( | 174 | return Rect.fromLTRB( |
| @@ -197,7 +201,6 @@ class _MobileScannerState extends State<MobileScanner> | @@ -197,7 +201,6 @@ class _MobileScannerState extends State<MobileScanner> | ||
| 197 | _controller.updateScanWindow(window); | 201 | _controller.updateScanWindow(window); |
| 198 | } | 202 | } |
| 199 | 203 | ||
| 200 | - | ||
| 201 | return ClipRect( | 204 | return ClipRect( |
| 202 | child: LayoutBuilder( | 205 | child: LayoutBuilder( |
| 203 | builder: (_, constraints) { | 206 | builder: (_, constraints) { |
| @@ -219,7 +222,7 @@ class _MobileScannerState extends State<MobileScanner> | @@ -219,7 +222,7 @@ class _MobileScannerState extends State<MobileScanner> | ||
| 219 | ); | 222 | ); |
| 220 | }, | 223 | }, |
| 221 | ); | 224 | ); |
| 222 | - } | 225 | + }, |
| 223 | ); | 226 | ); |
| 224 | } | 227 | } |
| 225 | 228 |
-
Please register or login to post a comment