MBulli

Moved calculateScanWindowRelativeToTextureInPercentage() into own file for unit tests

@@ -6,6 +6,7 @@ import 'package:mobile_scanner/src/mobile_scanner_controller.dart'; @@ -6,6 +6,7 @@ import 'package:mobile_scanner/src/mobile_scanner_controller.dart';
6 import 'package:mobile_scanner/src/mobile_scanner_exception.dart'; 6 import 'package:mobile_scanner/src/mobile_scanner_exception.dart';
7 import 'package:mobile_scanner/src/objects/barcode_capture.dart'; 7 import 'package:mobile_scanner/src/objects/barcode_capture.dart';
8 import 'package:mobile_scanner/src/objects/mobile_scanner_arguments.dart'; 8 import 'package:mobile_scanner/src/objects/mobile_scanner_arguments.dart';
  9 +import 'package:mobile_scanner/src/scan_window_calculation.dart';
9 10
10 /// The function signature for the error builder. 11 /// The function signature for the error builder.
11 typedef MobileScannerErrorBuilder = Widget Function( 12 typedef MobileScannerErrorBuilder = Widget Function(
@@ -175,75 +176,6 @@ class _MobileScannerState extends State<MobileScanner> @@ -175,75 +176,6 @@ class _MobileScannerState extends State<MobileScanner>
175 } 176 }
176 } 177 }
177 178
178 - /// the [scanWindow] rect will be relative and scaled to the [widgetSize] not the texture. so it is possible,  
179 - /// depending on the [fit], for the [scanWindow] to partially or not at all overlap the [textureSize]  
180 - ///  
181 - /// since when using a [BoxFit] the content will always be centered on its parent. we can convert the rect  
182 - /// to be relative to the texture.  
183 - ///  
184 - /// since the textures size and the actuall image (on the texture size) might not be the same, we also need to  
185 - /// calculate the scanWindow in terms of percentages of the texture, not pixels.  
186 - Rect calculateScanWindowRelativeToTextureInPercentage(  
187 - BoxFit fit,  
188 - Rect scanWindow,  
189 - Size textureSize,  
190 - Size widgetSize,  
191 - ) {  
192 - double fittedTextureWidth;  
193 - double fittedTextureHeight;  
194 -  
195 - switch (fit) {  
196 - case BoxFit.contain:  
197 - final widthRatio = widgetSize.width / textureSize.width;  
198 - final heightRatio = widgetSize.height / textureSize.height;  
199 - final scale = widthRatio < heightRatio ? widthRatio : heightRatio;  
200 - fittedTextureWidth = textureSize.width * scale;  
201 - fittedTextureHeight = textureSize.height * scale;  
202 - break;  
203 -  
204 - case BoxFit.cover:  
205 - final widthRatio = widgetSize.width / textureSize.width;  
206 - final heightRatio = widgetSize.height / textureSize.height;  
207 - final scale = widthRatio > heightRatio ? widthRatio : heightRatio;  
208 - fittedTextureWidth = textureSize.width * scale;  
209 - fittedTextureHeight = textureSize.height * scale;  
210 - break;  
211 -  
212 - case BoxFit.fill:  
213 - fittedTextureWidth = widgetSize.width;  
214 - fittedTextureHeight = widgetSize.height;  
215 - break;  
216 -  
217 - case BoxFit.fitHeight:  
218 - final ratio = widgetSize.height / textureSize.height;  
219 - fittedTextureWidth = textureSize.width * ratio;  
220 - fittedTextureHeight = widgetSize.height;  
221 - break;  
222 -  
223 - case BoxFit.fitWidth:  
224 - final ratio = widgetSize.width / textureSize.width;  
225 - fittedTextureWidth = widgetSize.width;  
226 - fittedTextureHeight = textureSize.height * ratio;  
227 - break;  
228 -  
229 - case BoxFit.none:  
230 - case BoxFit.scaleDown:  
231 - fittedTextureWidth = textureSize.width;  
232 - fittedTextureHeight = textureSize.height;  
233 - break;  
234 - }  
235 -  
236 - final offsetX = (widgetSize.width - fittedTextureWidth) / 2;  
237 - final offsetY = (widgetSize.height - fittedTextureHeight) / 2;  
238 -  
239 - final left = (scanWindow.left - offsetX) / fittedTextureWidth;  
240 - final top = (scanWindow.top - offsetY) / fittedTextureHeight;  
241 - final right = (scanWindow.right - offsetX) / fittedTextureWidth;  
242 - final bottom = (scanWindow.bottom - offsetY) / fittedTextureHeight;  
243 -  
244 - return Rect.fromLTRB(left, top, right, bottom);  
245 - }  
246 -  
247 Rect? scanWindow; 179 Rect? scanWindow;
248 180
249 @override 181 @override
  1 +import 'package:flutter/rendering.dart';
  2 +
  3 +/// the [scanWindow] rect will be relative and scaled to the [widgetSize] not the texture. so it is possible,
  4 +/// depending on the [fit], for the [scanWindow] to partially or not at all overlap the [textureSize]
  5 +///
  6 +/// since when using a [BoxFit] the content will always be centered on its parent. we can convert the rect
  7 +/// to be relative to the texture.
  8 +///
  9 +/// since the textures size and the actuall image (on the texture size) might not be the same, we also need to
  10 +/// calculate the scanWindow in terms of percentages of the texture, not pixels.
  11 +Rect calculateScanWindowRelativeToTextureInPercentage(
  12 + BoxFit fit,
  13 + Rect scanWindow,
  14 + Size textureSize,
  15 + Size widgetSize,
  16 +) {
  17 + double fittedTextureWidth;
  18 + double fittedTextureHeight;
  19 +
  20 + switch (fit) {
  21 + case BoxFit.contain:
  22 + final widthRatio = widgetSize.width / textureSize.width;
  23 + final heightRatio = widgetSize.height / textureSize.height;
  24 + final scale = widthRatio < heightRatio ? widthRatio : heightRatio;
  25 + fittedTextureWidth = textureSize.width * scale;
  26 + fittedTextureHeight = textureSize.height * scale;
  27 + break;
  28 +
  29 + case BoxFit.cover:
  30 + final widthRatio = widgetSize.width / textureSize.width;
  31 + final heightRatio = widgetSize.height / textureSize.height;
  32 + final scale = widthRatio > heightRatio ? widthRatio : heightRatio;
  33 + fittedTextureWidth = textureSize.width * scale;
  34 + fittedTextureHeight = textureSize.height * scale;
  35 + break;
  36 +
  37 + case BoxFit.fill:
  38 + fittedTextureWidth = widgetSize.width;
  39 + fittedTextureHeight = widgetSize.height;
  40 + break;
  41 +
  42 + case BoxFit.fitHeight:
  43 + final ratio = widgetSize.height / textureSize.height;
  44 + fittedTextureWidth = textureSize.width * ratio;
  45 + fittedTextureHeight = widgetSize.height;
  46 + break;
  47 +
  48 + case BoxFit.fitWidth:
  49 + final ratio = widgetSize.width / textureSize.width;
  50 + fittedTextureWidth = widgetSize.width;
  51 + fittedTextureHeight = textureSize.height * ratio;
  52 + break;
  53 +
  54 + case BoxFit.none:
  55 + case BoxFit.scaleDown:
  56 + fittedTextureWidth = textureSize.width;
  57 + fittedTextureHeight = textureSize.height;
  58 + break;
  59 + }
  60 +
  61 + final offsetX = (widgetSize.width - fittedTextureWidth) / 2;
  62 + final offsetY = (widgetSize.height - fittedTextureHeight) / 2;
  63 +
  64 + final left = (scanWindow.left - offsetX) / fittedTextureWidth;
  65 + final top = (scanWindow.top - offsetY) / fittedTextureHeight;
  66 + final right = (scanWindow.right - offsetX) / fittedTextureWidth;
  67 + final bottom = (scanWindow.bottom - offsetY) / fittedTextureHeight;
  68 +
  69 + return Rect.fromLTRB(left, top, right, bottom);
  70 +}