CameraDeviceSetupCompatFactory


public class CameraDeviceSetupCompatFactory


Factory for creating CameraDeviceSetupCompat instances.

Summary

Public constructors

Creates a new instance of CameraDeviceSetupCompatFactory.

Public methods

@NonNull CameraDeviceSetupCompat

Gets a new instance of CameraDeviceSetupCompat for the given camera ID.

Public constructors

CameraDeviceSetupCompatFactory

Added in 1.4.0
public CameraDeviceSetupCompatFactory(@NonNull Context context)

Creates a new instance of CameraDeviceSetupCompatFactory.

Parameters
@NonNull Context context

The context to use for creating CameraDeviceSetupCompat instances.

Public methods

getCameraDeviceSetupCompat

Added in 1.4.0
public @NonNull CameraDeviceSetupCompat getCameraDeviceSetupCompat(@NonNull String cameraId)

Gets a new instance of CameraDeviceSetupCompat for the given camera ID.

The returned instance aggregates the results from both the Play Services and the Android framework. It first checks if a Play Services implementation exists, and if so, return the query result from the Play Services implementation. If no Play Services implementation exists or the result from Play Services is undefined, the returned instance will then query Android framework for the result, if running on a new enough version. Sample code:

// Query the compatibility before opening the camera.
CameraDeviceSetupCompatFactory factory = new CameraDeviceSetupCompatFactory(context);
CameraDeviceSetupCompat cameraDeviceSetupCompat
    = factory.getCameraDeviceSetupCompat(cameraId);
Result result
    = cameraDeviceSetupCompat.isSessionConfigurationSupported(sessionConfiguration);
boolean supported = result.getValue() == CameraDeviceSetupCompat.RESULT_SUPPORTED;

To include the Play Services as a source, the app must depend on the camera-feature-combination-query-play-services artifact.

If the return value is RESULT_UNDEFINED, on API level between 29 and 34 inclusive, it's also possible to further check if the session configuration is supported by querying the isSessionConfigurationSupported. This approach requires opening the camera first which may introduce latency, so the AndroidX implementation does not include this code path by default. Additionally, this approach does not check the values set in setSessionParameters. For example, the FPS range is ignored. Sample code:

if (SDK_INT <= 34 && SDK_INT >= 29) {
  // Check if the session configuration is supported with an opened CameraDevice.
  try {
    supported = supported || (result.getValue() == RESULT_UNDEFINED &&
        cameraDevice.isSessionConfigurationSupported(sessionConfiguration));
  } catch (UnsupportedOperationException unsupportedException) {
    // CameraDevice may throw UnsupportedOperationException when the config is not supported.
  }
}
Throws
java.lang.IllegalStateException

If the Play Services implementation exists but the library fails to instantiate it. For example, if there are multiple Play Services implementations in the manifest.

java.lang.IllegalArgumentException

If cameraId is null, or if cameraId does not match any device in getCameraIdList.

android.hardware.camera2.CameraAccessException

If the camera has encountered a fatal error.