CameraSelector from ID

1,044 views
Skip to first unread message

Garrett Kajmowicz

unread,
Oct 18, 2021, 6:46:59 PM10/18/21
to Android CameraX Discussion Group
I've created a device/resolution picker using the android.hardware.camera2 APIs.
Now I'm trying to initialize a preview (and ultimately analysis and capture) CameraX usecases.

However, I'm having a bit of a challenge in converting one to the other. There doesn't seem to be a way to get a CameraSelector from the CameraDevice (or better, ID from  CameraDevice.getID()). I looked at the documentation for CameraSelector, CameraInfo and CameraFilter and didn't find a way to do so.

Is there a method I'm missing?

Alternatively, might such an interface be added?

-     Garrett

Trevor McGuire

unread,
Oct 18, 2021, 8:09:01 PM10/18/21
to Garrett Kajmowicz, Android CameraX Discussion Group
Hi Garrett,

If you are trying to get a CameraSelector that selects a camera based on Camera2 Camera ID, you can use the Camera2CameraInfo APIs along with the CameraFilter APIs like so:

val id = ... // Camera ID string retrieved from Camera2
val cameraSelector = CameraSelector.Builder()
    .addCameraFilter {
        it.filter { Camera2CameraInfo.from(it).cameraId == id }
    }.build()


Or alternatively, you can use Camera2CameraInfo along with ProcessCameraProvider.getAvailableCameraInfos():

val cameraSelector =
    processCameraProvider.availableCameraInfos.find {
        Camera2CameraInfo.from(it).cameraId == id
    }?.cameraSelector


The only difference being that the cameraSelector in the first method will throw an exception on ProcessCameraProvider.bindToLifecycle() if the 'id' argument is invalid whereas the second method will return a nulll CameraSelector if the 'id' argument is invalid. However, the 'id' argument shouldn't be invalid if you're getting it directly from Camera2.

Does this answer you question?

-Trevor

--
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/40b6adba-983e-4a92-b4cb-f28da8c7f686n%40android.com.

Garrett Kajmowicz

unread,
Oct 19, 2021, 10:55:15 PM10/19/21
to Android CameraX Discussion Group, trevor...@google.com, Android CameraX Discussion Group, Garrett Kajmowicz
This absolutely addresses this issue.

I'd still suggest adding a CameraId interface, but that can clearly be deprioritized given the solutions provided.

Thank you.

Reply all
Reply to author
Forward
0 new messages