SkiaCamera

fun SkiaCamera(device: CameraDeviceOrPosition, isActive: Boolean, onFrame: (frame: Frame, SkiaRender) -> Unit, modifier: Modifier = Modifier, outputs: List<CameraOutput> = emptyList(), constraints: List<Constraint> = emptyList(), orientationSource: OrientationSource? = OrientationSource.DEVICE, zoom: Double? = null, exposure: Double? = null, torchMode: TorchMode? = null, controllerConfiguration: CameraControllerConfiguration = CameraControllerConfiguration(), sessionConfiguration: CameraSessionConfiguration = CameraSessionConfiguration(), warnIfRenderSkipped: Boolean = true, pixelFormat: TargetVideoPixelFormat = defaultSkiaPixelFormat, enablePhysicalBufferRotation: Boolean = false, enablePreviewSizedOutputBuffers: Boolean = false, targetResolution: Size = FrameOutputOptions().targetResolution, onControllerChanged: (CameraController?) -> Unit? = null, onSessionConfigSelected: (CameraSessionConfig) -> Unit? = null, onConfigured: () -> Unit? = null, onStarted: () -> Unit? = null, onStopped: () -> Unit? = null, onError: (Throwable) -> Unit = { println("VisionCamera error: $it") }, onInterruptionStarted: (InterruptionReason) -> Unit? = null, onInterruptionEnded: () -> Unit? = null, onSubjectAreaChanged: () -> Unit? = null, onUIRotationChanged: (rotation: Int) -> Unit? = null, ref: SkiaCameraRef? = null)

A Camera view that uses Skia (through Skiko) for rendering (the equivalent of V5's <SkiaCamera />).

Instead of a native preview view, the SkiaCamera uses a CameraFrameOutput to stream Frames, which are converted to Skia textures (see SkiaOnFrameState.frameTexture) to be renderable.

You must ensure that the Frame is actually rendered to the canvas inside your onFrame callback, by calling its render function, otherwise it'll just be a blank view.

Simple rendering as-is:

SkiaCamera(
device = TargetCameraPosition.BACK,
isActive = true,
onFrame = { frame, render ->
render { (frameTexture, canvas) ->
canvas.drawImage(frameTexture, 0f, 0f)
}
frame.dispose()
},
modifier = Modifier.fillMaxSize(),
)

Drawing Frames with a custom pass-through shader:

// Simple pass-through Shader (just renders pixels as-is)
val effect = RuntimeEffect.makeForShader("""
uniform shader src;
half4 main(float2 xy) {
return src.eval(xy);
}
""")
val paint = Paint()

SkiaCamera(
device = TargetCameraPosition.BACK,
isActive = true,
onFrame = { frame, render ->
render { (frameTexture, canvas) ->
// Prepare the shader with the `frameTexture` as an input
val imageShader = frameTexture.makeShader(
FilterTileMode.CLAMP,
FilterTileMode.CLAMP,
SamplingMode.LINEAR,
)
paint.shader = effect.makeShader(null, arrayOf(imageShader), null)
// Draw the Frame with the shader/paint (Skia ignores the shader of a paint given to drawImage)
val bounds = Rect.makeWH(frameTexture.width.toFloat(), frameTexture.height.toFloat())
canvas.drawRect(bounds, paint)
}
frame.dispose()
},
)

The rendered result is copied to the CPU and displayed by Compose, scaled to cover the view (centered and cropped).

It is recommended to manually set a pixelFormat, for example TargetVideoPixelFormat.YUV for efficiency, or TargetVideoPixelFormat.RGB for compatibility with ML detection libraries.

Parameters

device

The CameraDevice to open, or a TargetCameraPosition to use the default device at that position. Front Cameras are mirrored.

isActive

Starts the session when true, and stops it when false.

onFrame

Called on every Frame with the Frame and a render function, on the Frame output's thread. You must render the Frame to the canvas yourself by calling render once with a draw function (see SkiaOnFrameState), otherwise it'll just be a blank view, and dispose the Frame afterwards.

modifier

The Modifier of the view.

outputs

Additional CameraOutputs, besides the Skia CameraFrameOutput.

constraints

Constraints the session tries to match, ranked by priority. A ResolutionBiasConstraint for each output is added after them, with a lower priority.

orientationSource

The OrientationSource whose orientation is automatically applied to the additional outputs, or null to set it yourself. The Skia CameraFrameOutput always follows the interface orientation.

zoom

The zoom value, applied declaratively.

exposure

The exposure bias, applied declaratively.

torchMode

The torch mode, applied declaratively.

controllerConfiguration

Low-light boost, smooth auto-focus and distortion correction.

sessionConfiguration

Session-wide configuration (iOS audio and haptics behavior).

warnIfRenderSkipped

Warns the developer if the onFrame callback did not call render. Disable this if you intentionally want to skip a render.

pixelFormat

The TargetVideoPixelFormat of the Skia CameraFrameOutput. TargetVideoPixelFormat.NATIVE is a zero-copy GPU-only path if the negotiated CameraSessionConfig.nativePixelFormat is supported by Skia (use it with caution, as the device's native format could be one Skia does not support, e.g. VideoPixelFormat.RAW_BAYER_PACKED96_12_BIT); only resort to TargetVideoPixelFormat.YUV (or even TargetVideoPixelFormat.RGB) if CPU-based Frame access is required. Defaults to YUV on iOS and NATIVE on Android, like V5.

enablePhysicalBufferRotation

Physically rotates buffers to the target orientation and mirror mode. Disabled by default, as Skia rotates and mirrors the Frame via efficient GPU-based transforms. Enabling this results in the Frame always being upright and never mirrored.

enablePreviewSizedOutputBuffers

Downscales Frames to a size suitable for preview, such as the screen size. This may be more efficient if the session negotiated a high-resolution output, as Skia doesn't have to operate on high-resolution Frames.

targetResolution

The target resolution of the Skia CameraFrameOutput, which affects both the Frame's size (in onFrame) and the preview's resolution. It is capped to the screen's size if enablePreviewSizedOutputBuffers is enabled. As with all target resolutions, actual resolutions are negotiated across constraints and outputs.

onControllerChanged

Called with the CameraController once the session is configured, e.g. to focus.

onSessionConfigSelected

Called once the constraints have been resolved.

onConfigured

Called whenever the session has been configured.

onStarted

Called when the session started.

onStopped

Called when the session stopped.

onError

Called when the session encountered an error.

onInterruptionStarted

Called when the session was interrupted.

onInterruptionEnded

Called when an interruption ended.

onSubjectAreaChanged

Called when the subject area substantially changes. iOS only, like V5.

onUIRotationChanged

Called when the output orientation or the interface orientation changes, with the rotation in degrees (-90, 0, 90 or 180) that UI elements such as Camera controls need to appear upright.

ref

A SkiaCameraRef exposing the controller, focus, snapshots and coordinate conversions, see rememberSkiaCameraRef.