-
Notifications
You must be signed in to change notification settings - Fork 14
Cam Selector (Lens Facing)
Lucas Yuji edited this page Nov 2, 2022
·
9 revisions
There are 2 selectors, CamSelector.Back
and CamSelector.Front
You can use and switch cameras like any state from compose, here is an example:
@Composable
fun Camera() {
var camSelector by rememberCamSelector(CamSelector.Back) // Or CamSelector.Front
CameraPreview(camSelector = camSelector) {
Button(onClick = {
camSelector = camSelector.reverse // Switch Camera
}) {
Text("Switch")
}
}
}
The rememberCamSelector
restore on configuration changed, using rememberSaveable
internally. If you DON'T want that restore on configuration changed, you can use with remember
like the example below:
// WITHOUT RESTORE ON CONFIGURATION CHANGED
val camSelector by remember { mutableStateOf(CamSelector.Back) }
To switch cameras from front to back or vice versa, use CamSelector.reverse
:
Button(onClick = {
camSelector = camSelector.reverse // Switch Camera
}) { /* code... /* }
If you want callbacks when switch cameras, check it out on onSwitchToFront
, onSwitchToBack
and onPreviewStreamChanged
- onSwitchToFront and onSwitchToBack are composable callbacks to use when the camera preview is idle, there's one latest bitmap frame from the camera as a parameter
- onPreviewStreamChanged is a generic callback that's triggered when the camera is switched.