google_ml_kit_flutter
google_ml_kit_flutter copied to clipboard
iOS crash with "MLKInvalidImage: Input image must not be nil." after upgrading from google_ml_kit 0.7.3 to google_mlkit_barcode_scanning 0.3.0
Using Camera 0.9.8+1
After switching packages from google_ml_kit: ^0.7.3
to google_mlkit_barcode_scanning: ^0.3.0
and changing the parts of the code that broke, mainly:
Replaced
final imageRotation =
InputImageRotationMethods.fromRawValue(camera.sensorOrientation) ??
InputImageRotation.Rotation_0deg;
final inputImageFormat =
InputImageFormatMethods.fromRawValue(image.format.raw) ??
InputImageFormat.NV21;
with
final imageRotation = InputImageRotation.rotation0deg;
final inputImageFormat = InputImageFormat.nv21;
and
barcodeScanner = GoogleMlKit.vision
.barcodeScanner([BarcodeFormat.qrCode, BarcodeFormat.aztec]);
with
barcodeScanner = BarcodeScanner(formats: [BarcodeFormat.qrCode, BarcodeFormat.aztec]);
and
String barcodeRawValue = barcode?.value?.rawValue;
String barcodeFormat = barcode?.value?.format?.name;
with
String barcodeRawValue = barcode?.rawValue;
String barcodeFormat = barcode?.format?.toString();
It works on Android but on iOS I get a crash, with the following exception.
MLKInvalidImage: Input image must not be nil.
I can't figure out what the problem is. There is no migration guide from 0.7.3 to the new version of MLKit, so I have no idea what might be missing now.
Unfortunately the stacktrace from Sentry isn't too informative (it isn't showing any part of my code).
I have the same problem sometimes with TextRecognizer
.
*** Terminating app due to uncaught exception 'MLKInvalidImage', reason: 'Input image must not be nil.'
*** First throw call stack:
(0x1805fa288 0x1992f4744 0x180651390 0x105aec148 0x105b21a9c 0x105b21918 0x105b22b10 0x105b22658 0x10abf8740 0x10a6dfa08 0x18025fe6c 0x180261a30 0x18026ff48 0x18026fb98 0x1805b2800 0x18056c704 0x18057fbc8 0x19c6b3374 0x182eef648 0x182c70d90 0x104658678 0x10732dce4)
libc++abi: terminating with uncaught exception of type NSException
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
frame #0: 0x00000001b74dfb38 libsystem_kernel.dylib`__pthread_kill + 8
libsystem_kernel.dylib`__pthread_kill:
-> 0x1b74dfb38 <+8>: b.lo 0x1b74dfb58 ; <+40>
0x1b74dfb3c <+12>: pacibsp
0x1b74dfb40 <+16>: stp x29, x30, [sp, #-0x10]!
0x1b74dfb44 <+20>: mov x29, sp
Target 0: (Runner) stopped.
Lost connection to device.
@jbx1 : you are hardcoding the values for imageRotation and inputImageFormat, try this:
final imageRotation =
InputImageRotationValue.fromRawValue(camera.sensorOrientation);
if (imageRotation == null) return;
final inputImageFormat =
InputImageFormatValue.fromRawValue(image.format.raw);
if (inputImageFormat == null) return;
@fbernaly We're occasionally seeing the same crash signature in Crashlytics, using the following:
google_mlkit_barcode_scanning: ^0.4.0
google_mlkit_image_labeling: ^0.4.0
google_mlkit_text_recognition: ^0.4.0
It appears to be crashing in ImageLabeling (stack trace below). We also see some related logging before the crash:
Bad state: LocalFile: '/var/mobile/Containers/Data/Application/E00088FD-418C-465F-BD90-EE2D3DEA762E/Documents/img12_1662733618579.jpg' is empty and cannot be loaded as an image.
Stack trace:
Fatal Exception: MLKInvalidImage
Input image must not be nil.
Fatal Exception: MLKInvalidImage
0 CoreFoundation 0x99288 __exceptionPreprocess
1 libobjc.A.dylib 0x16744 objc_exception_throw
2 CoreFoundation 0xf0390 __CFDictionaryCreateGeneric
3 Runner 0x20bf100 -[MLKVisionImage initWithImage:]
4 Runner 0x33e40d4 +[MLKVisionImage(FlutterPlugin) filePathToVisionImage:] + 22 (MLKVisionImage+FlutterPlugin.m:22)
5 Runner 0x33e3fb0 +[MLKVisionImage(FlutterPlugin) visionImageFromData:] + 9 (MLKVisionImage+FlutterPlugin.m:9)
6 Runner 0x33e4c04 -[GoogleMlKitImageLabelingPlugin handleDetection:result:] + 56 (GoogleMlKitImageLabelingPlugin.m:56)
7 Runner 0x33e49f4 -[GoogleMlKitImageLabelingPlugin handleMethodCall:result:] + 39 (GoogleMlKitImageLabelingPlugin.m:39)
8 Flutter 0x55ae50 (Missing UUID c8164c661c033398832ad3cbdb9a3cd8)
9 Flutter 0x42cd0 (Missing UUID c8164c661c033398832ad3cbdb9a3cd8)
10 libdispatch.dylib 0x1e6c _dispatch_call_block_and_release
11 libdispatch.dylib 0x3a30 _dispatch_client_callout
12 libdispatch.dylib 0x11f48 _dispatch_main_queue_drain
13 libdispatch.dylib 0x11b98 _dispatch_main_queue_callback_4CF
14 CoreFoundation 0x51800 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
15 CoreFoundation 0xb704 __CFRunLoopRun
16 CoreFoundation 0x1ebc8 CFRunLoopRunSpecific
17 GraphicsServices 0x1374 GSEventRunModal
18 UIKitCore 0x514b58 -[UIApplication _run]
19 UIKitCore 0x296090 UIApplicationMain
20 Runner 0x7f2c main + 7 (AppDelegate.swift:7)
21 ??? 0x109069da4 (Missing)