SkiaCamera
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
The CameraDevice to open, or a TargetCameraPosition to use the default device at that position. Front Cameras are mirrored.
Starts the session when true, and stops it when false.
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.
The Modifier of the view.
Additional CameraOutputs, besides the Skia CameraFrameOutput.
Constraints the session tries to match, ranked by priority. A ResolutionBiasConstraint for each output is added after them, with a lower priority.
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.
The zoom value, applied declaratively.
The exposure bias, applied declaratively.
The torch mode, applied declaratively.
Low-light boost, smooth auto-focus and distortion correction.
Session-wide configuration (iOS audio and haptics behavior).
Warns the developer if the onFrame callback did not call render. Disable this if you intentionally want to skip a render.
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.
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.
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.
Called with the CameraController once the session is configured, e.g. to focus.
Called once the constraints have been resolved.
Called whenever the session has been configured.
Called when the session started.
Called when the session stopped.
Called when the session encountered an error.
Called when the session was interrupted.
Called when an interruption ended.
Called when the subject area substantially changes. iOS only, like V5.
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.
A SkiaCameraRef exposing the controller, focus, snapshots and coordinate conversions, see rememberSkiaCameraRef.