public class

CameraCharacteristicsCompat

extends java.lang.Object

 java.lang.Object

↳androidx.camera.camera2.internal.compat.CameraCharacteristicsCompat

Gradle dependencies

compile group: 'androidx.camera', name: 'camera-camera2', version: '1.5.0-alpha01'

  • groupId: androidx.camera
  • artifactId: camera-camera2
  • version: 1.5.0-alpha01

Artifact androidx.camera:camera-camera2:1.5.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

A wrapper for CameraCharacteristics which caches the retrieved values to optimize the latency and might contain backward compatible fixes for certain parameters.

Summary

Methods
public java.lang.Objectget(<any> key)

Gets a camera characteristics field value and caches the value for later use.

public java.lang.StringgetCameraId()

Returns the camera id associated with the camera characteristics.

public java.util.Set<java.lang.String>getPhysicalCameraIds()

Returns the physical camera Ids if it is a logical camera.

public StreamConfigurationMapCompatgetStreamConfigurationMapCompat()

Obtains the StreamConfigurationMapCompat which contains the output sizes related workarounds in it.

public booleanisZoomOverrideAvailable()

Returns true if overriding zoom setting is available, otherwise false.

public CameraCharacteristicstoCameraCharacteristics()

Returns the CameraCharacteristics represented by this object.

public static CameraCharacteristicsCompattoCameraCharacteristicsCompat(CameraCharacteristics characteristics, java.lang.String cameraId)

Tests might need to create CameraCharacteristicsCompat directly for convenience.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Methods

public static CameraCharacteristicsCompat toCameraCharacteristicsCompat(CameraCharacteristics characteristics, java.lang.String cameraId)

Tests might need to create CameraCharacteristicsCompat directly for convenience. Elsewhere we should get the CameraCharacteristicsCompat instance from CameraManagerCompat.

public java.lang.Object get(<any> key)

Gets a camera characteristics field value and caches the value for later use.

It will cache the value once get() is called. If get() is called more than once using the same key, it will return instantly.

Parameters:

key: The characteristics field to read.

Returns:

The value of that key, or null if the field is not set.

public java.util.Set<java.lang.String> getPhysicalCameraIds()

Returns the physical camera Ids if it is a logical camera. Otherwise it would return an empty set.

public boolean isZoomOverrideAvailable()

Returns true if overriding zoom setting is available, otherwise false.

public StreamConfigurationMapCompat getStreamConfigurationMapCompat()

Obtains the StreamConfigurationMapCompat which contains the output sizes related workarounds in it.

public CameraCharacteristics toCameraCharacteristics()

Returns the CameraCharacteristics represented by this object.

public java.lang.String getCameraId()

Returns the camera id associated with the camera characteristics.

Source

/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.camera.camera2.internal.compat;

import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraMetadata;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.os.Build;

import androidx.annotation.GuardedBy;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.camera.camera2.internal.compat.workaround.OutputSizesCorrector;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * A wrapper for {@link CameraCharacteristics} which caches the retrieved values to optimize
 * the latency and might contain backward compatible fixes for certain parameters.
 */
public class CameraCharacteristicsCompat {
    @NonNull
    @GuardedBy("this")
    private final Map<CameraCharacteristics.Key<?>, Object> mValuesCache = new HashMap<>();
    @NonNull
    private final CameraCharacteristicsCompatImpl mCameraCharacteristicsImpl;
    @NonNull
    private final String mCameraId;

    @Nullable
    private StreamConfigurationMapCompat mStreamConfigurationMapCompat = null;

    private CameraCharacteristicsCompat(@NonNull CameraCharacteristics cameraCharacteristics,
            @NonNull String cameraId) {
        if (Build.VERSION.SDK_INT >= 28) {
            mCameraCharacteristicsImpl = new CameraCharacteristicsApi28Impl(cameraCharacteristics);
        } else {
            mCameraCharacteristicsImpl = new CameraCharacteristicsBaseImpl(cameraCharacteristics);
        }
        mCameraId = cameraId;
    }

    /**
     * Tests might need to create CameraCharacteristicsCompat directly for convenience. Elsewhere
     * we should get the CameraCharacteristicsCompat instance from {@link CameraManagerCompat}.
     */
    @VisibleForTesting
    @NonNull
    public static CameraCharacteristicsCompat toCameraCharacteristicsCompat(
            @NonNull CameraCharacteristics characteristics, @NonNull String cameraId) {
        return new CameraCharacteristicsCompat(characteristics, cameraId);
    }

    /**
     * Return true if the key should be retrieved from {@link CameraCharacteristics} without
     * caching it.
     */
    private boolean isKeyNonCacheable(@NonNull CameraCharacteristics.Key<?> key) {
        // SENSOR_ORIENTATION value should change in some circumstances.
        return key.equals(CameraCharacteristics.SENSOR_ORIENTATION);
    }

    /**
     * Gets a camera characteristics field value and caches the value for later use.
     *
     * <p>It will cache the value once get() is called. If get() is called more than once using
     * the same key, it will return instantly.
     *
     * @param key The characteristics field to read.
     * @return The value of that key, or null if the field is not set.
     */
    @Nullable
    public <T> T get(@NonNull CameraCharacteristics.Key<T> key) {
        // For some keys that will have varying value and cannot be cached, we need to always
        // retrieve the key from the CameraCharacteristics.
        if (isKeyNonCacheable(key)) {
            return mCameraCharacteristicsImpl.get(key);
        }

        synchronized (this) {
            @SuppressWarnings("unchecked") // The value type always matches the key type.
            T value = (T) mValuesCache.get(key);
            if (value != null) {
                return value;
            }

            value = mCameraCharacteristicsImpl.get(key);
            if (value != null) {
                mValuesCache.put(key, value);
            }
            return value;
        }
    }

    /**
     * Returns the physical camera Ids if it is a logical camera. Otherwise it would
     * return an empty set.
     */
    @NonNull
    public Set<String> getPhysicalCameraIds() {
        return mCameraCharacteristicsImpl.getPhysicalCameraIds();
    }

    /**
     * Returns {@code true} if overriding zoom setting is available, otherwise {@code false}.
     */
    public boolean isZoomOverrideAvailable() {
        if (Build.VERSION.SDK_INT >= 34) {
            int[] availableSettingsOverrides = mCameraCharacteristicsImpl.get(
                    CameraCharacteristics.CONTROL_AVAILABLE_SETTINGS_OVERRIDES);
            if (availableSettingsOverrides != null) {
                for (int i : availableSettingsOverrides) {
                    if (i == CameraMetadata.CONTROL_SETTINGS_OVERRIDE_ZOOM) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * Obtains the {@link StreamConfigurationMapCompat} which contains the output sizes related
     * workarounds in it.
     */
    @NonNull
    public StreamConfigurationMapCompat getStreamConfigurationMapCompat() {
        if (mStreamConfigurationMapCompat == null) {
            StreamConfigurationMap map;
            try {
                map = get(
                        CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            } catch (NullPointerException | AssertionError e) {
                // Some devices may throw AssertionError when querying stream configuration map
                // from CameraCharacteristics during bindToLifecycle. Catch the AssertionError and
                // throw IllegalArgumentException so app level can decide how to handle.
                throw new IllegalArgumentException(e.getMessage());
            }
            if (map == null) {
                throw new IllegalArgumentException("StreamConfigurationMap is null!");
            }
            OutputSizesCorrector outputSizesCorrector = new OutputSizesCorrector(mCameraId);
            mStreamConfigurationMapCompat =
                    StreamConfigurationMapCompat.toStreamConfigurationMapCompat(map,
                            outputSizesCorrector);
        }

        return mStreamConfigurationMapCompat;
    }

    /**
     * Returns the {@link CameraCharacteristics} represented by this object.
     */
    @NonNull
    public CameraCharacteristics toCameraCharacteristics() {
        return mCameraCharacteristicsImpl.unwrap();
    }

    /**
     * Returns the camera id associated with the camera characteristics.
     */
    @NonNull
    public String getCameraId() {
        return mCameraId;
    }

    /**
     * CameraCharacteristic Implementation Interface
     */
    public interface CameraCharacteristicsCompatImpl {
        /**
         * Gets the key/values from the CameraCharacteristics.
         */
        @Nullable
        <T> T get(@NonNull CameraCharacteristics.Key<T> key);

        /**
         * Gets physical camera ids.
         */
        @NonNull
        Set<String> getPhysicalCameraIds();

        /**
         * Returns the underlying {@link CameraCharacteristics} instance.
         */
        @NonNull
        CameraCharacteristics unwrap();
    }
}