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,21 +164,30 @@ class MethodChannelMobileScanner extends MobileScannerPlatform { @@ -164,21 +164,30 @@ 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 - final Map<Object?, Object?>? result =  
168 - await methodChannel.invokeMapMethod<Object?, Object?>(  
169 - 'analyzeImage',  
170 - {  
171 - 'filePath': path,  
172 - 'formats': formats.isEmpty  
173 - ? null  
174 - : [  
175 - for (final BarcodeFormat format in formats)  
176 - if (format != BarcodeFormat.unknown) format.rawValue,  
177 - ],  
178 - },  
179 - ); 167 + try {
  168 + final Map<Object?, Object?>? result =
  169 + await methodChannel.invokeMapMethod<Object?, Object?>(
  170 + 'analyzeImage',
  171 + {
  172 + 'filePath': path,
  173 + 'formats': formats.isEmpty
  174 + ? null
  175 + : [
  176 + for (final BarcodeFormat format in formats)
  177 + if (format != BarcodeFormat.unknown) format.rawValue,
  178 + ],
  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 }