Showing
3 changed files
with
21 additions
and
24 deletions
| @@ -5,6 +5,7 @@ import android.app.Activity | @@ -5,6 +5,7 @@ import android.app.Activity | ||
| 5 | import android.content.pm.PackageManager | 5 | import android.content.pm.PackageManager |
| 6 | import android.graphics.Point | 6 | import android.graphics.Point |
| 7 | import android.graphics.Rect | 7 | import android.graphics.Rect |
| 8 | +import android.graphics.RectF | ||
| 8 | import android.net.Uri | 9 | import android.net.Uri |
| 9 | import android.util.Log | 10 | import android.util.Log |
| 10 | import android.util.Size | 11 | import android.util.Size |
| @@ -27,6 +28,7 @@ import io.flutter.plugin.common.MethodChannel | @@ -27,6 +28,7 @@ import io.flutter.plugin.common.MethodChannel | ||
| 27 | import io.flutter.plugin.common.PluginRegistry | 28 | import io.flutter.plugin.common.PluginRegistry |
| 28 | import io.flutter.view.TextureRegistry | 29 | import io.flutter.view.TextureRegistry |
| 29 | import java.io.File | 30 | import java.io.File |
| 31 | +import kotlin.math.roundToInt | ||
| 30 | 32 | ||
| 31 | 33 | ||
| 32 | class MobileScanner(private val activity: Activity, private val textureRegistry: TextureRegistry) | 34 | class MobileScanner(private val activity: Activity, private val textureRegistry: TextureRegistry) |
| @@ -43,7 +45,7 @@ class MobileScanner(private val activity: Activity, private val textureRegistry: | @@ -43,7 +45,7 @@ class MobileScanner(private val activity: Activity, private val textureRegistry: | ||
| 43 | private var camera: Camera? = null | 45 | private var camera: Camera? = null |
| 44 | private var preview: Preview? = null | 46 | private var preview: Preview? = null |
| 45 | private var textureEntry: TextureRegistry.SurfaceTextureEntry? = null | 47 | private var textureEntry: TextureRegistry.SurfaceTextureEntry? = null |
| 46 | - private var scanWindow: Rect? = null; | 48 | + private var scanWindow: List<Float>? = null; |
| 47 | 49 | ||
| 48 | // @AnalyzeMode | 50 | // @AnalyzeMode |
| 49 | // private var analyzeMode: Int = AnalyzeMode.NONE | 51 | // private var analyzeMode: Int = AnalyzeMode.NONE |
| @@ -128,27 +130,23 @@ class MobileScanner(private val activity: Activity, private val textureRegistry: | @@ -128,27 +130,23 @@ class MobileScanner(private val activity: Activity, private val textureRegistry: | ||
| 128 | 130 | ||
| 129 | private var scanner = BarcodeScanning.getClient() | 131 | private var scanner = BarcodeScanning.getClient() |
| 130 | 132 | ||
| 131 | - | ||
| 132 | private fun updateScanWindow(call: MethodCall) { | 133 | private fun updateScanWindow(call: MethodCall) { |
| 133 | - val scanWindowData: List<Int>? = call.argument("rect") | ||
| 134 | - if(scanWindowData == null) return | ||
| 135 | - | ||
| 136 | - scanWindow = Rect(scanWindowData!![0], scanWindowData!![1], scanWindowData!![2], scanWindowData!![3]) | 134 | + scanWindow = call.argument<List<Float>>("rect") |
| 137 | } | 135 | } |
| 138 | 136 | ||
| 139 | // scales the scanWindow to the provided inputImage and checks if that scaled | 137 | // scales the scanWindow to the provided inputImage and checks if that scaled |
| 140 | // scanWindow contains the barcode | 138 | // scanWindow contains the barcode |
| 141 | - private fun isbarCodeInScanWindow(scanWindow: Rect, barcode: Barcode, inputImage: InputImage): Boolean { | 139 | + private fun isbarCodeInScanWindow(scanWindow: List<Float>, barcode: Barcode, inputImage: InputImage): Boolean { |
| 142 | val barcodeBoundingBox = barcode.getBoundingBox() | 140 | val barcodeBoundingBox = barcode.getBoundingBox() |
| 143 | if(barcodeBoundingBox == null) return false | 141 | if(barcodeBoundingBox == null) return false |
| 144 | - | 142 | + |
| 145 | val imageWidth = inputImage.getWidth(); | 143 | val imageWidth = inputImage.getWidth(); |
| 146 | val imageHeight = inputImage.getHeight(); | 144 | val imageHeight = inputImage.getHeight(); |
| 147 | 145 | ||
| 148 | - val left = scanWindow.left * imageWidth | ||
| 149 | - val top = scanWindow.top * imageHeight | ||
| 150 | - val right = scanWindow.right * imageWidth | ||
| 151 | - val bottom = scanWindow.bottom * imageHeight | 146 | + val left = (scanWindow[0] * imageWidth).roundToInt() |
| 147 | + val top = (scanWindow[1] * imageHeight).roundToInt() | ||
| 148 | + val right = (scanWindow[2] * imageWidth).roundToInt() | ||
| 149 | + val bottom = (scanWindow[3] * imageHeight).roundToInt() | ||
| 152 | 150 | ||
| 153 | val scaledScanWindow = Rect(left, top, right, bottom) | 151 | val scaledScanWindow = Rect(left, top, right, bottom) |
| 154 | return scaledScanWindow.contains(barcodeBoundingBox) | 152 | return scaledScanWindow.contains(barcodeBoundingBox) |
| @@ -92,20 +92,20 @@ class _MobileScannerState extends State<MobileScanner> | @@ -92,20 +92,20 @@ class _MobileScannerState extends State<MobileScanner> | ||
| 92 | /// map the texture size to get its new size after fitted to screen | 92 | /// map the texture size to get its new size after fitted to screen |
| 93 | final fittedSizes = applyBoxFit(fit, textureSize, widgetSize); | 93 | final fittedSizes = applyBoxFit(fit, textureSize, widgetSize); |
| 94 | final fittedTextureSize = fittedSizes.destination; | 94 | final fittedTextureSize = fittedSizes.destination; |
| 95 | - | 95 | + |
| 96 | + /// create a new rectangle that represents the texture on the screen | ||
| 96 | final minX = widgetSize.width / 2 - fittedTextureSize.width / 2; | 97 | final minX = widgetSize.width / 2 - fittedTextureSize.width / 2; |
| 97 | final minY = widgetSize.height / 2 - fittedTextureSize.height / 2; | 98 | final minY = widgetSize.height / 2 - fittedTextureSize.height / 2; |
| 98 | final width = fittedTextureSize.width; | 99 | final width = fittedTextureSize.width; |
| 99 | final height = fittedTextureSize.height; | 100 | final height = fittedTextureSize.height; |
| 100 | - | ||
| 101 | final textureWindow = Rect.fromLTWH(minX, minY, width, height); | 101 | final textureWindow = Rect.fromLTWH(minX, minY, width, height); |
| 102 | 102 | ||
| 103 | - /// create a new scan window and with only the area of the rect intersecting the texture | 103 | + /// create a new scan window and with only the area of the rect intersecting the texture window |
| 104 | final scanWindowInTexture = scanWindow.intersect(textureWindow); | 104 | final scanWindowInTexture = scanWindow.intersect(textureWindow); |
| 105 | 105 | ||
| 106 | /// update the scanWindow left and top to be relative to the texture not the widget | 106 | /// update the scanWindow left and top to be relative to the texture not the widget |
| 107 | - final newLeft = scanWindowInTexture.left - minX; | ||
| 108 | - final newTop = scanWindowInTexture.top - minY; | 107 | + final newLeft = scanWindowInTexture.left - textureWindow.left; |
| 108 | + final newTop = scanWindowInTexture.top - textureWindow.top; | ||
| 109 | final newWidth = scanWindowInTexture.width; | 109 | final newWidth = scanWindowInTexture.width; |
| 110 | final newHeight = scanWindowInTexture.height; | 110 | final newHeight = scanWindowInTexture.height; |
| 111 | 111 | ||
| @@ -114,8 +114,8 @@ class _MobileScannerState extends State<MobileScanner> | @@ -114,8 +114,8 @@ class _MobileScannerState extends State<MobileScanner> | ||
| 114 | 114 | ||
| 115 | /// get the scanWindow as a percentage of the texture | 115 | /// get the scanWindow as a percentage of the texture |
| 116 | final percentageLeft = windowInTexture.left / fittedTextureSize.width; | 116 | final percentageLeft = windowInTexture.left / fittedTextureSize.width; |
| 117 | - final percentageRight = windowInTexture.right / fittedTextureSize.width; | ||
| 118 | final percentageTop = windowInTexture.top / fittedTextureSize.height; | 117 | final percentageTop = windowInTexture.top / fittedTextureSize.height; |
| 118 | + final percentageRight = windowInTexture.right / fittedTextureSize.width; | ||
| 119 | final percentagebottom = windowInTexture.bottom / fittedTextureSize.height; | 119 | final percentagebottom = windowInTexture.bottom / fittedTextureSize.height; |
| 120 | 120 | ||
| 121 | /// this rectangle can be send to native code and used to cut out a rectangle of the scan image | 121 | /// this rectangle can be send to native code and used to cut out a rectangle of the scan image |
| @@ -145,16 +145,15 @@ class _MobileScannerState extends State<MobileScanner> | @@ -145,16 +145,15 @@ class _MobileScannerState extends State<MobileScanner> | ||
| 145 | value.size, | 145 | value.size, |
| 146 | Size(constraints.maxWidth, constraints.maxHeight), | 146 | Size(constraints.maxWidth, constraints.maxHeight), |
| 147 | ); | 147 | ); |
| 148 | - print(window); | 148 | + |
| 149 | controller.updateScanWindow(window); | 149 | controller.updateScanWindow(window); |
| 150 | } | 150 | } |
| 151 | 151 | ||
| 152 | controller.barcodes.listen((barcode) { | 152 | controller.barcodes.listen((barcode) { |
| 153 | if (!widget.allowDuplicates) { | 153 | if (!widget.allowDuplicates) { |
| 154 | - if (lastScanned != barcode.rawValue) { | ||
| 155 | - lastScanned = barcode.rawValue; | ||
| 156 | - widget.onDetect(barcode, value! as MobileScannerArguments); | ||
| 157 | - } | 154 | + if (lastScanned == barcode.rawValue) return; |
| 155 | + lastScanned = barcode.rawValue; | ||
| 156 | + widget.onDetect(barcode, value! as MobileScannerArguments); | ||
| 158 | } else { | 157 | } else { |
| 159 | widget.onDetect(barcode, value! as MobileScannerArguments); | 158 | widget.onDetect(barcode, value! as MobileScannerArguments); |
| 160 | } | 159 | } |
-
Please register or login to post a comment