-
Notifications
You must be signed in to change notification settings - Fork 14
Cam Selector (Lens Facing)
Lucas Yuji edited this page Nov 3, 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(Bitmap)
, onSwitchToBack(Bitmap)
, and onPreviewStreamChanged
- onSwitchToFront(Bitmap) and onSwitchToBack(Bitmap) are composable callbacks that are triggered when the camera preview is switched and 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.