Repository navigation
[Android] CameraX preview is rotated 90 degrees #154241
Description
Activity
An additional note, (while it is probably unrelated to this issue).
Currently,
camera_androidsCameraPlatform.buildPreviewimplementation uses aTexturedirectly, a single widget.
https://github.com/flutter/packages/blob/cc7b5fc79cd389bd02dba3dc7ee956761103b8b9/packages/camera/camera_android/lib/src/android_camera.dart#L536-L539While
camera_android_cameraxdoes some additional preview shifting.
https://github.com/flutter/packages/blob/cc7b5fc79cd389bd02dba3dc7ee956761103b8b9/packages/camera/camera_android_camerax/lib/src/android_camera_camerax.dart#L856-L930The concerning bit of potential mistakes is that the way to preview the camera is by using
package:camera/camera.dart#CameraPreviewwhich 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.buildPreviewalways returns the preview in the proper rotation without the need for additional manual corrections?- addedin triagePresently being triaged by the triage teamPresently being triaged by the triage team
on Aug 28, 2024 @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.- addedr: duplicateIssue is closed as a duplicate of an existing issueIssue is closed as a duplicate of an existing issueand removedin triagePresently being triaged by the triage teamPresently being triaged by the triage team
on Aug 28, 2024 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 after0.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.@darshankawar Can you please reopen?
Thanks for feedback. Reopening for further triage.
- addedin triagePresently being triaged by the triage teamPresently being triaged by the triage teamand removedr: duplicateIssue is closed as a duplicate of an existing issueIssue is closed as a duplicate of an existing issue
on Aug 28, 2024 @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 ?
- addedwaiting for responseThe Flutter team cannot make further progress on this issue until the original reporter respondsThe Flutter team cannot make further progress on this issue until the original reporter responds
on Aug 29, 2024 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+2and0.6.8+2.I will see if I can reproduce the issue on any other device, however, my options are currently limited.
191 remaining items
Load more actions- addedr: fixedIssue is closed as already fixed in a newer versionIssue is closed as already fixed in a newer version
on Feb 27, 2025 @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!
@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?
@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_cameraxplugin and play around with the value of_handlesCropAndRotationhere: 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 previewWidgeton that device when we should building another. We'd expect_handlesCropAndRotationto befalseon Android 13 devices, for context, but maybe something that we don't expect is happening.If you need help doing that, let me know!
Reacted by Kyle HayesSounds great, @camsim99—thank you for explaining that. I'll hopefully get around to looking at this tomorrow or early next week.
Reacted by Camille SimonAlright @camsim99 I was able to debug this today on the device and found some interesting things.
_handesCropAndRotationis indeedfalsewhich sounds like a good thing from your POV.When constructing the
RotatedPreview.frontFacingCamera, here are the reported values:
_initialDeviceOrientation=DeviceOrientation.landscapeLeft
sensorOrientationDegrees=0.0As I step through we end up in the
_wrapInRotatedBoxmethod ofcamera_previewand theRotatedBoxgets used and then calls_getQuarterTurns()which returns3forlandscapeLeft. However, a quarter turn of2is what sets the preview rotation correctly for this device.@kylehayes thank you for looking into that! Is it correct when you set
_handesCropAndRotationto true?If not, I think this is because that device is landscape-oriented, which actually has been broken for
camera_androidfor sometime and I believecamera_android_cameraxsince 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
RotatedPreviewis used via anotherRotatedBoxBTW, but in this line of thinking, if setting_handesCropAndRotationto true does not work, it must be because we are assuming the device is naturally portrait up in our logic somewhere.@camsim99 it is not correct if I override
_handlesCropAndRotationto 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.
@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.
Reacted by Kyle Hayes@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.
@elliotrtd Interesting :/ if you want us to take a deeper look, can you file a separate issue?
@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 :)
Reacted by Camille SimonThis 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 -vand a minimal reproduction of the issue.- locked as resolved and limited conversation to collaborators
on Mar 18, 2025
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
pubspec.lock
Steps to reproduce
Expected results
The camera preview should be properly rotated.
Actual results
The camera preview is rotated 90º.
Code sample
Code sample
Screenshots or Videos
No response
Logs
No response
Flutter Doctor output
Doctor output
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+2if I override the dependency to:camera_android_camerax: 0.6.7+2everything works as expected.