Fix tap-to-focus using wrong coordinate space in new camera.

We were using raw coordinates. Now we transform them appropriately
to the correct coordinate space.
This commit is contained in:
Greyson Parrelli
2026-02-06 14:13:32 -05:00
parent 73b0331884
commit f972382f5e
3 changed files with 22 additions and 12 deletions

View File

@@ -4,6 +4,7 @@ import android.content.res.Configuration
import androidx.camera.compose.CameraXViewfinder
import androidx.camera.core.SurfaceRequest
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.camera.viewfinder.compose.MutableCoordinateTransformer
import androidx.camera.core.Preview as CameraPreview
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.Animatable
@@ -132,8 +133,12 @@ fun CameraScreen(
}
)
} else if (surfaceRequest != null) {
val coordinateTransformer = remember { MutableCoordinateTransformer() }
val currentSurfaceRequest = surfaceRequest!!
CameraXViewfinder(
surfaceRequest = surfaceRequest!!,
surfaceRequest = currentSurfaceRequest,
coordinateTransformer = coordinateTransformer,
modifier = Modifier
.fillMaxSize()
.clip(cornerShape)
@@ -143,12 +148,15 @@ fun CameraScreen(
emitter(CameraScreenEvents.SwitchCamera(context))
},
onTap = { offset ->
val surfaceCoords = with(coordinateTransformer) { offset.transform() }
emitter(
CameraScreenEvents.TapToFocus(
x = offset.x,
y = offset.y,
width = size.width.toFloat(),
height = size.height.toFloat()
viewX = offset.x,
viewY = offset.y,
surfaceX = surfaceCoords.x,
surfaceY = surfaceCoords.y,
surfaceWidth = currentSurfaceRequest.resolution.width.toFloat(),
surfaceHeight = currentSurfaceRequest.resolution.height.toFloat()
)
)
}

View File

@@ -18,10 +18,12 @@ sealed interface CameraScreenEvents {
/** Focuses the camera on a point. */
data class TapToFocus(
val x: Float,
val y: Float,
val width: Float,
val height: Float
val viewX: Float,
val viewY: Float,
val surfaceX: Float,
val surfaceY: Float,
val surfaceWidth: Float,
val surfaceHeight: Float
) : CameraScreenEvents
/** Zoom that happens when you pinch your fingers. */

View File

@@ -363,14 +363,14 @@ class CameraScreenViewModel : ViewModel() {
) {
val currentCamera = camera ?: return
val factory = SurfaceOrientedMeteringPointFactory(event.width, event.height)
val point = factory.createPoint(event.x, event.y)
val factory = SurfaceOrientedMeteringPointFactory(event.surfaceWidth, event.surfaceHeight)
val point = factory.createPoint(event.surfaceX, event.surfaceY)
val action = FocusMeteringAction.Builder(point).build()
currentCamera.cameraControl.startFocusAndMetering(action)
_state.value = state.copy(
focusPoint = Offset(event.x, event.y),
focusPoint = Offset(event.viewX, event.viewY),
showFocusIndicator = true
)