-
Notifications
You must be signed in to change notification settings - Fork 202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add basic Camera sample #382
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some changes to make in order to get it working with the current CoroutineLifecycleOwner
code. I'll take another pass to get it working without CoroutineLifecycleOwner
.
fun CameraPreviewScreen(modifier: Modifier = Modifier) { | ||
val viewModel = CameraPreviewViewModel(LocalContext.current.applicationContext) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ViewModel should be retrieved via androidx.lifecycle.viewmodel.compose.viewModel
. Something like:
@Composable
fun CameraPreviewScreen(
modifier: Modifier = Modifier,
viewModel: CameraPreviewViewModel = viewModel()
) {
val surfaceRequest by viewModel.surfaceRequest.collectAsStateWithLifecycle()
val surfaceRequest by viewModel.surfaceRequest.collectAsStateWithLifecycle() | ||
|
||
LifecycleStartEffect(Unit) { | ||
viewModel.startCamera() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can just pass the context to the camera here instead of through the ViewModel constructor. This is to align with the advice from https://developer.android.com/topic/architecture/recommendations#viewmodel where it says not to use AndroidViewModel
. Though I wonder if there is still a better way to pass the context in.
val context = LocalContext.current
LifecycleStartEffect(Unit) {
viewModel.startCamera(context)
onStopOrDispose {
viewModel.stopCamera()
}
}
|
||
fun takePicture() { | ||
viewModelScope.launch { | ||
val imageProxy = captureUseCase.takePicture() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Take picture doesn't compile without parameters passed in
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't reproduce on my side. Code works well both with camerax-compose alpha02 and alpha06
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an extension function, so make sure you import androidx.camera.core.takePicture
.
No description provided.