From bafcb2146c697a054fd36f719ebd49ab0cf5eb5a Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Wed, 23 Oct 2019 20:45:58 +0200 Subject: [PATCH] Small changes --- .../cameraview/CameraOptions.java | 50 ++++++++----------- .../cameraview/engine/Camera2Engine.java | 38 ++++++++------ 2 files changed, 42 insertions(+), 46 deletions(-) diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java index d95a4af8..fb632103 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java @@ -60,8 +60,8 @@ public class CameraOptions { private float exposureCorrectionMinValue; private float exposureCorrectionMaxValue; private boolean autoFocusSupported; - private float fpsRangeMinValue; - private float fpsRangeMaxValue; + private float previewFrameRateMinValue; + private float previewFrameRateMaxValue; public CameraOptions(@NonNull Camera.Parameters params, int cameraId, boolean flipSizes) { @@ -160,8 +160,8 @@ public class CameraOptions { } //fps range - fpsRangeMinValue = 0F; - fpsRangeMaxValue = 0F; + previewFrameRateMinValue = 0F; + previewFrameRateMaxValue = 0F; } // Camera2Engine constructor. @@ -280,21 +280,21 @@ public class CameraOptions { } //fps Range - fpsRangeMinValue = Float.MAX_VALUE; - fpsRangeMaxValue = Float.MIN_VALUE; + previewFrameRateMinValue = Float.MAX_VALUE; + previewFrameRateMaxValue = Float.MIN_VALUE; Range[] range = cameraCharacteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES); if (range != null) { for (Range fpsRange : range) { - if (fpsRange.getLower() <= fpsRangeMinValue) { - fpsRangeMinValue = fpsRange.getLower(); + if (fpsRange.getLower() <= previewFrameRateMinValue) { + previewFrameRateMinValue = fpsRange.getLower(); } - if (fpsRange.getUpper() >= fpsRangeMaxValue) { - fpsRangeMaxValue = fpsRange.getUpper(); + if (fpsRange.getUpper() >= previewFrameRateMaxValue) { + previewFrameRateMaxValue = fpsRange.getUpper(); } } } else { - fpsRangeMinValue = 0F; - fpsRangeMaxValue = 0F; + previewFrameRateMinValue = 0F; + previewFrameRateMaxValue = 0F; } } @@ -308,7 +308,6 @@ public class CameraOptions { return getSupportedControls(control.getClass()).contains(control); } - /** * Shorthand for other methods in this class, * e.g. supports(GestureAction.ZOOM) == isZoomSupported(). @@ -333,7 +332,6 @@ public class CameraOptions { return false; } - @SuppressWarnings("unchecked") @NonNull public Collection getSupportedControls(@NonNull Class controlClass) { @@ -362,7 +360,6 @@ public class CameraOptions { return Collections.emptyList(); } - /** * Set of supported picture sizes for the currently opened camera. * @@ -373,7 +370,6 @@ public class CameraOptions { return Collections.unmodifiableSet(supportedPictureSizes); } - /** * Set of supported picture aspect ratios for the currently opened camera. * @@ -385,7 +381,6 @@ public class CameraOptions { return Collections.unmodifiableSet(supportedPictureAspectRatio); } - /** * Set of supported video sizes for the currently opened camera. * @@ -396,7 +391,6 @@ public class CameraOptions { return Collections.unmodifiableSet(supportedVideoSizes); } - /** * Set of supported picture aspect ratios for the currently opened camera. * @@ -408,7 +402,6 @@ public class CameraOptions { return Collections.unmodifiableSet(supportedVideoAspectRatio); } - /** * Set of supported facing values. * @@ -421,7 +414,6 @@ public class CameraOptions { return Collections.unmodifiableSet(supportedFacing); } - /** * Set of supported flash values. * @@ -436,7 +428,6 @@ public class CameraOptions { return Collections.unmodifiableSet(supportedFlash); } - /** * Set of supported white balance values. * @@ -452,7 +443,6 @@ public class CameraOptions { return Collections.unmodifiableSet(supportedWhiteBalance); } - /** * Set of supported hdr values. * @@ -488,7 +478,6 @@ public class CameraOptions { return autoFocusSupported; } - /** * Whether exposure correction is supported. If this is false, calling * {@link CameraView#setExposureCorrection(float)} has no effect. @@ -501,7 +490,6 @@ public class CameraOptions { return exposureCorrectionSupported; } - /** * The minimum value of negative exposure correction, in EV stops. * This is presumably negative or 0 if not supported. @@ -524,18 +512,20 @@ public class CameraOptions { } /** - * The minimum value for FPS + * The minimum value for the preview frame rate, in frames per second (FPS). + * * @return the min value */ - public float getFpsRangeMinValue() { - return fpsRangeMinValue; + public float getPreviewFrameRateMinValue() { + return previewFrameRateMinValue; } /** - * The maximum value for FPS + * The maximum value for the preview frame rate, in frames per second (FPS). + * * @return the max value */ - public float getFpsRangeMaxValue() { - return fpsRangeMaxValue; + public float getPreviewFrameRateMaxValue() { + return previewFrameRateMaxValue; } } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera2Engine.java b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera2Engine.java index 9df41019..8a6b6851 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera2Engine.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera2Engine.java @@ -83,7 +83,6 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv private static final int FRAME_PROCESSING_FORMAT = ImageFormat.NV21; private static final int FRAME_PROCESSING_INPUT_FORMAT = ImageFormat.YUV_420_888; - private static final int DEFAULT_FRAME_RATE = 30; @VisibleForTesting static final long METER_TIMEOUT = 2500; private final CameraManager mManager; @@ -1273,22 +1272,29 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv } @SuppressWarnings("WeakerAccess") - protected boolean applyPreviewFrameRate(@NonNull CaptureRequest.Builder builder, float oldPreviewFrameRate) { - Range[] fpsRanges = mCameraCharacteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES); - if (fpsRanges != null) { - if (mPreviewFrameRate != 0f) { - for (Range fpsRange : fpsRanges) { - if (fpsRange.contains((int) mPreviewFrameRate)) { - builder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, fpsRange); - return true; - } + protected boolean applyPreviewFrameRate(@NonNull CaptureRequest.Builder builder, + float oldPreviewFrameRate) { + //noinspection unchecked + Range[] fallback = new Range[]{}; + Range[] fpsRanges = readCharacteristic( + CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES, + fallback); + if (mPreviewFrameRate == 0F) { + // 0F is a special value. Fallback to a reasonable default. + for (Range fpsRange : fpsRanges) { + if (fpsRange.contains(30) || fpsRange.contains(24)) { + builder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, fpsRange); + return true; } - } else { - for (Range fpsRange : fpsRanges) { - if (fpsRange.contains(DEFAULT_FRAME_RATE)) { - builder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, fpsRange); - return true; - } + } + } else { + // If out of boundaries, adjust it. + mPreviewFrameRate = Math.min(mPreviewFrameRate, mCameraOptions.getPreviewFrameRateMaxValue()); + mPreviewFrameRate = Math.max(mPreviewFrameRate, mCameraOptions.getPreviewFrameRateMinValue()); + for (Range fpsRange : fpsRanges) { + if (fpsRange.contains(Math.round(mPreviewFrameRate))) { + builder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, fpsRange); + return true; } } }