Skip to content

Commit 6867ba7

Browse files
committed
Add camera preview aspect ratio hint.
This allows the Spatial layout to maintain an arbitrary aspect ratio without any cropping.
1 parent 1e16d5e commit 6867ba7

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

feature/camera/src/main/java/com/android/developers/androidify/camera/CameraLayout.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ internal fun CameraLayout(
7171
guideText: @Composable (modifier: Modifier) -> Unit,
7272
guide: @Composable (modifier: Modifier) -> Unit,
7373
rearCameraButton: @Composable (modifier: Modifier) -> Unit,
74+
surfaceAspectRatio: Float,
7475
xrEnabled: Boolean = false,
7576
supportsTabletop: Boolean = supportsTabletop(),
7677
isTabletop: Boolean = false,
@@ -107,6 +108,7 @@ internal fun CameraLayout(
107108
zoomButton,
108109
guideText,
109110
guide,
111+
surfaceAspectRatio,
110112
)
111113

112114
isAtLeastMedium() && shouldShowTabletopLayout(
@@ -580,6 +582,7 @@ private fun CameraOverlayPreview(
580582
},
581583
supportsTabletop = parameters.supportsTabletop,
582584
isTabletop = parameters.isTabletop,
585+
surfaceAspectRatio = 16f / 9f,
583586
)
584587
}
585588
}

feature/camera/src/main/java/com/android/developers/androidify/camera/CameraScreen.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ fun CameraPreviewScreen(
148148
isRearCameraEnabled = uiState.isRearCameraActive,
149149
cameraSessionId = uiState.cameraSessionId,
150150
xrEnabled = uiState.xrEnabled,
151+
surfaceAspectRatio = uiState.surfaceAspectRatio,
151152
)
152153
}
153154
} else {
@@ -204,13 +205,14 @@ fun StatelessCameraPreviewContent(
204205
onAnimateZoom: (Float) -> Unit,
205206
requestCaptureImage: () -> Unit,
206207
modifier: Modifier = Modifier,
208+
surfaceAspectRatio: Float = 9f / 16f,
207209
xrEnabled: Boolean = false,
208210
foldingFeature: FoldingFeature? = null,
209211
shouldShowRearCameraFeature: () -> Boolean = { false },
210212
toggleRearCameraFeature: () -> Unit = {},
211213
isRearCameraEnabled: Boolean = false,
212214
) {
213-
var aspectRatio by remember { mutableFloatStateOf(9f / 16f) }
215+
var layoutAspectRatio by remember { mutableFloatStateOf(9f / 16f) }
214216
val emptyComposable: @Composable (Modifier) -> Unit = {}
215217
val rearCameraButton: @Composable (Modifier) -> Unit = { rearModifier ->
216218
RearCameraButton(
@@ -261,7 +263,7 @@ fun StatelessCameraPreviewContent(
261263
CameraGuide(
262264
detectedPose = detectedPose,
263265
modifier = guideModifier,
264-
defaultAspectRatio = aspectRatio,
266+
defaultAspectRatio = layoutAspectRatio,
265267
)
266268
},
267269
rearCameraButton = (
@@ -275,9 +277,11 @@ fun StatelessCameraPreviewContent(
275277
modifier = modifier.onSizeChanged { size ->
276278
if (size.height > 0) {
277279
// Recalculate aspect ratio based on the overall layout size
278-
aspectRatio = calculateCorrectAspectRatio(size.height, size.width, aspectRatio)
280+
layoutAspectRatio =
281+
calculateCorrectAspectRatio(size.height, size.width, layoutAspectRatio)
279282
}
280283
},
284+
surfaceAspectRatio = surfaceAspectRatio,
281285
xrEnabled = xrEnabled,
282286
)
283287
}
@@ -301,6 +305,7 @@ private fun CameraPreviewContent(
301305
zoomLevel: () -> Float,
302306
onChangeZoomLevel: (zoomLevel: Float) -> Unit,
303307
requestCaptureImage: () -> Unit,
308+
surfaceAspectRatio: Float,
304309
modifier: Modifier = Modifier,
305310
foldingFeature: FoldingFeature? = null,
306311
shouldShowRearCameraFeature: () -> Boolean = { false },
@@ -328,7 +333,8 @@ private fun CameraPreviewContent(
328333
onScaleZoom = { scope.launch { zoomState.scaleZoom(it) } },
329334
modifier = viewfinderModifier.onSizeChanged { size -> // Apply modifier from slot
330335
if (size.height > 0) {
331-
aspectRatio = calculateCorrectAspectRatio(size.height, size.width, aspectRatio)
336+
aspectRatio =
337+
calculateCorrectAspectRatio(size.height, size.width, aspectRatio)
332338
}
333339
},
334340
)
@@ -345,6 +351,7 @@ private fun CameraPreviewContent(
345351
shouldShowRearCameraFeature = shouldShowRearCameraFeature,
346352
toggleRearCameraFeature = toggleRearCameraFeature,
347353
isRearCameraEnabled = isRearCameraEnabled,
354+
surfaceAspectRatio = surfaceAspectRatio,
348355
xrEnabled = xrEnabled,
349356
modifier = modifier,
350357
)

feature/camera/src/main/java/com/android/developers/androidify/camera/CameraViewModel.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,12 @@ class CameraViewModel
9292

9393
private val cameraPreviewUseCase = Preview.Builder().build().apply {
9494
setSurfaceProvider { newSurfaceRequest ->
95-
_uiState.update { it.copy(surfaceRequest = newSurfaceRequest) }
95+
val width = newSurfaceRequest.resolution.width.toFloat()
96+
val height = newSurfaceRequest.resolution.height.toFloat()
97+
_uiState.update { it.copy(surfaceRequest = newSurfaceRequest, surfaceAspectRatio = height / width) }
9698
surfaceMeteringPointFactory = SurfaceOrientedMeteringPointFactory(
97-
newSurfaceRequest.resolution.width.toFloat(),
98-
newSurfaceRequest.resolution.height.toFloat(),
99+
width,
100+
height,
99101
)
100102
}
101103
}
@@ -354,6 +356,7 @@ data class CameraUiState(
354356
val isRearCameraActive: Boolean = false,
355357
val autofocusUiState: AutofocusUiState = AutofocusUiState.Unspecified,
356358
val xrEnabled: Boolean = false,
359+
val surfaceAspectRatio: Float = 9f / 16f,
357360
) {
358361
val zoomOptions = when {
359362
zoomMinRatio <= 0.6f && zoomMaxRatio >= 1f -> listOf(0.6f, 1f)

feature/camera/src/main/java/com/android/developers/androidify/camera/xr/CameraLayoutSpatial.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ fun CameraLayoutSpatial(
5151
MainPanelWorkaround()
5252
SpatialPanel(
5353
SubspaceModifier
54-
.fillMaxSize(0.5f),
54+
.fillMaxSize(0.5f)
55+
.aspectRatio(surfaceAspectRatio),
5556
) {
5657
Orbiter(
5758
position = ContentEdge.Top,

0 commit comments

Comments
 (0)