Saturday, 11 March 2023

Camera started but can't see preview, Camera2 androidx compose

I have been trying to make cameraX2 to work with compose for couple of days now. The issue is that it seems like camera starts. It shows green label indicating that the camera is capturing, but preview does not work. After navigating back one image from camera is brifly visible during transition.

I have attached screenshots below so you see how it works.

Camera2 version is 1.2.1

private const val cameraVersion="1.2.1"
const val camera="androidx.camera:camera-camera2:$cameraVersion";
const val cameraLifecycle="androidx.camera:camera-lifecycle:$cameraVersion"
const val cameraView="androidx.camera:camera-view:$cameraVersion"

Composable

@Composable
@Destination
fun TakePictureScreen(
    viewModel: TakePictureViewModel = hiltViewModel()
) {


    val lensFacing = CameraSelector.LENS_FACING_FRONT
    val context = LocalContext.current
    val lifecycleOwner = LocalLifecycleOwner.current

    var cameraInit = remember {
        mutableStateOf(false)
    }
    val preview = Preview.Builder().build()
    val previewView = remember { PreviewView(context) }
    val imageCapture: ImageCapture = remember { ImageCapture.Builder().build() }
    val cameraSelector = CameraSelector.Builder()
        .requireLensFacing(lensFacing)
        .build()


    val cameraPermissionResultLauncher = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.RequestPermission(),
        onResult = { isGranted ->
            viewModel.onPermissionResult(
                permission = Manifest.permission.CAMERA,
                isGranted = isGranted
            )
        }
    )

    LaunchedEffect(key1 = true) {
        cameraPermissionResultLauncher.launch(Manifest.permission.CAMERA)
    }
    LaunchedEffect(lensFacing) {
        val cameraProvider = context.getCameraProvider()
        cameraProvider.unbindAll()
        cameraProvider.bindToLifecycle(
            lifecycleOwner,
            cameraSelector,
            preview
        )

        preview.setSurfaceProvider(previewView.surfaceProvider)
        delay(2000)
        cameraInit.value=true
    }

    if (cameraInit.value) {
        Icon(
            tint = MaterialTheme.colors.primary,
            modifier = Modifier
                .padding(8.dp)
                .fillMaxWidth()
                .height(IntrinsicSize.Max),
            painter = painterResource(R.drawable.can_location),
            contentDescription = stringResource(id = R.string.take_photo)
        )
        AndroidView(
            factory = { previewView }, modifier = Modifier
                .fillMaxSize(),
            update = {
                preview.setSurfaceProvider(previewView.surfaceProvider)
            }
        )
    }
}

First screenshots, after that I press the camera button and the app navigates to different composable screen. First picture befor starting the camera

This is the composable where the camera should be rendered. You can see top right corner there is a green dot indicating camera is running. Second picture shows the composable where camera should be rendering into previewview After I navigate back using a gesture, the camera shows one frame as the app is transitioning to the previous screen Picture showing rendered frame after navigating back



from Camera started but can't see preview, Camera2 androidx compose

No comments:

Post a Comment