Skip to content

[Android] CameraX preview is rotated 90 degrees #154241

Description

@jellynoone

What package does this bug report belong to?

camera

What target platforms are you seeing this bug on?

Android

Have you already upgraded your packages?

Yes

Dependency versions

pubspec.yaml
name: camera_test
description: "A new Flutter project."
publish_to: 'none'
version: 0.1.0

environment:
  sdk: ^3.5.1

dependencies:
  flutter:
    sdk: flutter
  camera: ^0.11.0+2

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^4.0.0

flutter:
  uses-material-design: true
pubspec.lock
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
  async:
    dependency: transitive
    description:
      name: async
      sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c"
      url: "https://pub.dev"
    source: hosted
    version: "2.11.0"
  boolean_selector:
    dependency: transitive
    description:
      name: boolean_selector
      sha256: "6cfb5af12253eaf2b368f07bacc5a80d1301a071c73360d746b7f2e32d762c66"
      url: "https://pub.dev"
    source: hosted
    version: "2.1.1"
  camera:
    dependency: "direct main"
    description:
      name: camera
      sha256: "26ff41045772153f222ffffecba711a206f670f5834d40ebf5eed3811692f167"
      url: "https://pub.dev"
    source: hosted
    version: "0.11.0+2"
  camera_android_camerax:
    dependency: transitive
    description:
      name: camera_android_camerax
      sha256: "7cd93578ad201dcc6bb5810451fb00d76a86bab9b68dceb68b8cbd7038ac5846"
      url: "https://pub.dev"
    source: hosted
    version: "0.6.8+3"
  camera_avfoundation:
    dependency: transitive
    description:
      name: camera_avfoundation
      sha256: "7c28969a975a7eb2349bc2cb2dfe3ad218a33dba9968ecfb181ce08c87486655"
      url: "https://pub.dev"
    source: hosted
    version: "0.9.17+3"
  camera_platform_interface:
    dependency: transitive
    description:
      name: camera_platform_interface
      sha256: b3ede1f171532e0d83111fe0980b46d17f1aa9788a07a2fbed07366bbdbb9061
      url: "https://pub.dev"
    source: hosted
    version: "2.8.0"
  camera_web:
    dependency: transitive
    description:
      name: camera_web
      sha256: "595f28c89d1fb62d77c73c633193755b781c6d2e0ebcd8dc25b763b514e6ba8f"
      url: "https://pub.dev"
    source: hosted
    version: "0.3.5"
  characters:
    dependency: transitive
    description:
      name: characters
      sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605"
      url: "https://pub.dev"
    source: hosted
    version: "1.3.0"
  clock:
    dependency: transitive
    description:
      name: clock
      sha256: cb6d7f03e1de671e34607e909a7213e31d7752be4fb66a86d29fe1eb14bfb5cf
      url: "https://pub.dev"
    source: hosted
    version: "1.1.1"
  collection:
    dependency: transitive
    description:
      name: collection
      sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
      url: "https://pub.dev"
    source: hosted
    version: "1.18.0"
  cross_file:
    dependency: transitive
    description:
      name: cross_file
      sha256: "7caf6a750a0c04effbb52a676dce9a4a592e10ad35c34d6d2d0e4811160d5670"
      url: "https://pub.dev"
    source: hosted
    version: "0.3.4+2"
  fake_async:
    dependency: transitive
    description:
      name: fake_async
      sha256: "511392330127add0b769b75a987850d136345d9227c6b94c96a04cf4a391bf78"
      url: "https://pub.dev"
    source: hosted
    version: "1.3.1"
  flutter:
    dependency: "direct main"
    description: flutter
    source: sdk
    version: "0.0.0"
  flutter_lints:
    dependency: "direct dev"
    description:
      name: flutter_lints
      sha256: "3f41d009ba7172d5ff9be5f6e6e6abb4300e263aab8866d2a0842ed2a70f8f0c"
      url: "https://pub.dev"
    source: hosted
    version: "4.0.0"
  flutter_plugin_android_lifecycle:
    dependency: transitive
    description:
      name: flutter_plugin_android_lifecycle
      sha256: "9ee02950848f61c4129af3d6ec84a1cfc0e47931abc746b03e7a3bc3e8ff6eda"
      url: "https://pub.dev"
    source: hosted
    version: "2.0.22"
  flutter_test:
    dependency: "direct dev"
    description: flutter
    source: sdk
    version: "0.0.0"
  flutter_web_plugins:
    dependency: transitive
    description: flutter
    source: sdk
    version: "0.0.0"
  leak_tracker:
    dependency: transitive
    description:
      name: leak_tracker
      sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05"
      url: "https://pub.dev"
    source: hosted
    version: "10.0.5"
  leak_tracker_flutter_testing:
    dependency: transitive
    description:
      name: leak_tracker_flutter_testing
      sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806"
      url: "https://pub.dev"
    source: hosted
    version: "3.0.5"
  leak_tracker_testing:
    dependency: transitive
    description:
      name: leak_tracker_testing
      sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3"
      url: "https://pub.dev"
    source: hosted
    version: "3.0.1"
  lints:
    dependency: transitive
    description:
      name: lints
      sha256: "976c774dd944a42e83e2467f4cc670daef7eed6295b10b36ae8c85bcbf828235"
      url: "https://pub.dev"
    source: hosted
    version: "4.0.0"
  matcher:
    dependency: transitive
    description:
      name: matcher
      sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb
      url: "https://pub.dev"
    source: hosted
    version: "0.12.16+1"
  material_color_utilities:
    dependency: transitive
    description:
      name: material_color_utilities
      sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec
      url: "https://pub.dev"
    source: hosted
    version: "0.11.1"
  meta:
    dependency: transitive
    description:
      name: meta
      sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7
      url: "https://pub.dev"
    source: hosted
    version: "1.15.0"
  path:
    dependency: transitive
    description:
      name: path
      sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af"
      url: "https://pub.dev"
    source: hosted
    version: "1.9.0"
  plugin_platform_interface:
    dependency: transitive
    description:
      name: plugin_platform_interface
      sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02"
      url: "https://pub.dev"
    source: hosted
    version: "2.1.8"
  sky_engine:
    dependency: transitive
    description: flutter
    source: sdk
    version: "0.0.99"
  source_span:
    dependency: transitive
    description:
      name: source_span
      sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c"
      url: "https://pub.dev"
    source: hosted
    version: "1.10.0"
  stack_trace:
    dependency: transitive
    description:
      name: stack_trace
      sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b"
      url: "https://pub.dev"
    source: hosted
    version: "1.11.1"
  stream_channel:
    dependency: transitive
    description:
      name: stream_channel
      sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7
      url: "https://pub.dev"
    source: hosted
    version: "2.1.2"
  stream_transform:
    dependency: transitive
    description:
      name: stream_transform
      sha256: "14a00e794c7c11aa145a170587321aedce29769c08d7f58b1d141da75e3b1c6f"
      url: "https://pub.dev"
    source: hosted
    version: "2.1.0"
  string_scanner:
    dependency: transitive
    description:
      name: string_scanner
      sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde"
      url: "https://pub.dev"
    source: hosted
    version: "1.2.0"
  term_glyph:
    dependency: transitive
    description:
      name: term_glyph
      sha256: a29248a84fbb7c79282b40b8c72a1209db169a2e0542bce341da992fe1bc7e84
      url: "https://pub.dev"
    source: hosted
    version: "1.2.1"
  test_api:
    dependency: transitive
    description:
      name: test_api
      sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb"
      url: "https://pub.dev"
    source: hosted
    version: "0.7.2"
  vector_math:
    dependency: transitive
    description:
      name: vector_math
      sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803"
      url: "https://pub.dev"
    source: hosted
    version: "2.1.4"
  vm_service:
    dependency: transitive
    description:
      name: vm_service
      sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d"
      url: "https://pub.dev"
    source: hosted
    version: "14.2.5"
  web:
    dependency: transitive
    description:
      name: web
      sha256: d43c1d6b787bf0afad444700ae7f4db8827f701bc61c255ac8d328c6f4d52062
      url: "https://pub.dev"
    source: hosted
    version: "1.0.0"
sdks:
  dart: ">=3.5.1 <4.0.0"
  flutter: ">=3.24.0"

Steps to reproduce

  1. Open the application
  2. Confirm camera permissions
  3. See improperly rotated preview

Expected results

The camera preview should be properly rotated.

Actual results

The camera preview is rotated 90º.

Code sample

Code sample
import 'package:camera/camera.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

void main() async {
  runApp(const MainApp());
}

class MainApp extends StatefulWidget {
  const MainApp({super.key});

  @override
  State<MainApp> createState() => _MainAppState();
}

class _MainAppState extends State<MainApp> {
  final _runner = _Runner();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: ValueListenableBuilder(
          valueListenable: _runner,
          builder: _build,
        ),
      ),
    );
  }

  Widget _build(BuildContext context, _State value, Widget? child) {
    switch (value) {
      case _Loaded():
        return Center(
          child: CameraPreview(value.controller),
        );

      case _Simple.initializing:
        return const Center(
          child: CircularProgressIndicator(),
        );

      case _Simple.error:
        return const Center(
          child: Icon(Icons.error),
        );
    }
  }
}

class _Runner with ChangeNotifier implements ValueListenable<_State> {
  _Runner() {
    _init();
  }

  void _init() async {
    final available = await availableCameras();
    if (available.isEmpty) {
      _emit(_Simple.error);
    } else {
      final controller =
          CameraController(available.first, ResolutionPreset.veryHigh);
      try {
        await controller.initialize();
        _emit(_Loaded(
          controller: controller,
          available: available,
        ));
      } catch (e) {
        _emit(_Simple.error);
      }
    }
  }

  _State _value = _Simple.initializing;
  @override
  _State get value => _value;
  void _emit(_State state) {
    _value = state;
    notifyListeners();
  }
}

sealed class _State {}

enum _Simple implements _State {
  initializing,
  error,
}

class _Loaded implements _State {
  final CameraController controller;
  final List<CameraDescription> available;

  const _Loaded({
    required this.controller,
    required this.available,
  });
}

Screenshots or Videos

No response

Logs

No response

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.24.1, on macOS 14.6.1 23G93 darwin-arm64, locale en-SI)
    • Flutter version 3.24.1 on channel stable at /Users/.../Development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5874a72aa4 (8 days ago), 2024-08-20 16:46:00 -0500
    • Engine revision c9b9d5780d
    • Dart version 3.5.1
    • DevTools version 2.37.2

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/.../Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11609105)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15F31d
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11609105)

[✓] VS Code (version 1.93.0-insider)
    • VS Code at /Applications/Visual Studio Code - Insiders.app/Contents
    • Flutter extension version 3.94.0

[✓] Connected device (4 available)
    • Pixel (mobile)                  • FA76R0301797          • android-arm64  • Android 10 (API 29)
    • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 14.6.1 23G93 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 14.6.1 23G93 darwin-arm64
    • Chrome (web)                    • chrome                • web-javascript • Google Chrome 127.0.6533.122

[✓] Network resources
    • All expected network resources are available.

• No issues found!

Additional:
This issue reproduces on a Pixel 1 device. I wasn't able to test this on a more capable device.

The issue first occurs with the camera_android_camerax: 0.6.8+2 if I override the dependency to: camera_android_camerax: 0.6.7+2 everything works as expected.

Activity

  1. jellynoone commented on Aug 28, 2024

    @jellynoone
    ContributorAuthor

    An additional note, (while it is probably unrelated to this issue).

    Currently, camera_androids CameraPlatform.buildPreview implementation uses a Texture directly, a single widget.
    https://github.com/flutter/packages/blob/cc7b5fc79cd389bd02dba3dc7ee956761103b8b9/packages/camera/camera_android/lib/src/android_camera.dart#L536-L539

    While camera_android_camerax does some additional preview shifting.
    https://github.com/flutter/packages/blob/cc7b5fc79cd389bd02dba3dc7ee956761103b8b9/packages/camera/camera_android_camerax/lib/src/android_camera_camerax.dart#L856-L930

    The concerning bit of potential mistakes is that the way to preview the camera is by using package:camera/camera.dart#CameraPreview which properly prepares the preview and further rotates it on Android platforms.

    Are both plugin implementations aware of this? Or would it make more sense to have all plugins handle any potential rotation requirements on their own and the user would expect that CameraPlatform.buildPreview always returns the preview in the proper rotation without the need for additional manual corrections?

  2. darshankawar commented on Aug 28, 2024

    @darshankawar
    Member

    @jellynoone
    This seems similar to #149294 which has been fixed and merged into main, may not be available in stable yet, so try to switch to latest main and re-run your scenario to confirm.
    Will close as duplicate for now. If you disagree, write in comments and I'll reopen it.

  3. added
    r: duplicateIssue is closed as a duplicate of an existing issue
    and removed
    in triagePresently being triaged by the triage team
    on Aug 28, 2024
  4. jellynoone commented on Aug 28, 2024

    @jellynoone
    ContributorAuthor

    Thank you for a quick response.

    As far as I can tell, the pull request to the issue you linked was made available in version 0.6.8+1.
    The issue reproduces for me on versions after 0.6.7+2.

    Please note, this issue is produced after the fix while running the regular skia engine.

    The fix was meant for Impeller right? Which doesn't seem like the same issue.

    I suppose the fix for Impeller caused a regression on Android devices running the regular skia engine.
    I also tried running the app with impeller enabled. Same results.
    Pixel 1 doesn't seem to support it.

  5. jellynoone commented on Aug 28, 2024

    @jellynoone
    ContributorAuthor

    @darshankawar Can you please reopen?

  6. darshankawar commented on Aug 28, 2024

    @darshankawar
    Member

    Thanks for feedback. Reopening for further triage.

  7. added
    in triagePresently being triaged by the triage team
    and removed
    r: duplicateIssue is closed as a duplicate of an existing issue
    on Aug 28, 2024
  8. darshankawar commented on Aug 29, 2024

    @darshankawar
    Member

    @jellynoone
    Although we don't have Pixel 1 device, but we tried on Pixel 7 running Android 14 in which we didn't observe the rotated preview:

    This probably indicates it could be a device specific issue, but can you try on other devices (not pixel) to check if it persists still ?

  9. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Aug 29, 2024
  10. jellynoone commented on Aug 29, 2024

    @jellynoone
    ContributorAuthor

    I checked on an old Samsung (Android 5.1.1, API 22 (still a Flutter supported platform)) and everything worked as expected.

    Regardless, even if this is a device specific issue, it is a regression that happened between 0.6.7+2 and 0.6.8+2.

    I will see if I can reproduce the issue on any other device, however, my options are currently limited.

  11. 191 remaining items

  12. added
    r: fixedIssue is closed as already fixed in a newer version
    on Feb 27, 2025
  13. camsim99 commented on Feb 27, 2025

    @camsim99
    Contributor

    @elliotrtd I saw a comment you left in my email about side effects, but I don't see it here. Did you find a fix? If not, can you open an issue so I can resolve it? Thank you!

  14. kylehayes commented on Feb 27, 2025

    @kylehayes

    @camsim99 Thank you for working on this fix. This definitely is working on my Samsung Tab S9 FE+ device, but still doesn't appear to be working on my MicroTouch display.

    Other than it running Android 13, what else can I provide to help debug what's happening?

    If I understand the underlying issue correctly, it has to do with the reported rotation of the camera?

  15. camsim99 commented on Feb 27, 2025

    @camsim99
    Contributor

    @kylehayes It likely has to do with how your device provides Surfaces to the camera plugin. It's possible that the API we use to detect if your device needs the preview rotation correction is not reporting the right value for the MicroTouch display.

    I haven't seen this on any of my devices, so it would be difficult for me to debug, but if you're willing to help, you could run a local version of the camera_android_camerax plugin and play around with the value of _handlesCropAndRotation here: https://github.com/flutter/packages/blob/main/packages/camera/camera_android_camerax/lib/src/android_camera_camerax.dart#L847. I wonder if we are building one preview Widget on that device when we should building another. We'd expect _handlesCropAndRotation to be false on Android 13 devices, for context, but maybe something that we don't expect is happening.

    If you need help doing that, let me know!

  16. kylehayes commented on Feb 27, 2025

    @kylehayes

    Sounds great, @camsim99—thank you for explaining that. I'll hopefully get around to looking at this tomorrow or early next week.

  17. kylehayes commented on Feb 28, 2025

    @kylehayes

    Alright @camsim99 I was able to debug this today on the device and found some interesting things. _handesCropAndRotation is indeed false which sounds like a good thing from your POV.

    When constructing the RotatedPreview.frontFacingCamera, here are the reported values:
    _initialDeviceOrientation = DeviceOrientation.landscapeLeft
    sensorOrientationDegrees = 0.0

    As I step through we end up in the _wrapInRotatedBox method of camera_preview and the RotatedBox gets used and then calls _getQuarterTurns() which returns 3 for landscapeLeft. However, a quarter turn of 2 is what sets the preview rotation correctly for this device.

  18. camsim99 commented on Feb 28, 2025

    @camsim99
    Contributor

    @kylehayes thank you for looking into that! Is it correct when you set _handesCropAndRotation to true?

    If not, I think this is because that device is landscape-oriented, which actually has been broken for camera_android for sometime and I believe camera_android_camerax since it was built, and I'll need to debug that further.

    As I step through we end up in the _wrapInRotatedBox method of camera_preview and the RotatedBox gets used and then calls _getQuarterTurns() which returns 3 for landscapeLeft. However, a quarter turn of 2 is what sets the preview rotation correctly for this device.

    We apply an additional rotation on top of that one when RotatedPreview is used via another RotatedBox BTW, but in this line of thinking, if setting _handesCropAndRotation to true does not work, it must be because we are assuming the device is naturally portrait up in our logic somewhere.

  19. kylehayes commented on Feb 28, 2025

    @kylehayes

    @camsim99 it is not correct if I override _handlesCropAndRotation to true.

    I bet you're right about this always being a landscape device as its a 22" touch screen computer, meant for commercial/industrial installations.

  20. camsim99 commented on Mar 1, 2025

    @camsim99
    Contributor

    @kylehayes Ah gotcha. Thank you so much for helping debug! :) I'll file an issue for landscape oriented devices and we can follow up there; I'll ping you when I do.

  21. elliotrtd commented on Mar 1, 2025

    @elliotrtd

    @camsim99 I realised it wasn't necessarily something to fix - but maybe you want to think about later on - certainly not related here.

    Quick summary - When I was testing rotating the display whilst the device was set to locked rotation it comes up with a button in the bottom left corner to force it to rotate. My button for the flash was just below this so the flash was firing at the same time as the rotation which threw an error because the camera was not ready.

  22. camsim99 commented on Mar 3, 2025

    @camsim99
    Contributor

    @elliotrtd Interesting :/ if you want us to take a deeper look, can you file a separate issue?

  23. elliotrtd commented on Mar 4, 2025

    @elliotrtd

    @camsim99 I will do some more testing in the coming days - it seems to me a bit more of a unique problem to my setup than a flaw in the package. it's also not strictly related to this issue, I thought it was initially the rotation not being handled until I saw the flash reference, hence I deleted my original comment.

    If I think it's something could / should be handled better I'll file a separate issue no worries.

    In good news - the customer who reported the original issue has confirmed that the new fix works correctly for them without my manual fix which I previously applied so I would confirm this is solved :)

  24. github-actions commented on Mar 18, 2025

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  25. locked as resolved and limited conversation to collaborators on Mar 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work liste: device-specificOnly manifests on certain devicesp: cameraThe camera pluginpackageflutter/packages repository. See also p: labels.platform-androidAndroid applications specificallyr: fixedIssue is closed as already fixed in a newer versionteam-androidOwned by Android platform team

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions