Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 170c336

Browse files
committed
comment cleanup for min and max zoom ratio functions
1 parent 54ec6b4 commit 170c336

File tree

5 files changed

+64
-9
lines changed

5 files changed

+64
-9
lines changed

packages/camera/camera_android/android/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ android {
5959
}
6060

6161
dependencies {
62-
testImplementation project(path: ':camera_android')
6362
compileOnly 'androidx.annotation:annotation:1.1.0'
6463
testImplementation 'junit:junit:4.13.2'
6564
testImplementation 'org.mockito:mockito-inline:4.6.1'

packages/camera/camera_android/android/src/main/java/io/flutter/plugins/camera/CameraProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public interface CameraProperties {
147147
* height and crop region height, for @see android.scaler.cropRegion.
148148
*
149149
* <p>By default maps to the @see
150-
* android.hardware.camera2.CameraCharacteristics#SCALER_AVAILABLE_MAX_DIGITAL_ZOOM key
150+
* android.hardware.camera2.CameraCharacteristics#SCALER_AVAILABLE_MAX_DIGITAL_ZOOM key.
151151
*
152152
* @return Float Maximum ratio between both active area width and crop region width, and active
153153
* area height and crop region height.

packages/camera/camera_android/android/src/main/java/io/flutter/plugins/camera/features/zoomlevel/ZoomLevelFeature.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212

1313
/** Controls the zoom configuration on the {@link android.hardware.camera2} API. */
1414
public class ZoomLevelFeature extends CameraFeature<Float> {
15+
private static final Float defaultZoomLevel = 1.0f;
1516
private final boolean hasSupport;
1617
private final Rect sensorArraySize;
17-
private Float currentSetting = 1.0f;
18+
private Float currentSetting = defaultZoomLevel;
1819
private Float minimumZoomLevel = currentSetting;
1920
private Float maximumZoomLevel;
2021

@@ -33,12 +34,12 @@ public ZoomLevelFeature(CameraProperties cameraProperties) {
3334
hasSupport = false;
3435
return;
3536
}
36-
37-
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.R) {
37+
// On Android 11+ CONTROL_ZOOM_RATIO_RANGE should be use to get the zoom ratio directly
38+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
3839
minimumZoomLevel = cameraProperties.getScalerMinZoomRatio();
3940
maximumZoomLevel = cameraProperties.getScalerMaxZoomRatio();
4041
} else {
41-
minimumZoomLevel = 1.0f;
42+
minimumZoomLevel = defaultZoomLevel;
4243
Float maxDigitalZoom = cameraProperties.getScalerAvailableMaxDigitalZoom();
4344
maximumZoomLevel =
4445
((maxDigitalZoom == null) || (maxDigitalZoom < minimumZoomLevel))
@@ -74,7 +75,8 @@ public void updateBuilder(CaptureRequest.Builder requestBuilder) {
7475
if (!checkIsSupported()) {
7576
return;
7677
}
77-
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.R) {
78+
// On Android 11+ CONTROL_ZOOM_RATIO should be use to get the zoom ratio directly
79+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
7880
requestBuilder.set(
7981
CaptureRequest.CONTROL_ZOOM_RATIO,
8082
ZoomUtils.computeZoomRatio(currentSetting, minimumZoomLevel, maximumZoomLevel));

packages/camera/camera_android/android/src/main/java/io/flutter/plugins/camera/features/zoomlevel/ZoomUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ final class ZoomUtils {
1818
* Computes an image sensor area based on the supplied zoom settings.
1919
*
2020
* <p>The returned image sensor area can be applied to the {@link android.hardware.camera2} API in
21-
* order to control zoom levels.
21+
* order to control zoom levels. This method of zoom should only be used for android versions less
22+
* than than 11 as past that the newer computeZoomRatio functional can be used.
2223
*
2324
* @param zoom The desired zoom level.
2425
* @param sensorArraySize The current area of the image sensor.
@@ -28,7 +29,7 @@ final class ZoomUtils {
2829
*/
2930
static Rect computeZoomRect(
3031
float zoom, @NonNull Rect sensorArraySize, float minimumZoomLevel, float maximumZoomLevel) {
31-
final float newZoom = MathUtils.clamp(zoom, minimumZoomLevel, maximumZoomLevel);
32+
final float newZoom = computeZoomRatio(zoom, minimumZoomLevel, maximumZoomLevel);
3233

3334
final int centerX = sensorArraySize.width() / 2;
3435
final int centerY = sensorArraySize.height() / 2;

packages/camera/camera_android/android/src/test/java/io/flutter/plugins/camera/features/zoomlevel/ZoomLevelFeatureTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818

1919
import android.graphics.Rect;
2020
import android.hardware.camera2.CaptureRequest;
21+
import android.os.Build;
2122
import io.flutter.plugins.camera.CameraProperties;
23+
import java.lang.reflect.Field;
24+
import java.lang.reflect.Modifier;
2225
import org.junit.After;
2326
import org.junit.Before;
2427
import org.junit.Test;
@@ -147,6 +150,22 @@ public void updateBuilder_shouldSetScalarCropRegionWhenCheckIsSupportIsTrue() {
147150
verify(mockBuilder, times(1)).set(CaptureRequest.SCALER_CROP_REGION, mockZoomArea);
148151
}
149152

153+
@Test
154+
public void updateBuilder_shouldControlZoomRatioWhenCheckIsSupportIsTrue() throws Exception {
155+
setSdkVersion(Build.VERSION_CODES.R);
156+
when(mockCameraProperties.getSensorInfoActiveArraySize()).thenReturn(mockSensorArray);
157+
when(mockCameraProperties.getScalerMaxZoomRatio()).thenReturn(42f);
158+
when(mockCameraProperties.getScalerMinZoomRatio()).thenReturn(1.0f);
159+
160+
ZoomLevelFeature zoomLevelFeature = new ZoomLevelFeature(mockCameraProperties);
161+
162+
CaptureRequest.Builder mockBuilder = mock(CaptureRequest.Builder.class);
163+
164+
zoomLevelFeature.updateBuilder(mockBuilder);
165+
166+
verify(mockBuilder, times(1)).set(CaptureRequest.CONTROL_ZOOM_RATIO, 0.0f);
167+
}
168+
150169
@Test
151170
public void getMinimumZoomLevel() {
152171
ZoomLevelFeature zoomLevelFeature = new ZoomLevelFeature(mockCameraProperties);
@@ -163,4 +182,38 @@ public void getMaximumZoomLevel() {
163182

164183
assertEquals(42f, zoomLevelFeature.getMaximumZoomLevel(), 0);
165184
}
185+
186+
@Test
187+
public void checkZoomLevelFeature_callsMaxDigitalZoomOnAndroidQ() throws Exception {
188+
setSdkVersion(Build.VERSION_CODES.Q);
189+
190+
when(mockCameraProperties.getSensorInfoActiveArraySize()).thenReturn(mockSensorArray);
191+
192+
new ZoomLevelFeature(mockCameraProperties);
193+
194+
verify(mockCameraProperties, times(0)).getScalerMaxZoomRatio();
195+
verify(mockCameraProperties, times(0)).getScalerMinZoomRatio();
196+
verify(mockCameraProperties, times(1)).getScalerAvailableMaxDigitalZoom();
197+
}
198+
199+
@Test
200+
public void checkZoomLevelFeature_callsScalarMaxZoomRatioOnAndroidR() throws Exception {
201+
setSdkVersion(Build.VERSION_CODES.R);
202+
when(mockCameraProperties.getSensorInfoActiveArraySize()).thenReturn(mockSensorArray);
203+
204+
new ZoomLevelFeature(mockCameraProperties);
205+
206+
verify(mockCameraProperties, times(1)).getScalerMaxZoomRatio();
207+
verify(mockCameraProperties, times(1)).getScalerMinZoomRatio();
208+
verify(mockCameraProperties, times(0)).getScalerAvailableMaxDigitalZoom();
209+
}
210+
211+
static void setSdkVersion(int sdkVersion) throws Exception {
212+
Field sdkInt = Build.VERSION.class.getField("SDK_INT");
213+
sdkInt.setAccessible(true);
214+
Field modifiersField = Field.class.getDeclaredField("modifiers");
215+
modifiersField.setAccessible(true);
216+
modifiersField.setInt(sdkInt, sdkInt.getModifiers() & ~Modifier.FINAL);
217+
sdkInt.set(null, sdkVersion);
218+
}
166219
}

0 commit comments

Comments
 (0)