Camera Session
Introduction
CameraSession is the central component that manages the camera lifecycle and provides access to the camera’s state, information, and controller. It serves as the main bridge between the UI and the camera hardware.
Creating a Camera Session
Use rememberCameraSession to create and remember a camera session across recompositions:
@Composable
fun MyCameraScreen() {
val cameraController = remember { CameraController() }
val cameraSession = rememberCameraSession(cameraController)
CameraPreview(cameraSession = cameraSession)
}
Alternatively, you can create a session without explicitly passing a controller:
In this case, a controller is created internally and can be accessed via cameraSession.controller.
Components
CameraSession provides access to three main components:
1. State (cameraSession.state)
The state holds all mutable camera configurations such as:
captureMode: Image or Video modecamSelector: Front or Back cameraflashMode: Flash settings (Off, On, Auto)zoomRatio: Current zoom levelexposureCompensation: Exposure adjustmentisTorchEnabled: Torch on/off statemirrorMode: Mirror mode configuration- And many more camera settings
Example:
val cameraSession = rememberCameraSession()
val flashMode by cameraSession.state.flashMode.collectAsStateWithLifecycle()
val zoomRatio by cameraSession.state.zoomRatio.collectAsStateWithLifecycle()
2. Info (cameraSession.info)
The info provides read-only hardware capabilities of the current camera and limits through CameraInfoState:
minZoom/maxZoom: Zoom rangeminExposure/maxExposure: Exposure rangeisFlashSupported: Flash availabilityisTorchSupported: Torch availabilityisFocusSupported: Focus capabilityminFPS/maxFPS: Frame rate limitsphotoFormats/videoFormats: Supported capture formats
Example:
import com.ujizin.camposer.lifecycle.compose.collectStateWithLifecycle
val cameraSession = rememberCameraSession()
val cameraInfoState by cameraSession.info.collectStateWithLifecycle() // or val cameraInfoState by cameraSession.info.state.collectAsStateWithLifecycle()
val isFlashSupported = cameraInfoState.isFlashSupported
val maxZoom = cameraInfoState.maxZoom
if (isFlashSupported) {
// Show flash button
}
3. Controller (cameraSession.controller)
The controller provides methods to perform camera operations:
takePicture(): Capture a photostartRecording()/stopRecording(): Record videosetZoomRatio(): Adjust zoomsetFlashMode(): Change flash modesetExposureCompensation(): Adjust exposure- And more camera actions
Example:
val controller = remember { CameraController() }
val cameraSession = rememberCameraSession(controller)
Button(onClick = {
controller.takePicture { result ->
when(result) {
is CaptureResult.Success -> { /* Handle success */ }
is CaptureResult.Error -> { /* Handle error */ }
}
}
}) {
Text("Take Picture")
}
Session Status Properties
isStreaming
Indicates whether the camera is currently streaming (preview is active).
val cameraSession = rememberCameraSession()
val isStreaming by rememberUpdatedState(cameraSession.isStreaming)
if (isStreaming) {
// Camera preview is active
}
isInitialized
Indicates whether the camera session has been fully initialized.