public class

StreamConfigurationMapCompat

extends java.lang.Object

 java.lang.Object

↳androidx.camera.camera2.internal.compat.StreamConfigurationMapCompat

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

Helper for accessing features in in a backwards compatible fashion.

Summary

Methods
public SizegetHighResolutionOutputSizes(int format)

Get a list of high resolution sizes compatible with the requested image format.

public int[]getOutputFormats()

Get the image format output formats in this stream configuration.

public SizegetOutputSizes(java.lang.Class<java.lang.Object> klass)

Get a list of sizes compatible with klass to use as an output.

public SizegetOutputSizes(int format)

Get a list of sizes compatible with the requested image format.

public StreamConfigurationMaptoStreamConfigurationMap()

Returns the represented by this object.

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

Methods

public int[] getOutputFormats()

Get the image format output formats in this stream configuration.

All image formats returned by this function will be defined in either ImageFormat or in PixelFormat.

Returns:

an array of integer format

See also:

public Size getOutputSizes(int format)

Get a list of sizes compatible with the requested image format.

Output sizes related quirks will be applied onto the returned sizes list.

Parameters:

format: an image format from or

Returns:

an array of supported sizes, or null if the format is not a supported output

See also:

public Size getOutputSizes(java.lang.Class<java.lang.Object> klass)

Get a list of sizes compatible with klass to use as an output.

Output sizes related quirks will be applied onto the returned sizes list.

Parameters:

klass: a non-null java.lang.Class object reference

Returns:

an array of supported sizes for format, or null iff the klass is not a supported output.

public Size getHighResolutionOutputSizes(int format)

Get a list of high resolution sizes compatible with the requested image format.

Parameters:

format: an image format from or

Returns:

an array of supported sizes, or null if the format is not a supported output

See also:

public StreamConfigurationMap toStreamConfigurationMap()

Returns the represented by this object.

Source

/*
 * Copyright 2022 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.graphics.ImageFormat;
import android.graphics.PixelFormat;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.os.Build;
import android.util.Size;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.camera.camera2.internal.compat.workaround.OutputSizesCorrector;
import androidx.camera.core.Logger;

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

/**
 * Helper for accessing features in {@link StreamConfigurationMap} in a backwards compatible
 * fashion.
 */
public class StreamConfigurationMapCompat {
    private static final String TAG = "StreamConfigurationMapCompat";

    private final StreamConfigurationMapCompatImpl mImpl;
    private final OutputSizesCorrector mOutputSizesCorrector;
    private final Map<Integer, Size[]> mCachedFormatOutputSizes = new HashMap<>();
    private final Map<Integer, Size[]> mCachedFormatHighResolutionOutputSizes = new HashMap<>();
    private final Map<Class<?>, Size[]> mCachedClassOutputSizes = new HashMap<>();

    private StreamConfigurationMapCompat(@NonNull StreamConfigurationMap map,
            @NonNull OutputSizesCorrector outputSizesCorrector) {
        if (Build.VERSION.SDK_INT >= 23) {
            mImpl = new StreamConfigurationMapCompatApi23Impl(map);
        } else {
            mImpl = new StreamConfigurationMapCompatBaseImpl(map);
        }
        mOutputSizesCorrector = outputSizesCorrector;
    }

    /**
     * Provides a backward-compatible wrapper for {@link StreamConfigurationMap}.
     *
     * @param map {@link StreamConfigurationMap} class to wrap
     * @param outputSizesCorrector {@link OutputSizesCorrector} which can apply the related
     *                                                         workarounds when output sizes are
     *                                                         retrieved.
     * @return wrapped class
     */
    @NonNull
    static StreamConfigurationMapCompat toStreamConfigurationMapCompat(
            @NonNull StreamConfigurationMap map,
            @NonNull OutputSizesCorrector outputSizesCorrector) {
        return new StreamConfigurationMapCompat(map, outputSizesCorrector);
    }


    /**
     * Get the image format output formats in this stream configuration.
     *
     * <p>All image formats returned by this function will be defined in either ImageFormat or in
     * PixelFormat.
     *
     * @return an array of integer format
     * @see ImageFormat
     * @see PixelFormat
     */
    @Nullable
    public int[] getOutputFormats() {
        int[] result = mImpl.getOutputFormats();
        return result == null ? null : result.clone();
    }

    /**
     * Get a list of sizes compatible with the requested image {@code format}.
     *
     * <p>Output sizes related quirks will be applied onto the returned sizes list.
     *
     * @param format an image format from {@link ImageFormat} or {@link PixelFormat}
     * @return an array of supported sizes, or {@code null} if the {@code format} is not a
     * supported output
     * @see ImageFormat
     * @see PixelFormat
     */
    @Nullable
    public Size[] getOutputSizes(int format) {
        if (mCachedFormatOutputSizes.containsKey(format)) {
            Size[] cachedOutputSizes = mCachedFormatOutputSizes.get(format);
            return cachedOutputSizes == null ? null : mCachedFormatOutputSizes.get(format).clone();
        }

        Size[] outputSizes = mImpl.getOutputSizes(format);

        if (outputSizes == null || outputSizes.length == 0) {
            Logger.w(TAG, "Retrieved output sizes array is null or empty for format " + format);
            return outputSizes;
        }

        outputSizes = mOutputSizesCorrector.applyQuirks(outputSizes, format);
        mCachedFormatOutputSizes.put(format, outputSizes);
        return outputSizes.clone();
    }

    /**
     * Get a list of sizes compatible with {@code klass} to use as an output.
     *
     * <p>Output sizes related quirks will be applied onto the returned sizes list.
     *
     * @param klass a non-{@code null} {@link Class} object reference
     * @return an array of supported sizes for {@link ImageFormat#PRIVATE} format,
     * or {@code null} iff the {@code klass} is not a supported output.
     * @throws NullPointerException if {@code klass} was {@code null}
     */
    @Nullable
    public <T> Size[] getOutputSizes(@NonNull Class<T> klass) {
        if (mCachedClassOutputSizes.containsKey(klass)) {
            Size[] cachedOutputSizes = mCachedClassOutputSizes.get(klass);
            return cachedOutputSizes == null ? null : mCachedClassOutputSizes.get(klass).clone();
        }

        Size[] outputSizes = mImpl.getOutputSizes(klass);

        if (outputSizes == null || outputSizes.length == 0) {
            Logger.w(TAG, "Retrieved output sizes array is null or empty for class " + klass);
            return outputSizes;
        }

        outputSizes = mOutputSizesCorrector.applyQuirks(outputSizes, klass);
        mCachedClassOutputSizes.put(klass, outputSizes);
        return outputSizes.clone();
    }

    /**
     * Get a list of high resolution sizes compatible with the requested image {@code format}.
     *
     * @param format an image format from {@link ImageFormat} or {@link PixelFormat}
     * @return an array of supported sizes, or {@code null} if the {@code format} is not a
     * supported output
     * @see ImageFormat
     * @see PixelFormat
     */
    @Nullable
    public Size[] getHighResolutionOutputSizes(int format) {
        if (mCachedFormatHighResolutionOutputSizes.containsKey(format)) {
            Size[] cachedOutputSizes = mCachedFormatHighResolutionOutputSizes.get(format);
            return cachedOutputSizes == null ? null : mCachedFormatHighResolutionOutputSizes.get(
                    format).clone();
        }

        Size[] outputSizes = mImpl.getHighResolutionOutputSizes(format);

        // High resolution output sizes can be null.
        if (outputSizes != null && outputSizes.length > 0) {
            outputSizes = mOutputSizesCorrector.applyQuirks(outputSizes, format);
        }

        mCachedFormatHighResolutionOutputSizes.put(format, outputSizes);
        return outputSizes != null ? outputSizes.clone() : null;
    }

    /**
     * Returns the {@link StreamConfigurationMap} represented by this object.
     */
    @NonNull
    public StreamConfigurationMap toStreamConfigurationMap() {
        return mImpl.unwrap();
    }

    interface StreamConfigurationMapCompatImpl {

        @Nullable
        int[] getOutputFormats();

        @Nullable
        Size[] getOutputSizes(int format);

        @Nullable
        <T> Size[] getOutputSizes(@NonNull Class<T> klass);

        @Nullable
        Size[] getHighResolutionOutputSizes(int format);

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