Repository navigation
Fix ViewPort and PreviewDisplay aspect ratio on landscape-natural devices - #592
Open
davidjiagoogle wants to merge 5 commits into
Open
davidjiagoogle wants to merge 5 commits into
davidjiagoogle wants to merge 5 commits into
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request adjusts the viewport aspect ratio calculation in CameraSession.kt to account for sensor rotation and dimensions, and updates PreviewDisplay in CaptureScreenComponents.kt to handle landscape orientation correctly. The feedback recommends adding explanatory comments to the non-obvious sensor rotation logic to improve maintainability, and requests adding unit tests to verify the viewport aspect ratio calculations under different sensor orientations and display rotations.
- Extract getViewPortAspectRatio in CameraSession.kt and document how sensor rotation and natural sensor dimensions determine viewport orientation - Add Robolectric unit tests in CameraSessionTest.kt and PreviewDisplayTest.kt covering portrait and landscape sensor/window orientations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes an issue on landscape-natural devices where
android.sensor.orientation = 0(such as tablet/enterprise devices with a landscape natural display and landscape camera sensor) causesViewPortandPreviewDisplayto crop landscape camera streams into vertical portrait strips (e.g., cropping1920x1080down to608x1080and1600x1200down to900x1200).Changes
CameraSession.kt(createUseCaseGroup,getViewPortAspectRatio):getViewPortAspectRatioto orient theRationalpassed toViewPort.Builderbased on whether the sensor output after applyingpreviewUseCase.targetRotationis landscape ((sensorRotationDegrees % 180 == 0) == (sensorRect.width() >= sensorRect.height())), usingRational(aspectRatio.denominator, aspectRatio.numerator)when landscape andRational(aspectRatio.numerator, aspectRatio.denominator)when portrait.CaptureScreenComponents.kt(PreviewDisplay):aspectRatio.toLandscapeFloat()when the window is landscape (maxWidth > maxHeight) instead of unconditionally usingaspectRatio.toFloat().CameraSessionTest.kt&PreviewDisplayTest.kt:getViewPortAspectRatioacross landscape/portrait sensors and rotations (0,90,180,270) andPreviewDisplaysizing across portrait and landscape windows.Test Plan
./gradlew spotlessCheck :core:camera:testStableDebugUnitTest :ui:components:capture:testDebugUnitTest compileDebugSources --init-script gradle/init.gradle.kts