Skip to content

[camera_android_camerax] Fix camera preview rotation for landscape oriented devices #9097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
May 8, 2025

Conversation

camsim99
Copy link
Contributor

@camsim99 camsim99 commented Apr 16, 2025

Fixes the incorrect camera preview rotation on landscape-oriented devices (like some tablets). Really, this PR generalizes the fix added in #8629 for correcting the camera preview rotation by correcting the camera preview rotation according to the rotation from the natural device orientation instead of assuming the device is naturally oriented in portrait up.

There are technically two different fixes in this PR; here's the breakdown for clarity:

  1. For devices using the SurfaceTexture Impeller backend: Rotate the camera preview according to the current default display rotation (the rotation away from the natural device orientation) and ignore rotation added in CameraPreview widget completely (as it assumes the natural device orientation to be portrait up).
  2. For devices using the ImageReader Impeller backend: Rotate the camera preview according to https://developer.android.com/media/camera/camera2/camera-preview#orientation_calculation, where deviceOrientationDegrees comes from a call to get the current default display rotation (the rotation away from the natural device orientation) instead of assuming it to be the rotation degrees from portrait up.

Also, fixes a potential regression that @bparrishMines discovered in the DeviceOrientationManager. The changes there essentially ensure that it resets its state every time it is "re-started" to listen into changes into device orientation changes.

Fixes flutter/flutter#164493.*

*Seems to work on all physical devices I test, but not emulators (tried Pixel Tablet API 28, 35). I think that is an emulator bug? Otherwise tested on a new Pixel Tablet (API 34), Pixel Tablet C (API 12), Pixel 3A (phone, API 32).

Pre-Review Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. 2 3

@camsim99 camsim99 marked this pull request as ready for review May 6, 2025 23:20
@camsim99 camsim99 requested review from bparrishMines and a team May 6, 2025 23:25
Copy link
Contributor

@bparrishMines bparrishMines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a few comments


/// Returns the clockwise quarter turns applied by the CameraPreview widget
/// based on [orientation], the current device orientation (see
/// camera/camera/lib/src/camera_preview.dart).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm slightly concerned that the rotation in this plugin depending on the rotation of the app-facing package. This comment doesn't block this PR, but in a future implementation of the entire camera package structure, each platform should probably handle its own rotations and the app facing shouldn't make any assumptions.

Copy link
Contributor Author

@camsim99 camsim99 May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. It does seem like Android is the only platform currently where rotation needs to be handled beyond the app-facing CameraPreview widget as of today, but nonetheless, I agree that organizationally, it should be handled within each platform implementation as a result.

camsim99 and others added 4 commits May 7, 2025 12:36
Co-authored-by: Maurice Parrish <10687576+bparrishMines@users.noreply.github.com>
Co-authored-by: Maurice Parrish <10687576+bparrishMines@users.noreply.github.com>
…tated_preview.dart

Co-authored-by: Maurice Parrish <10687576+bparrishMines@users.noreply.github.com>
…_delegate.dart

Co-authored-by: Maurice Parrish <10687576+bparrishMines@users.noreply.github.com>
@camsim99
Copy link
Contributor Author

camsim99 commented May 8, 2025

@reidbaker do you want to take a look before merging?

@reidbaker
Copy link
Contributor

yes looking now

@camsim99 camsim99 added the autosubmit Merge PR when tree becomes green via auto submit App label May 8, 2025
@auto-submit auto-submit bot merged commit cfdb2ec into flutter:main May 8, 2025
82 checks passed
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 9, 2025
github-merge-queue bot pushed a commit to flutter/flutter that referenced this pull request May 9, 2025
flutter/packages@ab44c26...7814fab

2025-05-08 40433176+KevynSM@users.noreply.github.com [go_router] Fix
requestFocus propagation to Navigator (flutter/packages#9177)
2025-05-08 65381000+raju8000@users.noreply.github.com
[go_router_builder] Fixes an deprecated warning for using
withNullability (flutter/packages#9158)
2025-05-08 engine-flutter-autoroll@skia.org Roll Flutter from
b0f5c8c to 02d8c1a (19 revisions) (flutter/packages#9230)
2025-05-08 43054281+camsim99@users.noreply.github.com
[camera_android_camerax] Fix camera preview rotation for landscape
oriented devices (flutter/packages#9097)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC flutter-ecosystem@google.com on the revert to ensure that a
human
is aware of the problem.

To file a bug in Flutter:
https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
FMorschel pushed a commit to FMorschel/packages that referenced this pull request Jun 9, 2025
…iented devices (flutter#9097)

Fixes the incorrect camera preview rotation on landscape-oriented devices (like some tablets). Really, this PR generalizes the fix added in flutter#8629 for correcting the camera preview rotation by correcting the camera preview rotation according to the rotation from the natural device orientation instead of assuming the device is naturally oriented in portrait up.

There are technically two different fixes in this PR; here's the breakdown for clarity:

1. For devices using the `SurfaceTexture` Impeller backend: Rotate the camera preview according to the current default display rotation (the rotation away from the natural device orientation) and ignore rotation added in `CameraPreview` widget completely (as it assumes the natural device orientation to be portrait up).
2. For devices using the `ImageReader` Impeller backend: Rotate the camera preview according to https://developer.android.com/media/camera/camera2/camera-preview#orientation_calculation, where `deviceOrientationDegrees` comes from [a call](https://developer.android.com/reference/android/view/Display?_gl=1*lysjx*_up*MQ..*_ga*NjY3Nzk0Mzg2LjE3NDU5NjY3ODc.*_ga_6HH9YJMN9M*MTc0NTk2Njc4Ni4xLjAuMTc0NTk2Njc4Ni4wLjAuMjEwMTI3NTg1Mg..#getRotation()) to get the current default display rotation (the rotation away from the natural device orientation) instead of assuming it to be the rotation degrees from portrait up.

Also, fixes a potential regression that @bparrishMines discovered in the `DeviceOrientationManager`. The changes there essentially ensure that it resets its state every time it is "re-started" to listen into changes into device orientation changes.

Fixes flutter/flutter#164493

*Seems to work on all physical devices I test, but not emulators (tried Pixel Tablet API 28, 35). I think that is an emulator bug? Otherwise tested on a new Pixel Tablet (API 34), Pixel Tablet C (API 12), Pixel 3A (phone, API 32).

## Pre-Review Checklist

[^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App p: camera platform-android
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[camera_android_camerax] Preview rotation is incorrect for naturally landscape-oriented devices
3 participants