Showing
1 changed file
with
18 additions
and
12 deletions
| @@ -2,14 +2,20 @@ import 'dart:math'; | @@ -2,14 +2,20 @@ import 'dart:math'; | ||
| 2 | 2 | ||
| 3 | import 'package:flutter/rendering.dart'; | 3 | import 'package:flutter/rendering.dart'; |
| 4 | 4 | ||
| 5 | -/// the [scanWindow] rect will be relative and scaled to the [widgetSize] not the texture. so it is possible, | ||
| 6 | -/// depending on the [fit], for the [scanWindow] to partially or not at all overlap the [textureSize] | 5 | +/// Calculate the scan window rectangle relative to the texture size. |
| 7 | /// | 6 | /// |
| 8 | -/// since when using a [BoxFit] the content will always be centered on its parent. we can convert the rect | ||
| 9 | -/// to be relative to the texture. | 7 | +/// The [scanWindow] rectangle will be relative and scaled to [widgetSize], not [textureSize]. |
| 8 | +/// Depending on the given [fit], the [scanWindow] can partially overlap the [textureSize], | ||
| 9 | +/// or not at all. | ||
| 10 | /// | 10 | /// |
| 11 | -/// since the textures size and the actuall image (on the texture size) might not be the same, we also need to | ||
| 12 | -/// calculate the scanWindow in terms of percentages of the texture, not pixels. | 11 | +/// Due to using [BoxFit] the content will always be centered on its parent, |
| 12 | +/// which enables converting the rectangle to be relative to the texture. | ||
| 13 | +/// | ||
| 14 | +/// Because the size of the actual texture and the size of the texture in widget-space | ||
| 15 | +/// can be different, calculate the size of the scan window in percentages, | ||
| 16 | +/// rather than pixels. | ||
| 17 | +/// | ||
| 18 | +/// Returns a [Rect] that represents the position and size of the scan window in the texture. | ||
| 13 | Rect calculateScanWindowRelativeToTextureInPercentage( | 19 | Rect calculateScanWindowRelativeToTextureInPercentage( |
| 14 | BoxFit fit, | 20 | BoxFit fit, |
| 15 | Rect scanWindow, | 21 | Rect scanWindow, |
| @@ -25,8 +31,7 @@ Rect calculateScanWindowRelativeToTextureInPercentage( | @@ -25,8 +31,7 @@ Rect calculateScanWindowRelativeToTextureInPercentage( | ||
| 25 | 31 | ||
| 26 | switch (fit) { | 32 | switch (fit) { |
| 27 | case BoxFit.fill: | 33 | case BoxFit.fill: |
| 28 | - // nop | ||
| 29 | - // Just use sx and sy | 34 | + // No-op, just use sx and sy. |
| 30 | break; | 35 | break; |
| 31 | case BoxFit.contain: | 36 | case BoxFit.contain: |
| 32 | final s = min(sx, sy); | 37 | final s = min(sx, sy); |
| @@ -55,13 +60,13 @@ Rect calculateScanWindowRelativeToTextureInPercentage( | @@ -55,13 +60,13 @@ Rect calculateScanWindowRelativeToTextureInPercentage( | ||
| 55 | break; | 60 | break; |
| 56 | } | 61 | } |
| 57 | 62 | ||
| 58 | - // Fit the texture size to the widget rectangle given by the scaling values above | 63 | + // Fit the texture size to the widget rectangle given by the scaling values above. |
| 59 | final textureWindow = Alignment.center.inscribe( | 64 | final textureWindow = Alignment.center.inscribe( |
| 60 | Size(textureSize.width * sx, textureSize.height * sy), | 65 | Size(textureSize.width * sx, textureSize.height * sy), |
| 61 | Rect.fromLTWH(0, 0, widgetSize.width, widgetSize.height), | 66 | Rect.fromLTWH(0, 0, widgetSize.width, widgetSize.height), |
| 62 | ); | 67 | ); |
| 63 | 68 | ||
| 64 | - // Transform the scan window from widget coordinates to texture coordinates | 69 | + // Transform the scan window from widget coordinates to texture coordinates. |
| 65 | final scanWindowInTexSpace = Rect.fromLTRB( | 70 | final scanWindowInTexSpace = Rect.fromLTRB( |
| 66 | (1 / sx) * (scanWindow.left - textureWindow.left), | 71 | (1 / sx) * (scanWindow.left - textureWindow.left), |
| 67 | (1 / sy) * (scanWindow.top - textureWindow.top), | 72 | (1 / sy) * (scanWindow.top - textureWindow.top), |
| @@ -74,13 +79,14 @@ Rect calculateScanWindowRelativeToTextureInPercentage( | @@ -74,13 +79,14 @@ Rect calculateScanWindowRelativeToTextureInPercentage( | ||
| 74 | final clippedScanWndInTexSpace = scanWindowInTexSpace | 79 | final clippedScanWndInTexSpace = scanWindowInTexSpace |
| 75 | .intersect(Rect.fromLTWH(0, 0, textureSize.width, textureSize.height)); | 80 | .intersect(Rect.fromLTWH(0, 0, textureSize.width, textureSize.height)); |
| 76 | 81 | ||
| 77 | - // Compute relative rectangle coordinates with respect to the texture size, i.e. scan image | 82 | + // Compute relative rectangle coordinates, |
| 83 | + // with respect to the texture size, i.e. scan image. | ||
| 78 | final percentageLeft = clippedScanWndInTexSpace.left / textureSize.width; | 84 | final percentageLeft = clippedScanWndInTexSpace.left / textureSize.width; |
| 79 | final percentageTop = clippedScanWndInTexSpace.top / textureSize.height; | 85 | final percentageTop = clippedScanWndInTexSpace.top / textureSize.height; |
| 80 | final percentageRight = clippedScanWndInTexSpace.right / textureSize.width; | 86 | final percentageRight = clippedScanWndInTexSpace.right / textureSize.width; |
| 81 | final percentageBottom = clippedScanWndInTexSpace.bottom / textureSize.height; | 87 | final percentageBottom = clippedScanWndInTexSpace.bottom / textureSize.height; |
| 82 | 88 | ||
| 83 | - // This rectangle can be send to native code and used to cut out a rectangle of the scan image | 89 | + // This rectangle can be used to cut out a rectangle of the scan image. |
| 84 | return Rect.fromLTRB( | 90 | return Rect.fromLTRB( |
| 85 | percentageLeft, | 91 | percentageLeft, |
| 86 | percentageTop, | 92 | percentageTop, |
-
Please register or login to post a comment