Tired to use a camera in Jetpack Compose with interoperability? Then Camposer was made for you.
A camera library totally in Jetpack Compose which supports taking photos, recording videos, flash modes, zoom ratio, and among others!
Check out the Sample project
Add dependency to your build.gradle
and sync your project
implementation 'io.github.ujizin:camposer:<version>'
To add CameraPreview
composable, just use the example below:
val cameraState = rememberCameraState()
var camSelector by rememberCamSelector(CamSelector.Back)
CameraPreview(
cameraState = cameraState,
camSelector = camSelector,
) {
// Camera Preview UI
}
To take picture use CameraState
and call takePicture
method from it.
// Using content values
cameraState.takePicture(contentValues, mediaURI) { result ->
/* ... */
}
// Using files
cameraState.takePicture(file) { result -> /* ... */ }
Change the capture mode for CaptureMode.Video
on CameraPreview
and call toggleRecording
method twice for stop recording, or use startRecording
and stopRecording
separately
// Using content values
cameraState.startRecording()
cameraState.stopRecording(contentValues, mediaURI) { result ->
/* ... */
}
// Using file
cameraState.startRecording()
cameraState.stopRecording(file) { result -> /* ... */ }
// Using content values + toggle
cameraState.toggleRecording(contentValues, mediaURI) { result ->
/* ... */
}
// Using files + toggle
cameraState.toggleRecording(file) { result -> /* ... */ }
To switch cameras, you have to add camSelector
to your CameraPreview
composable, as shown previously, after implementation, just need to change its state.
// Use front camera
camSelector = CamSelector.Front
// Use back camera
camSelector = CamSelector.Back
// Inverse camera selector
camSelector = camSelector.inverse
If you want to use other configurations, you can see our wiki.
Have a fun code()
!
Copyright 2022 ujizin (Lucas Yuji)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.