Skip to content

Add basic Camera sample - #382

Closed
JolandaVerhoef wants to merge 6 commits into
mainfrom
jv/camera-basic
Closed

JolandaVerhoef wants to merge 6 commits into
mainfrom
jv/camera-basic

Conversation

@JolandaVerhoef

Copy link
Copy Markdown
Contributor

No description provided.

@temcguir temcguir left a comment

Copy link
Copy Markdown

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.

Comment thread media/build.gradle.kts
Comment thread media/src/main/AndroidManifest.xml
Comment on lines +69 to +70
fun CameraPreviewScreen(modifier: Modifier = Modifier) {
val viewModel = CameraPreviewViewModel(LocalContext.current.applicationContext)

Copy link
Copy Markdown

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()

Copy link
Copy Markdown

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()
        }
    }

Comment thread media/src/main/java/com/example/media/camera/CameraSnippets.kt Outdated
Comment thread media/src/main/java/com/example/media/camera/CameraSnippets.kt Outdated
Comment thread media/build.gradle.kts
Comment thread media/src/main/java/com/example/media/camera/CameraSnippets.kt Outdated
Comment thread media/src/main/java/com/example/media/camera/CameraSnippets.kt Outdated

fun takePicture() {
viewModelScope.launch {
val imageProxy = captureUseCase.takePicture()

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown

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.

@yrezgui yrezgui closed this Dec 8, 2025
@yrezgui
yrezgui deleted the jv/camera-basic branch December 8, 2025 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants