Skip to content

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:

val cameraSession = rememberCameraSession()

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 mode
  • camSelector: Front or Back camera
  • flashMode: Flash settings (Off, On, Auto)
  • zoomRatio: Current zoom level
  • exposureCompensation: Exposure adjustment
  • isTorchEnabled: Torch on/off state
  • mirrorMode: 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 range
  • minExposure / maxExposure: Exposure range
  • isFlashSupported: Flash availability
  • isTorchSupported: Torch availability
  • isFocusSupported: Focus capability
  • minFPS / maxFPS: Frame rate limits
  • photoFormats / 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 photo
  • startRecording() / stopRecording(): Record video
  • setZoomRatio(): Adjust zoom
  • setFlashMode(): Change flash mode
  • setExposureCompensation(): 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.

val cameraSession = rememberCameraSession()
val isInitialized by rememberUpdatedState(cameraSession.isInitialized)

if (isInitialized) {
    // Camera is ready to use
}