Skip to content

Commit

Permalink
adjust analyze image to throw barcode exceptions if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
navaronbracke committed Jun 18, 2024
1 parent b0a99bd commit e48a689
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MobileScannerHandler(

private val analyzeImageErrorCallback: AnalyzerErrorCallback = {
Handler(Looper.getMainLooper()).post {
analyzerResult?.error(MobileScannerErrorCodes.GENERIC_ERROR, it, null)
analyzerResult?.error(MobileScannerErrorCodes.BARCODE_ERROR, it, null)
analyzerResult = null
}
}
Expand Down
2 changes: 1 addition & 1 deletion ios/Classes/MobileScannerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin {
mobileScanner.analyzeImage(image: uiImage!, position: AVCaptureDevice.Position.back, callback: { barcodes, error in
if error != nil {
DispatchQueue.main.async {
result(FlutterError(code: MobileScannerErrorCodes.GENERIC_ERROR,
result(FlutterError(code: MobileScannerErrorCodes.BARCODE_ERROR,
message: error?.localizedDescription,
details: nil))
}
Expand Down
20 changes: 14 additions & 6 deletions lib/src/method_channel/mobile_scanner_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,21 @@ class MethodChannelMobileScanner extends MobileScannerPlatform {

@override
Future<BarcodeCapture?> analyzeImage(String path) async {
final Map<String, Object?>? result =
await methodChannel.invokeMapMethod<String, Object?>(
'analyzeImage',
path,
);
try {
final Map<String, Object?>? result =
await methodChannel.invokeMapMethod<String, Object?>(
'analyzeImage',
path,
);

return _parseBarcode(result);
return _parseBarcode(result);
} on PlatformException catch (exception) {
if (exception.code == kBarcodeErrorEventName) {
throw MobileScannerBarcodeException(exception.message);
}

rethrow;
}
}

@override
Expand Down
3 changes: 3 additions & 0 deletions lib/src/mobile_scanner_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> {
/// This is only supported on Android and iOS.
///
/// Returns the [BarcodeCapture] that was found in the image.
///
/// If an error occurred during the analysis of the image,
/// a [MobileScannerBarcodeException] error is thrown.
Future<BarcodeCapture?> analyzeImage(String path) {
return MobileScannerPlatform.instance.analyzeImage(path);
}
Expand Down

0 comments on commit e48a689

Please sign in to comment.