Julian Steenbakker
Committed by GitHub

Merge pull request #677 from sdkysfzai/master

perf: improvements to scan
@@ -5,7 +5,8 @@ @@ -5,7 +5,8 @@
5 [![mobile_scanner](https://github.com/juliansteenbakker/mobile_scanner/actions/workflows/flutter.yml/badge.svg)](https://github.com/juliansteenbakker/mobile_scanner/actions/workflows/flutter.yml) 5 [![mobile_scanner](https://github.com/juliansteenbakker/mobile_scanner/actions/workflows/flutter.yml/badge.svg)](https://github.com/juliansteenbakker/mobile_scanner/actions/workflows/flutter.yml)
6 [![GitHub Sponsors](https://img.shields.io/github/sponsors/juliansteenbakker?label=like%20my%20work?%20sponsor%20me!)](https://github.com/sponsors/juliansteenbakker) 6 [![GitHub Sponsors](https://img.shields.io/github/sponsors/juliansteenbakker?label=like%20my%20work?%20sponsor%20me!)](https://github.com/sponsors/juliansteenbakker)
7 7
8 -An universal barcode and QR code scanner for Flutter based on MLKit. Uses CameraX on Android, AVFoundation on iOS and Apple Vision & AVFoundation on macOS. 8 +A universal scanner for Flutter based on MLKit. Uses CameraX on Android and AVFoundation on iOS.
  9 +
9 10
10 ## Features Supported 11 ## Features Supported
11 12
@@ -192,44 +192,59 @@ class _MobileScannerState extends State<MobileScanner> @@ -192,44 +192,59 @@ class _MobileScannerState extends State<MobileScanner>
192 Size textureSize, 192 Size textureSize,
193 Size widgetSize, 193 Size widgetSize,
194 ) { 194 ) {
195 - /// map the texture size to get its new size after fitted to screen  
196 - final fittedTextureSize = applyBoxFit(fit, textureSize, widgetSize);  
197 -  
198 - /// create a new rectangle that represents the texture on the screen  
199 - final minX = widgetSize.width / 2 - fittedTextureSize.destination.width / 2;  
200 - final minY =  
201 - widgetSize.height / 2 - fittedTextureSize.destination.height / 2;  
202 - final textureWindow = Offset(minX, minY) & fittedTextureSize.destination;  
203 -  
204 - /// create a new scan window and with only the area of the rect intersecting the texture window  
205 - final scanWindowInTexture = scanWindow.intersect(textureWindow);  
206 -  
207 - /// update the scanWindow left and top to be relative to the texture not the widget  
208 - final newLeft = scanWindowInTexture.left - textureWindow.left;  
209 - final newTop = scanWindowInTexture.top - textureWindow.top;  
210 - final newWidth = scanWindowInTexture.width;  
211 - final newHeight = scanWindowInTexture.height;  
212 -  
213 - /// new scanWindow that is adapted to the boxfit and relative to the texture  
214 - final windowInTexture = Rect.fromLTWH(newLeft, newTop, newWidth, newHeight);  
215 -  
216 - /// get the scanWindow as a percentage of the texture  
217 - final percentageLeft =  
218 - windowInTexture.left / fittedTextureSize.destination.width;  
219 - final percentageTop =  
220 - windowInTexture.top / fittedTextureSize.destination.height;  
221 - final percentageRight =  
222 - windowInTexture.right / fittedTextureSize.destination.width;  
223 - final percentagebottom =  
224 - windowInTexture.bottom / fittedTextureSize.destination.height;  
225 -  
226 - /// this rectangle can be send to native code and used to cut out a rectangle of the scan image  
227 - return Rect.fromLTRB(  
228 - percentageLeft,  
229 - percentageTop,  
230 - percentageRight,  
231 - percentagebottom,  
232 - ); 195 + double fittedTextureWidth;
  196 + double fittedTextureHeight;
  197 +
  198 + switch (fit) {
  199 + case BoxFit.contain:
  200 + final widthRatio = widgetSize.width / textureSize.width;
  201 + final heightRatio = widgetSize.height / textureSize.height;
  202 + final scale = widthRatio < heightRatio ? widthRatio : heightRatio;
  203 + fittedTextureWidth = textureSize.width * scale;
  204 + fittedTextureHeight = textureSize.height * scale;
  205 + break;
  206 +
  207 + case BoxFit.cover:
  208 + final widthRatio = widgetSize.width / textureSize.width;
  209 + final heightRatio = widgetSize.height / textureSize.height;
  210 + final scale = widthRatio > heightRatio ? widthRatio : heightRatio;
  211 + fittedTextureWidth = textureSize.width * scale;
  212 + fittedTextureHeight = textureSize.height * scale;
  213 + break;
  214 +
  215 + case BoxFit.fill:
  216 + fittedTextureWidth = widgetSize.width;
  217 + fittedTextureHeight = widgetSize.height;
  218 + break;
  219 +
  220 + case BoxFit.fitHeight:
  221 + final ratio = widgetSize.height / textureSize.height;
  222 + fittedTextureWidth = textureSize.width * ratio;
  223 + fittedTextureHeight = widgetSize.height;
  224 + break;
  225 +
  226 + case BoxFit.fitWidth:
  227 + final ratio = widgetSize.width / textureSize.width;
  228 + fittedTextureWidth = widgetSize.width;
  229 + fittedTextureHeight = textureSize.height * ratio;
  230 + break;
  231 +
  232 + case BoxFit.none:
  233 + case BoxFit.scaleDown:
  234 + fittedTextureWidth = textureSize.width;
  235 + fittedTextureHeight = textureSize.height;
  236 + break;
  237 + }
  238 +
  239 + final offsetX = (widgetSize.width - fittedTextureWidth) / 2;
  240 + final offsetY = (widgetSize.height - fittedTextureHeight) / 2;
  241 +
  242 + final left = (scanWindow.left - offsetX) / fittedTextureWidth;
  243 + final top = (scanWindow.top - offsetY) / fittedTextureHeight;
  244 + final right = (scanWindow.right - offsetX) / fittedTextureWidth;
  245 + final bottom = (scanWindow.bottom - offsetY) / fittedTextureHeight;
  246 +
  247 + return Rect.fromLTRB(left, top, right, bottom);
233 } 248 }
234 249
235 Rect? scanWindow; 250 Rect? scanWindow;