|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +package io.flutter.plugins.camera; |
| 6 | + |
| 7 | +import android.annotation.TargetApi; |
| 8 | +import android.hardware.camera2.CaptureRequest; |
| 9 | +import android.hardware.camera2.params.MeteringRectangle; |
| 10 | +import android.os.Build; |
| 11 | +import android.util.Size; |
| 12 | +import androidx.annotation.NonNull; |
| 13 | +import androidx.annotation.VisibleForTesting; |
| 14 | +import java.util.Arrays; |
| 15 | + |
| 16 | +/** |
| 17 | + * Utility class offering functions to calculate values regarding the camera boundaries. |
| 18 | + * |
| 19 | + * <p>The functions are used to calculate focus and exposure settings. |
| 20 | + */ |
| 21 | +public final class CameraRegionUtils { |
| 22 | + |
| 23 | + /** |
| 24 | + * Obtains the boundaries for the currently active camera, that can be used for calculating |
| 25 | + * MeteringRectangle instances required for setting focus or exposure settings. |
| 26 | + * |
| 27 | + * @param cameraProperties - Collection of the characteristics for the current camera device. |
| 28 | + * @param requestBuilder - The request builder for the current capture request. |
| 29 | + * @return The boundaries for the current camera device. |
| 30 | + */ |
| 31 | + public static Size getCameraBoundaries( |
| 32 | + @NonNull CameraProperties cameraProperties, @NonNull CaptureRequest.Builder requestBuilder) { |
| 33 | + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.P |
| 34 | + && supportsDistortionCorrection(cameraProperties)) { |
| 35 | + // Get the current distortion correction mode. |
| 36 | + Integer distortionCorrectionMode = |
| 37 | + requestBuilder.get(CaptureRequest.DISTORTION_CORRECTION_MODE); |
| 38 | + |
| 39 | + // Return the correct boundaries depending on the mode. |
| 40 | + android.graphics.Rect rect; |
| 41 | + if (distortionCorrectionMode == null |
| 42 | + || distortionCorrectionMode == CaptureRequest.DISTORTION_CORRECTION_MODE_OFF) { |
| 43 | + rect = cameraProperties.getSensorInfoPreCorrectionActiveArraySize(); |
| 44 | + } else { |
| 45 | + rect = cameraProperties.getSensorInfoActiveArraySize(); |
| 46 | + } |
| 47 | + |
| 48 | + return SizeFactory.create(rect.width(), rect.height()); |
| 49 | + } else { |
| 50 | + // No distortion correction support. |
| 51 | + return cameraProperties.getSensorInfoPixelArraySize(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Converts a point into a {@link MeteringRectangle} with the supplied coordinates as the center |
| 57 | + * point. |
| 58 | + * |
| 59 | + * <p>Since the Camera API (due to cross-platform constraints) only accepts a point when |
| 60 | + * configuring a specific focus or exposure area and Android requires a rectangle to configure |
| 61 | + * these settings there is a need to convert the point into a rectangle. This method will create |
| 62 | + * the required rectangle with an arbitrarily size that is a 10th of the current viewport and the |
| 63 | + * coordinates as the center point. |
| 64 | + * |
| 65 | + * @param boundaries - The camera boundaries to calculate the metering rectangle for. |
| 66 | + * @param x x - 1 >= coordinate >= 0. |
| 67 | + * @param y y - 1 >= coordinate >= 0. |
| 68 | + * @return The dimensions of the metering rectangle based on the supplied coordinates and |
| 69 | + * boundaries. |
| 70 | + */ |
| 71 | + public static MeteringRectangle convertPointToMeteringRectangle( |
| 72 | + @NonNull Size boundaries, double x, double y) { |
| 73 | + assert (boundaries.getWidth() > 0 && boundaries.getHeight() > 0); |
| 74 | + assert (x >= 0 && x <= 1); |
| 75 | + assert (y >= 0 && y <= 1); |
| 76 | + |
| 77 | + // Interpolate the target coordinate. |
| 78 | + int targetX = (int) Math.round(x * ((double) (boundaries.getWidth() - 1))); |
| 79 | + int targetY = (int) Math.round(y * ((double) (boundaries.getHeight() - 1))); |
| 80 | + // Determine the dimensions of the metering rectangle (10th of the viewport). |
| 81 | + int targetWidth = (int) Math.round(((double) boundaries.getWidth()) / 10d); |
| 82 | + int targetHeight = (int) Math.round(((double) boundaries.getHeight()) / 10d); |
| 83 | + // Adjust target coordinate to represent top-left corner of metering rectangle. |
| 84 | + targetX -= targetWidth / 2; |
| 85 | + targetY -= targetHeight / 2; |
| 86 | + // Adjust target coordinate as to not fall out of bounds. |
| 87 | + if (targetX < 0) { |
| 88 | + targetX = 0; |
| 89 | + } |
| 90 | + if (targetY < 0) { |
| 91 | + targetY = 0; |
| 92 | + } |
| 93 | + int maxTargetX = boundaries.getWidth() - 1 - targetWidth; |
| 94 | + int maxTargetY = boundaries.getHeight() - 1 - targetHeight; |
| 95 | + if (targetX > maxTargetX) { |
| 96 | + targetX = maxTargetX; |
| 97 | + } |
| 98 | + if (targetY > maxTargetY) { |
| 99 | + targetY = maxTargetY; |
| 100 | + } |
| 101 | + |
| 102 | + // Build the metering rectangle. |
| 103 | + return MeteringRectangleFactory.create(targetX, targetY, targetWidth, targetHeight, 1); |
| 104 | + } |
| 105 | + |
| 106 | + @TargetApi(Build.VERSION_CODES.P) |
| 107 | + private static boolean supportsDistortionCorrection(CameraProperties cameraProperties) { |
| 108 | + int[] availableDistortionCorrectionModes = |
| 109 | + cameraProperties.getDistortionCorrectionAvailableModes(); |
| 110 | + if (availableDistortionCorrectionModes == null) { |
| 111 | + availableDistortionCorrectionModes = new int[0]; |
| 112 | + } |
| 113 | + long nonOffModesSupported = |
| 114 | + Arrays.stream(availableDistortionCorrectionModes) |
| 115 | + .filter((value) -> value != CaptureRequest.DISTORTION_CORRECTION_MODE_OFF) |
| 116 | + .count(); |
| 117 | + return nonOffModesSupported > 0; |
| 118 | + } |
| 119 | + |
| 120 | + /** Factory class that assists in creating a {@link MeteringRectangle} instance. */ |
| 121 | + static class MeteringRectangleFactory { |
| 122 | + /** |
| 123 | + * Creates a new instance of the {@link MeteringRectangle} class. |
| 124 | + * |
| 125 | + * <p>This method is visible for testing purposes only and should never be used outside this * |
| 126 | + * class. |
| 127 | + * |
| 128 | + * @param x coordinate >= 0. |
| 129 | + * @param y coordinate >= 0. |
| 130 | + * @param width width >= 0. |
| 131 | + * @param height height >= 0. |
| 132 | + * @param meteringWeight weight between {@value MeteringRectangle#METERING_WEIGHT_MIN} and |
| 133 | + * {@value MeteringRectangle#METERING_WEIGHT_MAX} inclusively |
| 134 | + * @return new instance of the {@link MeteringRectangle} class. |
| 135 | + * @throws IllegalArgumentException if any of the parameters were negative. |
| 136 | + */ |
| 137 | + @VisibleForTesting |
| 138 | + public static MeteringRectangle create( |
| 139 | + int x, int y, int width, int height, int meteringWeight) { |
| 140 | + return new MeteringRectangle(x, y, width, height, meteringWeight); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /** Factory class that assists in creating a {@link Size} instance. */ |
| 145 | + static class SizeFactory { |
| 146 | + /** |
| 147 | + * Creates a new instance of the {@link Size} class. |
| 148 | + * |
| 149 | + * <p>This method is visible for testing purposes only and should never be used outside this * |
| 150 | + * class. |
| 151 | + * |
| 152 | + * @param width width >= 0. |
| 153 | + * @param height height >= 0. |
| 154 | + * @return new instance of the {@link Size} class. |
| 155 | + */ |
| 156 | + @VisibleForTesting |
| 157 | + public static Size create(int width, int height) { |
| 158 | + return new Size(width, height); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments