Switch between multiple cameras

387 views
Skip to first unread message
Assigned to tonyt...@google.com by wuj...@google.com

Mohammad Sajib Al Seraj

unread,
Aug 16, 2022, 5:37:20 PM8/16/22
to Android CameraX Discussion Group
Hello,

Does CameraX have the ability to select which of the Smartphone cameras you use for the photo?  Can we select whether to use the wide, normal or telephoto?

Thanks
Mohammad

tonyt...@google.com

unread,
Aug 17, 2022, 2:47:57 AM8/17/22
to Android CameraX Discussion Group, mals...@mtu.edu
Hi,
Thanks for the question. To select a different camera, it depends on whether the device has logical multi-camera capability. If it does, there's CameraControl.setZoomRatio to directly switch between different FOV.

However, if the device doesn't support logical camera, the alternative is using CameraProvider.getAvailableCameraInfos to get the camera information of all cameras, then iterate through them to find the desired camera, then use CameraInfo.getCameraSelector to get the corresponding camera selector and bind the use cases to it. In order to pick a camera based on FOV (focal length), you may use Camera2CameraInfo to get LENS_INFO_AVAILABLE_FOCAL_LENGTHS from Camera2. The implementation could be like:
```
private boolean checkFocalLength(float[] focalLengths) {
    // Check if the focal length matches requirement.
    ...
}



for (CameraInfo cameraInfo : cameraProvider.getAvailableCameraInfos()) {
    // Get the available focal lengths of the camera.
    float[] focalLengths = Camera2CameraInfo.from(cameraInfo).getCameraCharacteristic(CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS);
    if (checkFocalLength(focalLengths) {
        cameraProvider.bindToLifecycle(lifecycleOwner, cameraInfo.getCameraSelector(), useCases);
        break;
    }
}
```
We understand this might not be the most straightforward way and will continuously make improvement to the APIs. Hope it helps!

Mohammad Sajib Al Seraj

unread,
Aug 22, 2022, 1:19:36 PM8/22/22
to tonyt...@google.com, Android CameraX Discussion Group

I have tried both options, in both cases, I am getting center camera, I am not getting telephoto camera or wide-angle camera.

 Using CameraProvider.getAvailableCameraInfos, I am getting one camera.

My whole implementation on cameraX. It seems your provided solution for camera2 API. If yes, can I use camera2 and camera at the same time?

Thanks
Mohammad

tonyt...@google.com

unread,
Aug 23, 2022, 12:29:06 AM8/23/22
to Android CameraX Discussion Group, mals...@mtu.edu, Android CameraX Discussion Group, tonyt...@google.com
In that case, the camera you got is probably a logical camera. Could you try to setZoomRatio to ZoomState.getMaxZoomRatio or ZoomState.getMinZoomRatio and see if the camera stream switches to other lenses?

The alternative solution I provided is the way to interoperate Camera2 with CameraX APIs. But that only works for the devices that expose all cameras individually, which is probably not your case. May we know which device you are using?

Mohammad Sajib Al Seraj

unread,
Aug 23, 2022, 10:01:34 AM8/23/22
to tonyt...@google.com, Android CameraX Discussion Group
I have tried to setZoomRatio to ZoomState.getMaxZoomRatio or ZoomState.getMinZoomRatio and the camera stream didn't switch to other lenses? I found min zoom ratio 1.0 and max zoom ratio 8.0.
I am using Samsung galaxy A71 5G device.

Thanks
Mohammad

tonyt...@google.com

unread,
Aug 23, 2022, 10:56:09 AM8/23/22
to Android CameraX Discussion Group, mals...@mtu.edu, Android CameraX Discussion Group, tonyt...@google.com
I see. That probably means the device doesn't implement logical camera nor expose individual lenses. Could you help to check if CameraManager.getCameraIdList also contains only one camera ID? If that's the case, I'm afraid there's no way to get the stream from specific lens either by CameraX or Camera2 due to OEM limitation.

Mohammad Sajib Al Seraj

unread,
Aug 23, 2022, 12:10:53 PM8/23/22
to tonyt...@google.com, Android CameraX Discussion Group
Galaxy A71 device is showing 3 camera IDs when I checked with  CameraManager.getCameraIdList .
I have also checked this zoomRatio with a Pixel 5a device with two cameras. It's switching between two cameras with changing zoomRaio.

Thanks
Mohammad

tonyt...@google.com

unread,
Aug 23, 2022, 8:09:29 PM8/23/22
to Android CameraX Discussion Group, mals...@mtu.edu, Android CameraX Discussion Group, tonyt...@google.com
Thanks for the reply. We will have to check A71 to know what happens. To make sure, are you using CameraXConfig.Builder.setAvailableCamerasLimiter?

Mohammad Sajib Al Seraj

unread,
Aug 23, 2022, 8:18:04 PM8/23/22
to tonyt...@google.com, Android CameraX Discussion Group
No, I am not using CameraXConfig.Builder.setAvailableCamerasLimiter.

Thanks
Mohammad 

Scott Nien

unread,
Aug 24, 2022, 2:27:39 AM8/24/22
to Mohammad Sajib Al Seraj, tonyt...@google.com, Android CameraX Discussion Group
>  Using CameraProvider.getAvailableCameraInfos, I am getting one camera.

Can you double check this on Samsung A71 ?  

I've tested it on Samsung A71 5G (SM-A71B/DS).  
CameraProvider.getAvailableCameraInfos returns 4 different cameras("0","1","2","3")



--
You received this message because you are subscribed to the Google Groups "Android CameraX Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to camerax-develop...@android.com.
To view this discussion on the web visit https://groups.google.com/a/android.com/d/msgid/camerax-developers/CAHDOb3nASbCT9NE%3Djo1kJcLi6_86HUinr-zwRPBUjeM%3DOLHc6w%40mail.gmail.com.

Mohammad Sajib Al Seraj

unread,
Aug 24, 2022, 9:38:03 AM8/24/22
to Scott Nien, tonyt...@google.com, Android CameraX Discussion Group
I am getting 3 different cameras ("0", "1", "3"), camera "2" is missing from the list. Are you able to switch between different cameras by using the A71 device?
The model number of my device is SM-A716V.

Thanks
Mohammad

Scott Nien

unread,
Aug 25, 2022, 12:20:17 AM8/25/22
to Mohammad Sajib Al Seraj, tonyt...@google.com, Android CameraX Discussion Group
Yes, I can switch between all these 4 cameras on SM-A71B/DS. 

On your SM-A716V, I suppose that CameraManager.getCameraIdList also returns "0", "1", "3 ,  right ?  
This seems to be a device specific issue.  

Can you open the extra wide angle camera in native camera app ?  Also,  will rebooting the device fix the issue ?

Mohammad Sajib Al Seraj

unread,
Aug 25, 2022, 8:59:30 AM8/25/22
to Scott Nien, tonyt...@google.com, Android CameraX Discussion Group
Right, my device CameraManager.getCameraIdList returns "0", "1", "3.
Using native camera app, I can switch between different cameras.
I have few more devices in my office, I can check this issue with those cameras and let you know.

Thanks
Mohammad

tonyt...@google.com

unread,
Aug 25, 2022, 1:44:00 PM8/25/22
to Android CameraX Discussion Group, mals...@mtu.edu, tonyt...@google.com, Android CameraX Discussion Group, Scott Nien
As Scott said, missing of camera "2" might be a device issue. Although with my original workaround, you should be able to bind to camera "1" or "3". Camera "1" should be the front facing camera and "3" could be one of the other back facing cameras. Could you try it another try?

Mohammad Sajib Al Seraj

unread,
Aug 25, 2022, 4:28:32 PM8/25/22
to tonyt...@google.com, Android CameraX Discussion Group, Scott Nien
Hello,
Thank you so much for your valuable feedback.
Today, I tried with Samsung Galaxy A72(SM-A725M/DS) and Galaxy A53 5G (SM-A536U1/DS).
Both devices CameraManager.getCameraIdList returns "0", "1", "2", "3". But no device switching between different cameras. When I set the min or max zoom ratio, it basically zoom, but everytime it is using center camera, not telephoto or wide angle camera.
When I try to bind camera "2" or "3", I am getting an IllegalArgumentException.

java.lang.IllegalArgumentException: Provided camera selector unable to resolve a camera for the given use case

        at androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle(ProcessCameraProvider.java:470)

        at androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle(ProcessCameraProvider.java:360)


Thanks
Mohammad

tonyt...@google.com

unread,
Aug 26, 2022, 5:06:26 AM8/26/22
to Android CameraX Discussion Group, mals...@mtu.edu, Android CameraX Discussion Group, Scott Nien, tonyt...@google.com
To be clear, you are binding to the CameraSelector obtained from CameraInfo.getCameraSelector(), correct? And are you using CameraX Extentions?

Mohammad Sajib Al Seraj

unread,
Aug 26, 2022, 8:36:58 AM8/26/22
to tonyt...@google.com, Android CameraX Discussion Group, Scott Nien
Here is the code snippet for camera selector 
CameraSelector cameraSelector;
cameraSelector = new CameraSelector.Builder()
.requireLensFacing(1)
.build();
camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview,
imageCapture);
Yes, I am using camerax extension for night mode.

Thanks
Mohammad

tonyt...@google.com

unread,
Aug 26, 2022, 9:48:25 AM8/26/22
to Android CameraX Discussion Group, mals...@mtu.edu, Android CameraX Discussion Group, Scott Nien, tonyt...@google.com
I see. First of all, extensions may only be available on specific camera depending on the OEM implementation. Use ExtensionsManager.isExtensionAvailable to check if the extension mode can be supported. The exception you met is probably caused by enabling extension on an unsupported camera selector. Secondly, if you would like to bind to a camera with specific camera ID, here is a post that you may refer to.

Mohammad Sajib Al Seraj

unread,
Aug 26, 2022, 10:49:40 AM8/26/22
to tonyt...@google.com, Android CameraX Discussion Group, Scott Nien
Okay, I will check it.

Thanks
Mohammad
Reply all
Reply to author
Forward
0 new messages