androidx.camera.compose

Jetpack Compose tools for users of the Jetpack Camera camera-core library

Top-level functions summary

Unit
@Composable
CameraXViewfinder(
    surfaceRequest: SurfaceRequest,
    modifier: Modifier,
    implementationMode: ImplementationMode,
    coordinateTransformer: MutableCoordinateTransformer?
)

An adapter composable that displays frames from CameraX by completing provided SurfaceRequests.

Top-level functions

CameraXViewfinder

@Composable
fun CameraXViewfinder(
    surfaceRequest: SurfaceRequest,
    modifier: Modifier = Modifier,
    implementationMode: ImplementationMode = ImplementationMode.EXTERNAL,
    coordinateTransformer: MutableCoordinateTransformer? = null
): Unit

An adapter composable that displays frames from CameraX by completing provided SurfaceRequests.

This is a wrapper around Viewfinder that will convert a CameraX SurfaceRequest internally into a ViewfinderSurfaceRequest. Additionally, all interactions normally handled through the ViewfinderSurfaceRequest will be derived from the SurfaceRequest.

If implementationMode is changed while the provided surfaceRequest has been fulfilled, the surface request will be invalidated as if SurfaceRequest.invalidate has been called. This will allow CameraX to know that a new surface request is required since the underlying viewfinder implementation will be providing a new surface.

Example usage:

import androidx.camera.compose.CameraXViewfinder
import androidx.camera.core.Preview
import androidx.camera.core.SurfaceRequest
import androidx.camera.viewfinder.compose.MutableCoordinateTransformer
import androidx.camera.viewfinder.surface.ImplementationMode
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.pointerInput
import androidx.lifecycle.ViewModel

class PreviewViewModel : ViewModel() {
    private val _surfaceRequests = MutableStateFlow<SurfaceRequest?>(null)

    val surfaceRequests: StateFlow<SurfaceRequest?>
        get() = _surfaceRequests.asStateFlow()

    private fun produceSurfaceRequests(previewUseCase: Preview) {
        // Always publish new SurfaceRequests from Preview
        previewUseCase.setSurfaceProvider { newSurfaceRequest ->
            _surfaceRequests.value = newSurfaceRequest
        }
    }

    fun focusOnPoint(surfaceBounds: Size, x: Float, y: Float) {
        // Create point for CameraX's CameraControl.startFocusAndMetering() and submit...
    }

    // ...
}

@Composable
fun MyCameraViewfinder(viewModel: PreviewViewModel, modifier: Modifier = Modifier) {
    val currentSurfaceRequest: SurfaceRequest? by viewModel.surfaceRequests.collectAsState()

    currentSurfaceRequest?.let { surfaceRequest ->

        // CoordinateTransformer for transforming from Offsets to Surface coordinates
        val coordinateTransformer = remember { MutableCoordinateTransformer() }

        CameraXViewfinder(
            surfaceRequest = surfaceRequest,
            implementationMode = ImplementationMode.EXTERNAL, // Can also use EMBEDDED
            modifier =
                modifier.pointerInput(Unit) {
                    detectTapGestures {
                        with(coordinateTransformer) {
                            val surfaceCoords = it.transform()
                            viewModel.focusOnPoint(
                                surfaceRequest.resolution,
                                surfaceCoords.x,
                                surfaceCoords.y
                            )
                        }
                    }
                },
            coordinateTransformer = coordinateTransformer
        )
    }
}
Parameters
surfaceRequest: SurfaceRequest

The surface request from CameraX

modifier: Modifier = Modifier

The Modifier to be applied to this viewfinder

implementationMode: ImplementationMode = ImplementationMode.EXTERNAL

The ImplementationMode to be used by this viewfinder.

coordinateTransformer: MutableCoordinateTransformer? = null

The MutableCoordinateTransformer used to map offsets of this viewfinder to the source coordinates of the data being provided to the surface that fulfills surfaceRequest