casvanluijtelaar

convert to passive cropping

@@ -103,24 +103,16 @@ class MobileScanner(private val activity: Activity, private val textureRegistry: @@ -103,24 +103,16 @@ class MobileScanner(private val activity: Activity, private val textureRegistry:
103 // when (analyzeMode) { 103 // when (analyzeMode) {
104 // AnalyzeMode.BARCODE -> { 104 // AnalyzeMode.BARCODE -> {
105 val mediaImage = imageProxy.image ?: return@Analyzer 105 val mediaImage = imageProxy.image ?: return@Analyzer
106 - var inputImage: InputImage?;  
107 -  
108 - if(scanWindow != null) {  
109 - val croppedImage = croppedNV21(mediaImage, scanWindow!!)  
110 - inputImage = InputImage.fromByteArray(  
111 - croppedImage,  
112 - scanWindow!!.width(),  
113 - scanWindow!!.height(),  
114 - imageProxy.imageInfo.rotationDegrees,  
115 - IMAGE_FORMAT_NV21,  
116 - )  
117 - } else {  
118 - inputImage = InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees)  
119 - } 106 + var inputImage = InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees)
120 107
121 scanner.process(inputImage) 108 scanner.process(inputImage)
122 .addOnSuccessListener { barcodes -> 109 .addOnSuccessListener { barcodes ->
123 for (barcode in barcodes) { 110 for (barcode in barcodes) {
  111 + if(scanWindow != null) {
  112 + val boundingBox = barcode.getBoundingBox()
  113 + if(boundingBox == null || !scanWindow!!.contains(boundingBox!!)) continue
  114 + }
  115 +
124 val event = mapOf("name" to "barcode", "data" to barcode.data) 116 val event = mapOf("name" to "barcode", "data" to barcode.data)
125 sink?.success(event) 117 sink?.success(event)
126 } 118 }
@@ -132,51 +124,6 @@ class MobileScanner(private val activity: Activity, private val textureRegistry: @@ -132,51 +124,6 @@ class MobileScanner(private val activity: Activity, private val textureRegistry:
132 // } 124 // }
133 } 125 }
134 126
135 - private fun croppedNV21(mediaImage: Image, cropRect: Rect): ByteArray {  
136 - val yBuffer = mediaImage.planes[0].buffer // Y  
137 - val vuBuffer = mediaImage.planes[2].buffer // VU  
138 -  
139 - val ySize = yBuffer.remaining()  
140 - val vuSize = vuBuffer.remaining()  
141 -  
142 - val nv21 = ByteArray(ySize + vuSize)  
143 -  
144 - yBuffer.get(nv21, 0, ySize)  
145 - vuBuffer.get(nv21, ySize, vuSize)  
146 -  
147 - return cropByteArray(nv21, mediaImage.width, mediaImage.height, cropRect)  
148 - }  
149 -  
150 - private fun cropByteArray(src: ByteArray, width: Int, height: Int, cropRect: Rect, ): ByteArray {  
151 - val x = cropRect.left * 2 / 2  
152 - val y = cropRect.top * 2 / 2  
153 - val w = cropRect.width() * 2 / 2  
154 - val h = cropRect.height() * 2 / 2  
155 - val yUnit = w * h  
156 - val uv = yUnit / 2  
157 - val nData = ByteArray(yUnit + uv)  
158 - val uvIndexDst = w * h - y / 2 * w  
159 - val uvIndexSrc = width * height + x  
160 - var srcPos0 = y * width  
161 - var destPos0 = 0  
162 - var uvSrcPos0 = uvIndexSrc  
163 - var uvDestPos0 = uvIndexDst  
164 - for (i in y until y + h) {  
165 - System.arraycopy(src, srcPos0 + x, nData, destPos0, w) //y memory block copy  
166 - srcPos0 += width  
167 - destPos0 += w  
168 - if (i and 1 == 0) {  
169 - System.arraycopy(src, uvSrcPos0, nData, uvDestPos0, w) //uv memory block copy  
170 - uvSrcPos0 += width  
171 - uvDestPos0 += w  
172 - }  
173 - }  
174 - return nData  
175 - }  
176 -  
177 -  
178 -  
179 -  
180 private var scanner = BarcodeScanning.getClient() 127 private var scanner = BarcodeScanning.getClient()
181 128
182 @ExperimentalGetImage 129 @ExperimentalGetImage
@@ -305,6 +252,11 @@ class MobileScanner(private val activity: Activity, private val textureRegistry: @@ -305,6 +252,11 @@ class MobileScanner(private val activity: Activity, private val textureRegistry:
305 scanner.process(inputImage) 252 scanner.process(inputImage)
306 .addOnSuccessListener { barcodes -> 253 .addOnSuccessListener { barcodes ->
307 for (barcode in barcodes) { 254 for (barcode in barcodes) {
  255 + if(scanWindow != null) {
  256 + val boundingBox = barcode.getBoundingBox()
  257 + if(boundingBox == null || !scanWindow!!.contains(boundingBox!!)) continue
  258 + }
  259 +
308 barcodeFound = true 260 barcodeFound = true
309 sink?.success(mapOf("name" to "barcode", "data" to barcode.data)) 261 sink?.success(mapOf("name" to "barcode", "data" to barcode.data))
310 } 262 }
@@ -26,6 +26,8 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan @@ -26,6 +26,8 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
26 // var analyzeMode: Int = 0 26 // var analyzeMode: Int = 0
27 var analyzing: Bool = false 27 var analyzing: Bool = false
28 var position = AVCaptureDevice.Position.back 28 var position = AVCaptureDevice.Position.back
  29 +
  30 + var scanWindow: CGRect?
29 31
30 var scanner = BarcodeScanner.barcodeScanner() 32 var scanner = BarcodeScanner.barcodeScanner()
31 33
@@ -110,6 +112,14 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan @@ -110,6 +112,14 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
110 scanner.process(image) { [self] barcodes, error in 112 scanner.process(image) { [self] barcodes, error in
111 if error == nil && barcodes != nil { 113 if error == nil && barcodes != nil {
112 for barcode in barcodes! { 114 for barcode in barcodes! {
  115 +
  116 + if scanWindow != nil {
  117 + let boundingBox = barcode.frame
  118 + if !scanWindow!.contains(boundingBox) {
  119 + continue
  120 + }
  121 + }
  122 +
113 let event: [String: Any?] = ["name": "barcode", "data": barcode.data] 123 let event: [String: Any?] = ["name": "barcode", "data": barcode.data]
114 sink?(event) 124 sink?(event)
115 } 125 }
@@ -174,6 +184,20 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan @@ -174,6 +184,20 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
174 let torch: Bool = argReader.bool(key: "torch") ?? false 184 let torch: Bool = argReader.bool(key: "torch") ?? false
175 let facing: Int = argReader.int(key: "facing") ?? 1 185 let facing: Int = argReader.int(key: "facing") ?? 1
176 let formats: Array = argReader.intArray(key: "formats") ?? [] 186 let formats: Array = argReader.intArray(key: "formats") ?? []
  187 + let scanWindowData: Array? = argReader.floatArray(key: "scanWindow")
  188 +
  189 + if(scanWindowData != nil) {
  190 +
  191 + let minX = scanWindowData![0]
  192 + let minY = scanWindowData![1]
  193 +
  194 + let width = scanWindowData![2] - minX
  195 + let height = scanWindowData![3] - minY
  196 +
  197 + scanWindow = CGRect(x: minX, y: minY, width: width, height: height)
  198 + }
  199 +
  200 +
177 201
178 let formatList: NSMutableArray = [] 202 let formatList: NSMutableArray = []
179 for index in formats { 203 for index in formats {
@@ -243,27 +267,6 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan @@ -243,27 +267,6 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
243 captureSession.startRunning() 267 captureSession.startRunning()
244 268
245 let demensions = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription) 269 let demensions = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription)
246 -  
247 - /// limit captureSession area of interest to the scanWindow if provided  
248 - let scanWindowData: Array? = argReader.floatArray(key: "scanWindow")  
249 - if(scanWindowData != nil) {  
250 -  
251 - let captureMetadataOutput = AVCaptureMetadataOutput()  
252 -  
253 - let minX = scanWindowData![0] / CGFloat(demensions.width)  
254 - let minY = scanWindowData![1] / CGFloat(demensions.height)  
255 -  
256 - let maxX = scanWindowData![2] / CGFloat(demensions.width)  
257 - let maxY = scanWindowData![3] / CGFloat(demensions.height)  
258 -  
259 - let width = maxX - minX  
260 - let height = maxY - minY  
261 -  
262 - captureMetadataOutput.rectOfInterest = CGRect(x: minX, y: minY, width: width, height: height)  
263 - captureSession.addOutput(captureMetadataOutput)  
264 - }  
265 -  
266 -  
267 let width = Double(demensions.height) 270 let width = Double(demensions.height)
268 let height = Double(demensions.width) 271 let height = Double(demensions.width)
269 let size = ["width": width, "height": height] 272 let size = ["width": width, "height": height]
@@ -314,6 +317,14 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan @@ -314,6 +317,14 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
314 scanner.process(image) { [self] barcodes, error in 317 scanner.process(image) { [self] barcodes, error in
315 if error == nil && barcodes != nil { 318 if error == nil && barcodes != nil {
316 for barcode in barcodes! { 319 for barcode in barcodes! {
  320 +
  321 + if scanWindow != nil {
  322 + let boundingBox = barcode.frame
  323 + if !scanWindow!.contains(boundingBox) {
  324 + continue
  325 + }
  326 + }
  327 +
317 barcodeFound = true 328 barcodeFound = true
318 let event: [String: Any?] = ["name": "barcode", "data": barcode.data] 329 let event: [String: Any?] = ["name": "barcode", "data": barcode.data]
319 sink?(event) 330 sink?(event)