Search code examples
androidandroid-camera2android-camerax

Is it possible to combine Preview, ImageAnalysis, and VideoCapture with CameraX?


Is it possible to bind Preview, ImageAnalysis, and VideoCapture at the same time? Combined in pairs, all possible combinations (ie. Preview+ImageAnalysis, Preview+VideoCapture, and ImageAnalysis+VideoCapture) work perfectly fine but binding all three together throws the following exception.

java.lang.IllegalArgumentException: No supported surface combination is found for camera device - Id : 1.  May be attempting to bind too many use cases.
    at androidx.camera.camera2.internal.Camera2DeviceSurfaceManager.getSuggestedResolutions(Camera2DeviceSurfaceManager.java:193)
    at androidx.camera.core.CameraX.calculateSuggestedResolutions(CameraX.java:943)
    at androidx.camera.core.CameraX.bindToLifecycle(CameraX.java:293)
    at androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle(ProcessCameraProvider.java:229)

Is there any workaround that allows me to use ImageAnalysis and at the same time record a video?


Solution

  • There are limits to the number of simultaneous outputs that are due to the camera hardware pipeline's scaler unit limitations.

    In general, 3 parallel YUV outputs aren't supported outside of high-end devices.

    You can look at the many tables here: https://developer.android.com/reference/android/hardware/camera2/CameraDevice#createCaptureSession(android.hardware.camera2.params.SessionConfiguration)

    and see what kinds of parallel outputs can be set up, on the various hardware levels (PRIV and YUV are what these usecases operate on).

    A FULL device can do it, as long as the analysis runs at 640x480 or lower. But LEGACY and LIMITED cannot.

    That said, you can use ImageAnalysis frames to drive a preview, but it's certainly a bit less convenient - may require using the NDK or other annoyances.