camera-samples icon indicating copy to clipboard operation
camera-samples copied to clipboard

CameraX Back and front camera can't preview at same time ?

Open Reginer opened this issue 5 years ago • 7 comments

previewBack.setSurfaceProvider(viewBack.surfaceProvider)
previewFront.setSurfaceProvider(viewFront.surfaceProvider)
val cameraBack = cameraProvider.bindToLifecycle(this, cameraSelectorBack, previewBack)
val cameraFront = cameraProvider.bindToLifecycle(this, cameraSelectorFront, previewFront)
 java.lang.IllegalArgumentException: Multiple LifecycleCameras with use cases are registered to the same LifecycleOwner.
        at androidx.camera.lifecycle.LifecycleCameraRepository.bindToLifecycleCamera(LifecycleCameraRepository.java:269)
        at androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle(ProcessCameraProvider.java:423)
        at androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle(ProcessCameraProvider.java:274)

Reginer avatar Jan 15 '21 04:01 Reginer

It seems it's not supported. https://issuetracker.google.com/issues/151866349 I've even tried to use different LifecycleOwner for both previews but the only one camera is working, other one is black

agent10 avatar Apr 14 '21 19:04 agent10

I get it

Reginer avatar Apr 15 '21 00:04 Reginer

Hii guy's use - cameraProvider.unbindAll() // Unbind use cases before rebinding,

btnSwitchCamera.setOnClickListener {

        if (cameraSelector == CameraSelector.DEFAULT_FRONT_CAMERA && cameraProvider.hasCamera(
                CameraSelector.DEFAULT_BACK_CAMERA)) {

            cameraProvider.unbindAll()    // Unbind use cases before rebinding

            cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
            val preview = Preview.Builder().build().also {
                it.setSurfaceProvider(viewFinder.createSurfaceProvider())
            }
            cameraProvider.bindToLifecycle(this, cameraSelector, preview)
            
        } else if (cameraSelector == CameraSelector.DEFAULT_BACK_CAMERA && cameraProvider.hasCamera(
                CameraSelector.DEFAULT_FRONT_CAMERA)) {

            cameraProvider.unbindAll()    // Unbind use cases before rebinding

            cameraSelector = CameraSelector.DEFAULT_FRONT_CAMERA
            val preview = Preview.Builder().build().also {
                it.setSurfaceProvider(viewFinder.createSurfaceProvider())
            }
            cameraProvider.bindToLifecycle(this, cameraSelector, preview)
        }

    }

taiyabali avatar May 11 '21 18:05 taiyabali

@taiyabali There is no problem with switching back/front cameras. CameraX doesn't support both simultaneously. Like two previews on the screen.

agent10 avatar May 11 '21 18:05 agent10

@agent10 Do u have any solution cuz I have exactly same problem.

letuss004 avatar Dec 28 '21 04:12 letuss004

@letuss004 I do) I use pure Camera2 api

agent10 avatar Dec 28 '21 06:12 agent10

@agent10 haha i expected an camerax but camera2 fair enough. Did u share source in github? I really want to take a look

letuss004 avatar Dec 29 '21 18:12 letuss004

@agent10 what do you do with devices that are not supported by hardware both cameras? As far as I understand, not all devices support both camera same time

gokor8 avatar Apr 04 '23 09:04 gokor8

@agent10 what do you do with devices that are not supported by hardware both cameras? As far as I understand, not all devices support both camera in time As far as I understand, all devices support both camera same time use Camera1 api.

Reginer avatar Apr 04 '23 09:04 Reginer

Hi, CameraX will support back and front camera operating at the same time in the next alpha release. Not only for preview but also for image capture and potentially other use cases as well. We will appreciate if you could try it and give feedback to us.

kailianc avatar Apr 04 '23 19:04 kailianc

To check the device capability of concurrent cameras in Camera2, you can use FEATURE_CAMERA_CONCURRENT.

https://developer.android.com/reference/android/content/pm/PackageManager#FEATURE_CAMERA_CONCURRENT

kailianc avatar Apr 04 '23 19:04 kailianc

Hi, CameraX will support back and front camera operating at the same time in the next alpha release. Not only for preview but also for image capture and potentially other use cases as well. We will appreciate if you could try it and give feedback to us.

Has the alpha version that supports using both front and back cameras simultaneously been released? What is the version number?

lodgkk avatar May 11 '23 03:05 lodgkk

Hi lodgkk,

Version 1.3.0-alpha06 is the latest version supporting both front and back cameras simultaneously. https://developer.android.com/jetpack/androidx/releases/camera#1.3.0-alpha06

This is the javadoc for main api: https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-main/camera/camera-lifecycle/src/main/java/androidx/camera/lifecycle/ProcessCameraProvider.java#759

This is the sample app to show a front + back camera previews in Picture-in-Picutre layout or Side-by-Side layout: https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-main/camera/integration-tests/coretestapp/src/main/java/androidx/camera/integration/core/ConcurrentCameraActivity.java#239

Let us know if any other questions.

kailianc avatar May 11 '23 07:05 kailianc

Hi lodgkk,

Version 1.3.0-alpha06 is the latest version supporting both front and back cameras simultaneously. https://developer.android.com/jetpack/androidx/releases/camera#1.3.0-alpha06

This is the javadoc for main api: https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-main/camera/camera-lifecycle/src/main/java/androidx/camera/lifecycle/ProcessCameraProvider.java#759

This is the sample app to show a front + back camera previews in Picture-in-Picutre layout or Side-by-Side layout: https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-main/camera/integration-tests/coretestapp/src/main/java/androidx/camera/integration/core/ConcurrentCameraActivity.java#239

Let us know if any other questions.

Concurrent camera is not supported on the device ,How can I solve this?

lodgkk avatar Nov 17 '23 06:11 lodgkk

If it's not supported on this device, then OEM decides to not expose this feature (e.g. due to power consumption or other device capability reason), the behavior will not be guaranteed. You need to find a device with this capability.

kailianc avatar Nov 17 '23 16:11 kailianc