Navaron Bracke

adjust analyze image to throw barcode exceptions if needed

... ... @@ -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
}
}
... ...
... ... @@ -269,7 +269,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin {
barcodeScannerOptions: scannerOptions, 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))
}
... ...
... ... @@ -164,21 +164,30 @@ class MethodChannelMobileScanner extends MobileScannerPlatform {
String path, {
List<BarcodeFormat> formats = const <BarcodeFormat>[],
}) async {
final Map<Object?, Object?>? result =
await methodChannel.invokeMapMethod<Object?, Object?>(
'analyzeImage',
{
'filePath': path,
'formats': formats.isEmpty
? null
: [
for (final BarcodeFormat format in formats)
if (format != BarcodeFormat.unknown) format.rawValue,
],
},
);
try {
final Map<Object?, Object?>? result =
await methodChannel.invokeMapMethod<Object?, Object?>(
'analyzeImage',
{
'filePath': path,
'formats': formats.isEmpty
? null
: [
for (final BarcodeFormat format in formats)
if (format != BarcodeFormat.unknown) format.rawValue,
],
},
);
return _parseBarcode(result);
return _parseBarcode(result);
} on PlatformException catch (error) {
// Handle any errors from analyze image requests.
if (error.code == kBarcodeErrorEventName) {
throw MobileScannerBarcodeException(error.message);
}
return null;
}
}
@override
... ...
... ... @@ -191,6 +191,9 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> {
/// This is only supported on Android, iOS and MacOS.
///
/// 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);
}
... ...