Repository navigation
Add basic Camera sample - #382
JolandaVerhoef wants to merge 6 commits into
Conversation
temcguir
left a comment
There was a problem hiding this comment.
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.
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.
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.
Take picture doesn't compile without parameters passed in
There was a problem hiding this comment.
Can't reproduce on my side. Code works well both with camerax-compose alpha02 and alpha06
There was a problem hiding this comment.
This is an extension function, so make sure you import androidx.camera.core.takePicture.
No description provided.