Saeed

improved accuracy

@@ -188,44 +188,59 @@ class _MobileScannerState extends State<MobileScanner> @@ -188,44 +188,59 @@ class _MobileScannerState extends State<MobileScanner>
188 Size textureSize, 188 Size textureSize,
189 Size widgetSize, 189 Size widgetSize,
190 ) { 190 ) {
191 - /// map the texture size to get its new size after fitted to screen  
192 - final fittedTextureSize = applyBoxFit(fit, textureSize, widgetSize);  
193 -  
194 - /// create a new rectangle that represents the texture on the screen  
195 - final minX = widgetSize.width / 2 - fittedTextureSize.destination.width / 2;  
196 - final minY =  
197 - widgetSize.height / 2 - fittedTextureSize.destination.height / 2;  
198 - final textureWindow = Offset(minX, minY) & fittedTextureSize.destination;  
199 -  
200 - /// create a new scan window and with only the area of the rect intersecting the texture window  
201 - final scanWindowInTexture = scanWindow.intersect(textureWindow);  
202 -  
203 - /// update the scanWindow left and top to be relative to the texture not the widget  
204 - final newLeft = scanWindowInTexture.left - textureWindow.left;  
205 - final newTop = scanWindowInTexture.top - textureWindow.top;  
206 - final newWidth = scanWindowInTexture.width;  
207 - final newHeight = scanWindowInTexture.height;  
208 -  
209 - /// new scanWindow that is adapted to the boxfit and relative to the texture  
210 - final windowInTexture = Rect.fromLTWH(newLeft, newTop, newWidth, newHeight);  
211 -  
212 - /// get the scanWindow as a percentage of the texture  
213 - final percentageLeft =  
214 - windowInTexture.left / fittedTextureSize.destination.width;  
215 - final percentageTop =  
216 - windowInTexture.top / fittedTextureSize.destination.height;  
217 - final percentageRight =  
218 - windowInTexture.right / fittedTextureSize.destination.width;  
219 - final percentagebottom =  
220 - windowInTexture.bottom / fittedTextureSize.destination.height;  
221 -  
222 - /// this rectangle can be send to native code and used to cut out a rectangle of the scan image  
223 - return Rect.fromLTRB(  
224 - percentageLeft,  
225 - percentageTop,  
226 - percentageRight,  
227 - percentagebottom,  
228 - ); 191 + double fittedTextureWidth;
  192 + double fittedTextureHeight;
  193 +
  194 + switch (fit) {
  195 + case BoxFit.contain:
  196 + final widthRatio = widgetSize.width / textureSize.width;
  197 + final heightRatio = widgetSize.height / textureSize.height;
  198 + final scale = widthRatio < heightRatio ? widthRatio : heightRatio;
  199 + fittedTextureWidth = textureSize.width * scale;
  200 + fittedTextureHeight = textureSize.height * scale;
  201 + break;
  202 +
  203 + case BoxFit.cover:
  204 + final widthRatio = widgetSize.width / textureSize.width;
  205 + final heightRatio = widgetSize.height / textureSize.height;
  206 + final scale = widthRatio > heightRatio ? widthRatio : heightRatio;
  207 + fittedTextureWidth = textureSize.width * scale;
  208 + fittedTextureHeight = textureSize.height * scale;
  209 + break;
  210 +
  211 + case BoxFit.fill:
  212 + fittedTextureWidth = widgetSize.width;
  213 + fittedTextureHeight = widgetSize.height;
  214 + break;
  215 +
  216 + case BoxFit.fitHeight:
  217 + final ratio = widgetSize.height / textureSize.height;
  218 + fittedTextureWidth = textureSize.width * ratio;
  219 + fittedTextureHeight = widgetSize.height;
  220 + break;
  221 +
  222 + case BoxFit.fitWidth:
  223 + final ratio = widgetSize.width / textureSize.width;
  224 + fittedTextureWidth = widgetSize.width;
  225 + fittedTextureHeight = textureSize.height * ratio;
  226 + break;
  227 +
  228 + case BoxFit.none:
  229 + case BoxFit.scaleDown:
  230 + fittedTextureWidth = textureSize.width;
  231 + fittedTextureHeight = textureSize.height;
  232 + break;
  233 + }
  234 +
  235 + final offsetX = (widgetSize.width - fittedTextureWidth) / 2;
  236 + final offsetY = (widgetSize.height - fittedTextureHeight) / 2;
  237 +
  238 + final left = (scanWindow.left - offsetX) / fittedTextureWidth;
  239 + final top = (scanWindow.top - offsetY) / fittedTextureHeight;
  240 + final right = (scanWindow.right - offsetX) / fittedTextureWidth;
  241 + final bottom = (scanWindow.bottom - offsetY) / fittedTextureHeight;
  242 +
  243 + return Rect.fromLTRB(left, top, right, bottom);
229 } 244 }
230 245
231 Rect? scanWindow; 246 Rect? scanWindow;
@@ -259,6 +274,7 @@ class _MobileScannerState extends State<MobileScanner> @@ -259,6 +274,7 @@ class _MobileScannerState extends State<MobileScanner>
259 size: constraints.biggest, 274 size: constraints.biggest,
260 child: FittedBox( 275 child: FittedBox(
261 fit: widget.fit, 276 fit: widget.fit,
  277 + // alignment: Alignment.topCenter,
262 child: SizedBox( 278 child: SizedBox(
263 width: value.size.width, 279 width: value.size.width,
264 height: value.size.height, 280 height: value.size.height,