Showing
4 changed files
with
25 additions
and
24 deletions
| @@ -78,7 +78,7 @@ class MobileScanner( | @@ -78,7 +78,7 @@ class MobileScanner( | ||
| 78 | scanner.process(inputImage) | 78 | scanner.process(inputImage) |
| 79 | .addOnSuccessListener { barcodes -> | 79 | .addOnSuccessListener { barcodes -> |
| 80 | if (detectionSpeed == DetectionSpeed.NO_DUPLICATES) { | 80 | if (detectionSpeed == DetectionSpeed.NO_DUPLICATES) { |
| 81 | - val newScannedBarcodes = barcodes.mapNotNull({ barcode -> barcode.rawValue }).sorted() | 81 | + val newScannedBarcodes = barcodes.mapNotNull { barcode -> barcode.rawValue }.sorted() |
| 82 | if (newScannedBarcodes == lastScanned) { | 82 | if (newScannedBarcodes == lastScanned) { |
| 83 | // New scanned is duplicate, returning | 83 | // New scanned is duplicate, returning |
| 84 | return@addOnSuccessListener | 84 | return@addOnSuccessListener |
| @@ -424,7 +424,7 @@ class MobileScanner( | @@ -424,7 +424,7 @@ class MobileScanner( | ||
| 424 | /** | 424 | /** |
| 425 | * Analyze a single image. | 425 | * Analyze a single image. |
| 426 | */ | 426 | */ |
| 427 | - fun analyzeImage(image: Uri, analyzerCallback: AnalyzerCallback) { | 427 | + fun analyzeImage(image: Uri, onSuccess: AnalyzerSuccessCallback, onError: AnalyzerErrorCallback) { |
| 428 | val inputImage = InputImage.fromFilePath(activity, image) | 428 | val inputImage = InputImage.fromFilePath(activity, image) |
| 429 | 429 | ||
| 430 | scanner.process(inputImage) | 430 | scanner.process(inputImage) |
| @@ -432,15 +432,13 @@ class MobileScanner( | @@ -432,15 +432,13 @@ class MobileScanner( | ||
| 432 | val barcodeMap = barcodes.map { barcode -> barcode.data } | 432 | val barcodeMap = barcodes.map { barcode -> barcode.data } |
| 433 | 433 | ||
| 434 | if (barcodeMap.isNotEmpty()) { | 434 | if (barcodeMap.isNotEmpty()) { |
| 435 | - analyzerCallback(barcodeMap) | 435 | + onSuccess(barcodeMap) |
| 436 | } else { | 436 | } else { |
| 437 | - analyzerCallback(null) | 437 | + onSuccess(null) |
| 438 | } | 438 | } |
| 439 | } | 439 | } |
| 440 | .addOnFailureListener { e -> | 440 | .addOnFailureListener { e -> |
| 441 | - mobileScannerErrorCallback( | ||
| 442 | - e.localizedMessage ?: e.toString() | ||
| 443 | - ) | 441 | + onError(e.localizedMessage ?: e.toString()) |
| 444 | } | 442 | } |
| 445 | } | 443 | } |
| 446 | 444 |
| @@ -3,7 +3,8 @@ package dev.steenbakker.mobile_scanner | @@ -3,7 +3,8 @@ package dev.steenbakker.mobile_scanner | ||
| 3 | import dev.steenbakker.mobile_scanner.objects.MobileScannerStartParameters | 3 | import dev.steenbakker.mobile_scanner.objects.MobileScannerStartParameters |
| 4 | 4 | ||
| 5 | typealias MobileScannerCallback = (barcodes: List<Map<String, Any?>>, image: ByteArray?, width: Int?, height: Int?) -> Unit | 5 | typealias MobileScannerCallback = (barcodes: List<Map<String, Any?>>, image: ByteArray?, width: Int?, height: Int?) -> Unit |
| 6 | -typealias AnalyzerCallback = (barcodes: List<Map<String, Any?>>?) -> Unit | 6 | +typealias AnalyzerErrorCallback = (message: String) -> Unit |
| 7 | +typealias AnalyzerSuccessCallback = (barcodes: List<Map<String, Any?>>?) -> Unit | ||
| 7 | typealias MobileScannerErrorCallback = (error: String) -> Unit | 8 | typealias MobileScannerErrorCallback = (error: String) -> Unit |
| 8 | typealias TorchStateCallback = (state: Int) -> Unit | 9 | typealias TorchStateCallback = (state: Int) -> Unit |
| 9 | typealias ZoomScaleStateCallback = (zoomScale: Double) -> Unit | 10 | typealias ZoomScaleStateCallback = (zoomScale: Double) -> Unit |
| @@ -26,16 +26,19 @@ class MobileScannerHandler( | @@ -26,16 +26,19 @@ class MobileScannerHandler( | ||
| 26 | private val addPermissionListener: (RequestPermissionsResultListener) -> Unit, | 26 | private val addPermissionListener: (RequestPermissionsResultListener) -> Unit, |
| 27 | textureRegistry: TextureRegistry): MethodChannel.MethodCallHandler { | 27 | textureRegistry: TextureRegistry): MethodChannel.MethodCallHandler { |
| 28 | 28 | ||
| 29 | - private val analyzerCallback: AnalyzerCallback = { barcodes: List<Map<String, Any?>>?-> | ||
| 30 | - if (barcodes != null) { | ||
| 31 | - barcodeHandler.publishEvent(mapOf( | ||
| 32 | - "name" to "barcode", | ||
| 33 | - "data" to barcodes | ||
| 34 | - )) | 29 | + private val analyzeImageErrorCallback: AnalyzerErrorCallback = { |
| 30 | + Handler(Looper.getMainLooper()).post { | ||
| 31 | + analyzerResult?.error("MobileScanner", it, null) | ||
| 32 | + analyzerResult = null | ||
| 35 | } | 33 | } |
| 34 | + } | ||
| 36 | 35 | ||
| 36 | + private val analyzeImageSuccessCallback: AnalyzerSuccessCallback = { | ||
| 37 | Handler(Looper.getMainLooper()).post { | 37 | Handler(Looper.getMainLooper()).post { |
| 38 | - analyzerResult?.success(barcodes != null) | 38 | + analyzerResult?.success(mapOf( |
| 39 | + "name" to "barcode", | ||
| 40 | + "data" to it | ||
| 41 | + )) | ||
| 39 | analyzerResult = null | 42 | analyzerResult = null |
| 40 | } | 43 | } |
| 41 | } | 44 | } |
| @@ -236,7 +239,8 @@ class MobileScannerHandler( | @@ -236,7 +239,8 @@ class MobileScannerHandler( | ||
| 236 | private fun analyzeImage(call: MethodCall, result: MethodChannel.Result) { | 239 | private fun analyzeImage(call: MethodCall, result: MethodChannel.Result) { |
| 237 | analyzerResult = result | 240 | analyzerResult = result |
| 238 | val uri = Uri.fromFile(File(call.arguments.toString())) | 241 | val uri = Uri.fromFile(File(call.arguments.toString())) |
| 239 | - mobileScanner!!.analyzeImage(uri, analyzerCallback) | 242 | + |
| 243 | + mobileScanner!!.analyzeImage(uri, analyzeImageSuccessCallback, analyzeImageErrorCallback) | ||
| 240 | } | 244 | } |
| 241 | 245 | ||
| 242 | private fun toggleTorch(call: MethodCall, result: MethodChannel.Result) { | 246 | private fun toggleTorch(call: MethodCall, result: MethodChannel.Result) { |
| @@ -245,12 +245,12 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin { | @@ -245,12 +245,12 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin { | ||
| 245 | return | 245 | return |
| 246 | } | 246 | } |
| 247 | 247 | ||
| 248 | - mobileScanner.analyzeImage(image: uiImage!, position: AVCaptureDevice.Position.back, callback: { [self] barcodes, error in | 248 | + mobileScanner.analyzeImage(image: uiImage!, position: AVCaptureDevice.Position.back, callback: { barcodes, error in |
| 249 | if error != nil { | 249 | if error != nil { |
| 250 | - barcodeHandler.publishEvent(["name": "error", "message": error?.localizedDescription]) | ||
| 251 | - | ||
| 252 | DispatchQueue.main.async { | 250 | DispatchQueue.main.async { |
| 253 | - result(false) | 251 | + result(FlutterError(code: "MobileScanner", |
| 252 | + message: error?.localizedDescription, | ||
| 253 | + details: nil)) | ||
| 254 | } | 254 | } |
| 255 | 255 | ||
| 256 | return | 256 | return |
| @@ -258,15 +258,13 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin { | @@ -258,15 +258,13 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin { | ||
| 258 | 258 | ||
| 259 | if (barcodes == nil || barcodes!.isEmpty) { | 259 | if (barcodes == nil || barcodes!.isEmpty) { |
| 260 | DispatchQueue.main.async { | 260 | DispatchQueue.main.async { |
| 261 | - result(false) | 261 | + result(nil) |
| 262 | } | 262 | } |
| 263 | } else { | 263 | } else { |
| 264 | let barcodesMap: [Any?] = barcodes!.compactMap { barcode in barcode.data } | 264 | let barcodesMap: [Any?] = barcodes!.compactMap { barcode in barcode.data } |
| 265 | - let event: [String: Any?] = ["name": "barcode", "data": barcodesMap] | ||
| 266 | - barcodeHandler.publishEvent(event) | ||
| 267 | 265 | ||
| 268 | DispatchQueue.main.async { | 266 | DispatchQueue.main.async { |
| 269 | - result(true) | 267 | + result(["name": "barcode", "data": barcodesMap]) |
| 270 | } | 268 | } |
| 271 | } | 269 | } |
| 272 | }) | 270 | }) |
-
Please register or login to post a comment