public class

SurfaceOrientedMeteringPointFactory

extends MeteringPointFactory

 java.lang.Object

androidx.camera.core.MeteringPointFactory

↳androidx.camera.core.SurfaceOrientedMeteringPointFactory

Gradle dependencies

compile group: 'androidx.camera', name: 'camera-core', version: '1.2.0-alpha01'

  • groupId: androidx.camera
  • artifactId: camera-core
  • version: 1.2.0-alpha01

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

Overview

A MeteringPointFactory that can create MeteringPoint by surface oriented x, y on an area defined by (0, 0) - (width, height). MeteringPoint can then be used to construct a FocusMeteringAction to start a focus and metering action.

The MeteringPoint defines a normalized coordinate system whose left-top is (0, 0) and right-bottom is (1.0, 1.0). This coordinate system is the normalized coordinate system of a of certain aspect ratio. SurfaceOrientedMeteringPointFactory is the simplest factory to create this normalized (x, y) by dividing the (x, y) with (width, height).

This factory is suitable for apps that already have coordinates converted into surface oriented coordinates. It is also useful for apps that want to focus on something detected in ImageAnalysis. Apps can pass the ImageAnalysis instance for useCaseForSurface argument and CameraX will then adjust the final sensor coordinates by aspect ratio of ImageAnalysis.

Summary

Constructors
publicSurfaceOrientedMeteringPointFactory(float width, float height)

Creates the SurfaceOrientedMeteringPointFactory by width and height

publicSurfaceOrientedMeteringPointFactory(float width, float height, UseCase useCaseForAspectRatio)

Creates the SurfaceOrientedMeteringPointFactory by width, height and the surface aspect ratio.

Methods
protected PointFconvertPoint(float x, float y)

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

Constructors

public SurfaceOrientedMeteringPointFactory(float width, float height)

Creates the SurfaceOrientedMeteringPointFactory by width and height

The width/height is the width/height in surface orientation which defines an area (0, 0) - (width, height) within which apps can specify metering points by MeteringPointFactory.createPoint(float, float). Setting width and height to 1.0 will allow points to be created by specifying normalized coordinates.

By default, it will use active Preview to get the surface aspect ratio for final coordinates conversion.

Parameters:

width: the width of the area in surface orientation.
height: the height of the area in surface orientation.

public SurfaceOrientedMeteringPointFactory(float width, float height, UseCase useCaseForAspectRatio)

Creates the SurfaceOrientedMeteringPointFactory by width, height and the surface aspect ratio. The surface aspect ratio is retrieved from the UseCase.

The width/height is the width/height in surface orientation which defines an area (0, 0) - (width, height) within which apps can specify metering points by MeteringPointFactory.createPoint(float, float). Setting width and height to 1.0 will allow points to be created by specifying normalized coordinates.

A UseCase is passed in order to determine the surface aspect ratio for final coordinates conversion. This use case needs to be bound at the time this method is called, otherwise an java.lang.IllegalStateException will be thrown.

Parameters:

width: the width of the area in surface orientation.
height: the height of the area in surface orientation.
useCaseForAspectRatio: the UseCase to get the surface aspect ratio.

Methods

protected PointF convertPoint(float x, float y)

Source

/*
 * Copyright 2019 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.core;

import android.graphics.PointF;
import android.util.Rational;
import android.util.Size;
import android.view.Surface;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;

/**
 * A {@link MeteringPointFactory} that can create {@link MeteringPoint} by surface oriented x, y
 * on an area defined by (0, 0) - (width, height). {@link MeteringPoint} can then be used to
 * construct a {@link FocusMeteringAction} to start a focus and metering action.
 *
 * <p>The {@link MeteringPoint} defines a normalized coordinate system whose left-top is (0, 0)
 * and right-bottom is (1.0, 1.0). This coordinate system is the normalized coordinate
 * system of a {@link Surface} of certain aspect ratio.
 * {@link SurfaceOrientedMeteringPointFactory} is the simplest factory to create this normalized
 * (x, y) by dividing the (x, y) with (width, height).
 *
 * <p>This factory is suitable for apps that already have coordinates converted into surface
 * oriented coordinates. It is also useful for apps that want to focus on something detected in
 * {@link ImageAnalysis}. Apps can pass the {@link ImageAnalysis} instance for useCaseForSurface
 * argument and CameraX will then adjust the final sensor coordinates by aspect ratio of
 * ImageAnalysis.
 *
 * @see MeteringPoint
 */
@RequiresApi(21) // TODO(b/200306659): Remove and replace with annotation on package-info.java
public class SurfaceOrientedMeteringPointFactory extends MeteringPointFactory {
    /** the width of the area in surface orientation */
    private final float mWidth;
    /** The height of the area in surface orientation */
    private final float mHeight;

    /**
     * Creates the {@link SurfaceOrientedMeteringPointFactory} by width and height
     *
     * <p>The width/height is the width/height in surface orientation which defines an area (0, 0)
     * - (width, height) within which apps can specify metering points by
     * {@link #createPoint(float, float)}. Setting width and height to 1.0 will allow points to
     * be created by specifying normalized coordinates.
     *
     * <p>By default, it will use active {@link Preview} to get the surface aspect ratio for final
     * coordinates conversion.
     *
     * @param width the width of the area in surface orientation.
     * @param height the height of the area in surface orientation.
     */
    public SurfaceOrientedMeteringPointFactory(float width, float height) {
        mWidth = width;
        mHeight = height;
    }

    /**
     * Creates the {@link SurfaceOrientedMeteringPointFactory} by width, height and the surface
     * aspect ratio. The surface aspect ratio is retrieved from the {@link UseCase}.
     *
     * <p>The width/height is the width/height in surface orientation which defines an
     * area (0, 0) - (width, height) within which apps can specify metering points by
     * {@link #createPoint(float, float)}. Setting width and height to 1.0 will allow points to
     * be created by specifying normalized coordinates.
     *
     * <p>A {@link UseCase} is passed in order to determine the surface aspect ratio for final
     * coordinates conversion. This use case needs to be bound at the time this method is called,
     * otherwise an {@link IllegalStateException} will be thrown.
     *
     * @param width the width of the area in surface orientation.
     * @param height the height of the area in surface orientation.
     * @param useCaseForAspectRatio the {@link UseCase} to get the surface aspect ratio.
     */
    public SurfaceOrientedMeteringPointFactory(float width, float height,
            @NonNull UseCase useCaseForAspectRatio) {
        super(getUseCaseAspectRatio(useCaseForAspectRatio));
        mWidth = width;
        mHeight = height;
    }

    @Nullable
    private static Rational getUseCaseAspectRatio(@Nullable UseCase useCase) {
        if (useCase == null) {
            return null;
        }

        Size resolution = useCase.getAttachedSurfaceResolution();
        if (resolution == null) {
            throw new IllegalStateException("UseCase " + useCase + " is not bound.");
        }

        // Returns an aspect ratio of first found attachedSurfaceResolution.
        return new Rational(resolution.getWidth(), resolution.getHeight());
    }

    /**
     * {@inheritDoc}
     *
     * @hide
     */
    @Override
    @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    @NonNull
    protected PointF convertPoint(float x, float y) {
        PointF pt = new PointF(x / mWidth, y / mHeight);
        return pt;
    }
}