Navaron Bracke

adjust analyze image to throw barcode exceptions if needed

@@ -29,7 +29,7 @@ class MobileScannerHandler( @@ -29,7 +29,7 @@ class MobileScannerHandler(
29 29
30 private val analyzeImageErrorCallback: AnalyzerErrorCallback = { 30 private val analyzeImageErrorCallback: AnalyzerErrorCallback = {
31 Handler(Looper.getMainLooper()).post { 31 Handler(Looper.getMainLooper()).post {
32 - analyzerResult?.error(MobileScannerErrorCodes.GENERIC_ERROR, it, null) 32 + analyzerResult?.error(MobileScannerErrorCodes.BARCODE_ERROR, it, null)
33 analyzerResult = null 33 analyzerResult = null
34 } 34 }
35 } 35 }
@@ -269,7 +269,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin { @@ -269,7 +269,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin {
269 barcodeScannerOptions: scannerOptions, callback: { barcodes, error in 269 barcodeScannerOptions: scannerOptions, callback: { barcodes, error in
270 if error != nil { 270 if error != nil {
271 DispatchQueue.main.async { 271 DispatchQueue.main.async {
272 - result(FlutterError(code: MobileScannerErrorCodes.GENERIC_ERROR, 272 + result(FlutterError(code: MobileScannerErrorCodes.BARCODE_ERROR,
273 message: error?.localizedDescription, 273 message: error?.localizedDescription,
274 details: nil)) 274 details: nil))
275 } 275 }
@@ -164,6 +164,7 @@ class MethodChannelMobileScanner extends MobileScannerPlatform { @@ -164,6 +164,7 @@ class MethodChannelMobileScanner extends MobileScannerPlatform {
164 String path, { 164 String path, {
165 List<BarcodeFormat> formats = const <BarcodeFormat>[], 165 List<BarcodeFormat> formats = const <BarcodeFormat>[],
166 }) async { 166 }) async {
  167 + try {
167 final Map<Object?, Object?>? result = 168 final Map<Object?, Object?>? result =
168 await methodChannel.invokeMapMethod<Object?, Object?>( 169 await methodChannel.invokeMapMethod<Object?, Object?>(
169 'analyzeImage', 170 'analyzeImage',
@@ -179,6 +180,14 @@ class MethodChannelMobileScanner extends MobileScannerPlatform { @@ -179,6 +180,14 @@ class MethodChannelMobileScanner extends MobileScannerPlatform {
179 ); 180 );
180 181
181 return _parseBarcode(result); 182 return _parseBarcode(result);
  183 + } on PlatformException catch (error) {
  184 + // Handle any errors from analyze image requests.
  185 + if (error.code == kBarcodeErrorEventName) {
  186 + throw MobileScannerBarcodeException(error.message);
  187 + }
  188 +
  189 + return null;
  190 + }
182 } 191 }
183 192
184 @override 193 @override
@@ -191,6 +191,9 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> { @@ -191,6 +191,9 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> {
191 /// This is only supported on Android, iOS and MacOS. 191 /// This is only supported on Android, iOS and MacOS.
192 /// 192 ///
193 /// Returns the [BarcodeCapture] that was found in the image. 193 /// Returns the [BarcodeCapture] that was found in the image.
  194 + ///
  195 + /// If an error occurred during the analysis of the image,
  196 + /// a [MobileScannerBarcodeException] error is thrown.
194 Future<BarcodeCapture?> analyzeImage(String path) { 197 Future<BarcodeCapture?> analyzeImage(String path) {
195 return MobileScannerPlatform.instance.analyzeImage(path); 198 return MobileScannerPlatform.instance.analyzeImage(path);
196 } 199 }