[firebase_ml_vision] Example app not reading barcodes on Android
Describe the bug
When navigating to the CameraPreviewScanner and selecting Detect Barcode from the header select menu, the example app fails to detect barcodes accurately on Android.
To Reproduce Steps to reproduce the behavior:
- In the example app, go to
CameraPreviewScanner - Select
Detect Barcodefrom the header select menu - Point device at barcode
- See error or incorrect behavior
Expected behavior I would expect to see a green box painted around the detected barcode. This happens on iOS, but not on Android (see screenshots below):
| iOS | Android |
|---|---|
![]() |
![]() |
Flutter doctor
Run flutter doctor and paste the output below:
[✓] Flutter (Channel stable, 1.20.2, on Mac OS X 10.15.6 19G73, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 11.6)
[✓] Android Studio (version 4.0)
[✓] VS Code (version 1.48.0)
[✓] Connected device (2 available)
• No issues found!
Issue replicable and test on Android 8.1.0 device. Trying to scan a qr code doesn't highlight it.

code sample : https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_ml_vision/example
flutter doctor -v
Flutter 1.20.2 • channel stable • https://github.com/flutter/flutter.git
Framework • revision bbfbf1770c (15 hours ago) • 2020-08-13 08:33:09 -0700
Engine • revision 9d5b21729f
Tools • Dart 2.9.1
Running flutter doctor...
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 1.20.2, on Mac OS X 10.15.2 19C57, locale en-US)
[!] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
✗ Android license status unknown.
Try re-installing or updating your Android SDK Manager.
See https://developer.android.com/studio/#downloads or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions.
[✓] Xcode - develop for iOS and macOS (Xcode 11.6)
[✓] Android Studio (version 3.6)
[✓] Connected device (1 available)
! Doctor found issues in 1 category.
Any suggested workarounds to this?
Any update on this please?
While testing the example app, I noticed that the example MaterialBarcodeScanner is not taking into account the sensorOrientation while processing the image frame from the camera stream.
If I understand correctly, the Android CameraX documentation mentions that the image would not be in the same orientation as the device's natural display orientation. This would require us to flip the dimensions of the image accordingly, on Android only, based on the sensorOrientation as explained here
If you look at this log below, my phone is in portrait mode but the dimension reported is Size(640, 480). It seems to me that this dimension is based on the orientation of the camera sensor, and we would need to flip it to be Size(480, 640) if the sensorOrientation is either 90 or 270.
flutter: Sensor orientation: 90
flutter: Image size: Size(640.0, 480.0)
flutter: Media query size: Size(375.0, 667.0)
flutter: Max logical height: 667.0
flutter: Image height: 480.0
flutter: Image scale: 0.719640179910045
flutter: Half width: 100.74962518740631
flutter: Half height: 100.74962518740631
flutter: Center: Offset(320.0, 240.0)
flutter: Valid rect: Rect.fromLTRB(219.3, 139.3, 420.7, 340.7)
On iOS, this is not an issue at all, and the reported dimension of the image matches the device orientation regardless of the sensorOrientation. I assume this is due to the usage of UIImage, which handles the rotation automatically for us.
Please let me know if any of my assumption and finding above is incorrect. I'm open to learning more about the cause of this issue.
@luandnguyen I think you're on to something. My own testing indicates that the CameraImage image frame (generated by the official flutter camera plugin) does not report its width and height correctly on Android (works fine on iOS). This in turn causes the barcode scanner to incorrectly highlight the QR code (as described above).
For example, if the ResolutionPreset.high preset is used when creating the CameraController (resulting in portrait image frame dimensions of 720x1280), the actual reported dimensions of each image frame on Android is 1280x720.
So it seems to me that the issue actually lies with the camera plugin that generates the image frames, not with the firebase_ml_vision plugin. @luandnguyen Have you come to the same conclusion, or am I missing something?
@philiplindberg I'm not sure if this is a camera plugin issue; it depends on the expectation outlined by the CameraX or Camera class on Android. From the documentation of CameraX I linked above, it looks to me like the "wrong" dimension is the expected result of the camera on Android. For now, I'm just checking for Android platform and flipping the dimension based on the sensorOrientation. If any OEM (such as Samsung, Huawei, etc.) decides this is not the case, then I guess I would need to check for each OEM and handle the expected behavior accordingly. Hope this helps!
any update on this please?
any fix to this?
Hey, :wave:, the firebase_ml_vision package is now discontinued since its APIs have been deprecated and removed from the Android & iOS Firebase SDKs.
I'd recommend switching to the alternatives now;
- for on-device vision APIs; switch to Google's standalone ML Kit library via google_ml_kit
- for cloud vision APIs; the recommended approach now is to use Firebase Authentication and Functions, which gives you a managed, serverless gateway to the Google Cloud Vision APIs. For an example Functions project see the vision-annotate-images sample project. You'd deploy this project for example and then call the cloud function from your app with your image data to get the annotation result.
Apologies for any inconvenience here and best of luck switching over the the new APIs. firebase_ml_custom is not affected by this deprecation.
Thanks

