public class

CaptureRequestOptions

extends java.lang.Object

implements ReadableConfig

 java.lang.Object

↳androidx.camera.camera2.interop.CaptureRequestOptions

Subclasses:

Camera2ImplConfig

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 bundle of Camera2 capture request options.

Summary

Constructors
publicCaptureRequestOptions(Config config)

Creates a CaptureRequestOptions for reading Camera2 capture request options from the given config.

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

Returns a value for the given or null if it hasn't been set.

public java.lang.ObjectgetCaptureRequestOption(<any> key, java.lang.Object valueIfMissing)

Returns a value for the given .

public ConfiggetConfig()

Returns the Config object associated with this CaptureRequestOptions.

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

Constructors

public CaptureRequestOptions(Config config)

Creates a CaptureRequestOptions for reading Camera2 capture request options from the given config.

Parameters:

config: The config that potentially contains Camera2 capture request options.

Methods

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

Returns a value for the given or null if it hasn't been set.

Parameters:

key: The key to retrieve.

Returns:

The stored value or null if the value does not exist in this configuration.

public java.lang.Object getCaptureRequestOption(<any> key, java.lang.Object valueIfMissing)

Returns a value for the given .

Parameters:

key: The key to retrieve.
valueIfMissing: The value to return if this configuration option has not been set.

Returns:

The stored value or valueIfMissing if the value does not exist in this configuration.

public Config getConfig()

Returns the Config object associated with this CaptureRequestOptions.

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.interop;

import android.hardware.camera2.CaptureRequest;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import androidx.camera.camera2.impl.Camera2ImplConfig;
import androidx.camera.core.ExtendableBuilder;
import androidx.camera.core.impl.Config;
import androidx.camera.core.impl.MutableConfig;
import androidx.camera.core.impl.MutableOptionsBundle;
import androidx.camera.core.impl.OptionsBundle;
import androidx.camera.core.impl.ReadableConfig;

/**
 * A bundle of Camera2 capture request options.
 */
@ExperimentalCamera2Interop
public class CaptureRequestOptions implements ReadableConfig {

    private final Config mConfig;

    /**
     * Creates a CaptureRequestOptions for reading Camera2 capture request options from the
     * given config.
     *
     * @param config The config that potentially contains Camera2 capture request options.
     */
    @RestrictTo(Scope.LIBRARY)
    public CaptureRequestOptions(@NonNull Config config) {
        mConfig = config;
    }

    /**
     * Returns a value for the given {@link CaptureRequest.Key} or null if it hasn't been set.
     *
     * @param key            The key to retrieve.
     * @param <ValueT>       The type of the value.
     * @return The stored value or null if the value does not exist in this
     * configuration.
     */
    @Nullable
    public <ValueT> ValueT getCaptureRequestOption(@NonNull CaptureRequest.Key<ValueT> key) {
        @SuppressWarnings(
                "unchecked") // Type should have been only set via Builder#setCaptureRequestOption()
                Option<ValueT> opt = (Option<ValueT>) Camera2ImplConfig.createCaptureRequestOption(
                key);
        return mConfig.retrieveOption(opt, null);
    }

    /**
     * Returns a value for the given {@link CaptureRequest.Key}.
     *
     * @param key            The key to retrieve.
     * @param valueIfMissing The value to return if this configuration option has not been set.
     * @param <ValueT>       The type of the value.
     * @return The stored value or <code>valueIfMissing</code> if the value does not exist in this
     * configuration.
     */
    @RestrictTo(Scope.LIBRARY)
    @Nullable
    public <ValueT> ValueT getCaptureRequestOption(
            @NonNull CaptureRequest.Key<ValueT> key, @Nullable ValueT valueIfMissing) {
        @SuppressWarnings(
                "unchecked") // Type should have been only set via Builder#setCaptureRequestOption()
                Option<ValueT> opt = (Option<ValueT>) Camera2ImplConfig.createCaptureRequestOption(
                key);
        return mConfig.retrieveOption(opt, valueIfMissing);
    }

    /**
     * Returns the {@link Config} object associated with this {@link CaptureRequestOptions}.
     *
     */
    @RestrictTo(Scope.LIBRARY)
    @NonNull
    @Override
    public Config getConfig() {
        return mConfig;
    }

    /**
     * Builder for creating {@link CaptureRequestOptions} instance.
     */
    public static final class Builder implements ExtendableBuilder<CaptureRequestOptions> {

        private final MutableOptionsBundle mMutableOptionsBundle = MutableOptionsBundle.create();

        /**
         * Generates a Builder from another Config object.
         *
         * @param config An immutable configuration to pre-populate this builder.
         * @return The new Builder.
         */
        @RestrictTo(Scope.LIBRARY)
        @NonNull
        public static CaptureRequestOptions.Builder from(@NonNull Config config) {
            CaptureRequestOptions.Builder bundleBuilder = new CaptureRequestOptions.Builder();
            config.findOptions(
                    Camera2ImplConfig.CAPTURE_REQUEST_ID_STEM,
                    option -> {
                        // Erase the type of the option. Capture request options should only be
                        // set via Camera2Interop so that the type of the key and value should
                        // always match.
                        @SuppressWarnings("unchecked")
                        Config.Option<Object> objectOpt = (Config.Option<Object>) option;
                        bundleBuilder.getMutableConfig().insertOption(objectOpt,
                                config.getOptionPriority(objectOpt),
                                config.retrieveOption(objectOpt));
                        return true;
                    });
            return bundleBuilder;
        }

        /**
         * {@inheritDoc}
         *
         */
        @RestrictTo(Scope.LIBRARY)
        @Override
        @NonNull
        public MutableConfig getMutableConfig() {
            return mMutableOptionsBundle;
        }

        /**
         * Inserts new capture request option with specific {@link CaptureRequest.Key} setting.
         */
        @NonNull
        public <ValueT> CaptureRequestOptions.Builder setCaptureRequestOption(
                @NonNull CaptureRequest.Key<ValueT> key, @NonNull ValueT value) {
            Option<Object> opt = Camera2ImplConfig.createCaptureRequestOption(key);
            mMutableOptionsBundle.insertOption(opt, value);
            return this;
        }

        /**
         * Removes a capture request option with specific {@link CaptureRequest.Key} setting.
         */
        @NonNull
        public <ValueT> CaptureRequestOptions.Builder clearCaptureRequestOption(
                @NonNull CaptureRequest.Key<ValueT> key) {
            Config.Option<Object> opt = Camera2ImplConfig.createCaptureRequestOption(key);
            mMutableOptionsBundle.removeOption(opt);
            return this;
        }


        /**
         * Builds an immutable {@link CaptureRequestOptions} from the current state.
         *
         * @return A {@link CaptureRequestOptions} populated with the current state.
         */
        @Override
        @NonNull
        public CaptureRequestOptions build() {
            return new CaptureRequestOptions(OptionsBundle.from(mMutableOptionsBundle));
        }
    }
}