Skip to content

Fix ViewPort and PreviewDisplay aspect ratio on landscape-natural devices - #592

Open
davidjiagoogle wants to merge 5 commits into
mainfrom
david/videoStabilizationCropFix
Open

davidjiagoogle wants to merge 5 commits into
mainfrom
david/videoStabilizationCropFix

Conversation

@davidjiagoogle

@davidjiagoogle davidjiagoogle commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

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) causes ViewPort and PreviewDisplay to crop landscape camera streams into vertical portrait strips (e.g., cropping 1920x1080 down to 608x1080 and 1600x1200 down to 900x1200).

Changes

  1. CameraSession.kt (createUseCaseGroup, getViewPortAspectRatio):
    • Extract getViewPortAspectRatio to orient the Rational passed to ViewPort.Builder based on whether the sensor output after applying previewUseCase.targetRotation is landscape ((sensorRotationDegrees % 180 == 0) == (sensorRect.width() >= sensorRect.height())), using Rational(aspectRatio.denominator, aspectRatio.numerator) when landscape and Rational(aspectRatio.numerator, aspectRatio.denominator) when portrait.
  2. CaptureScreenComponents.kt (PreviewDisplay):
    • Use aspectRatio.toLandscapeFloat() when the window is landscape (maxWidth > maxHeight) instead of unconditionally using aspectRatio.toFloat().
  3. CameraSessionTest.kt & PreviewDisplayTest.kt:
    • Add Robolectric unit tests verifying getViewPortAspectRatio across landscape/portrait sensors and rotations (0, 90, 180, 270) and PreviewDisplay sizing across portrait and landscape windows.

Test Plan

  • ./gradlew spotlessCheck :core:camera:testStableDebugUnitTest :ui:components:capture:testDebugUnitTest compileDebugSources --init-script gradle/init.gradle.kts

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant