forked from flutter/plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[camera] Zoom functionality for Android and iOS (flutter#3315)
* Refactored and tested zoom on Android * Fix merge conflict
- Loading branch information
1 parent
414d47a
commit 98699b0
Showing
13 changed files
with
662 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/CameraZoom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.flutter.plugins.camera; | ||
|
||
import android.graphics.Rect; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.core.math.MathUtils; | ||
|
||
public final class CameraZoom { | ||
public static final float DEFAULT_ZOOM_FACTOR = 1.0f; | ||
|
||
@NonNull private final Rect cropRegion = new Rect(); | ||
@Nullable private final Rect sensorSize; | ||
|
||
public final float maxZoom; | ||
public final boolean hasSupport; | ||
|
||
public CameraZoom(@Nullable final Rect sensorArraySize, final Float maxZoom) { | ||
this.sensorSize = sensorArraySize; | ||
|
||
if (this.sensorSize == null) { | ||
this.maxZoom = DEFAULT_ZOOM_FACTOR; | ||
this.hasSupport = false; | ||
return; | ||
} | ||
|
||
this.maxZoom = | ||
((maxZoom == null) || (maxZoom < DEFAULT_ZOOM_FACTOR)) ? DEFAULT_ZOOM_FACTOR : maxZoom; | ||
|
||
this.hasSupport = (Float.compare(this.maxZoom, DEFAULT_ZOOM_FACTOR) > 0); | ||
} | ||
|
||
public Rect computeZoom(final float zoom) { | ||
if (sensorSize == null || !this.hasSupport) { | ||
return null; | ||
} | ||
|
||
final float newZoom = MathUtils.clamp(zoom, DEFAULT_ZOOM_FACTOR, this.maxZoom); | ||
|
||
final int centerX = this.sensorSize.width() / 2; | ||
final int centerY = this.sensorSize.height() / 2; | ||
final int deltaX = (int) ((0.5f * this.sensorSize.width()) / newZoom); | ||
final int deltaY = (int) ((0.5f * this.sensorSize.height()) / newZoom); | ||
|
||
this.cropRegion.set(centerX - deltaX, centerY - deltaY, centerX + deltaX, centerY + deltaY); | ||
|
||
return cropRegion; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
packages/camera/camera/android/src/test/java/io/flutter/plugins/camera/CameraZoomTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package io.flutter.plugins.camera; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import android.graphics.Rect; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
|
||
@RunWith(RobolectricTestRunner.class) | ||
public class CameraZoomTest { | ||
|
||
@Test | ||
public void ctor_when_parameters_are_valid() { | ||
final Rect sensorSize = new Rect(0, 0, 0, 0); | ||
final Float maxZoom = 4.0f; | ||
final CameraZoom cameraZoom = new CameraZoom(sensorSize, maxZoom); | ||
|
||
assertNotNull(cameraZoom); | ||
assertTrue(cameraZoom.hasSupport); | ||
assertEquals(4.0f, cameraZoom.maxZoom, 0); | ||
assertEquals(1.0f, CameraZoom.DEFAULT_ZOOM_FACTOR, 0); | ||
} | ||
|
||
@Test | ||
public void ctor_when_sensor_size_is_null() { | ||
final Rect sensorSize = null; | ||
final Float maxZoom = 4.0f; | ||
final CameraZoom cameraZoom = new CameraZoom(sensorSize, maxZoom); | ||
|
||
assertNotNull(cameraZoom); | ||
assertFalse(cameraZoom.hasSupport); | ||
assertEquals(cameraZoom.maxZoom, 1.0f, 0); | ||
} | ||
|
||
@Test | ||
public void ctor_when_max_zoom_is_null() { | ||
final Rect sensorSize = new Rect(0, 0, 0, 0); | ||
final Float maxZoom = null; | ||
final CameraZoom cameraZoom = new CameraZoom(sensorSize, maxZoom); | ||
|
||
assertNotNull(cameraZoom); | ||
assertFalse(cameraZoom.hasSupport); | ||
assertEquals(cameraZoom.maxZoom, 1.0f, 0); | ||
} | ||
|
||
@Test | ||
public void ctor_when_max_zoom_is_smaller_then_default_zoom_factor() { | ||
final Rect sensorSize = new Rect(0, 0, 0, 0); | ||
final Float maxZoom = 0.5f; | ||
final CameraZoom cameraZoom = new CameraZoom(sensorSize, maxZoom); | ||
|
||
assertNotNull(cameraZoom); | ||
assertFalse(cameraZoom.hasSupport); | ||
assertEquals(cameraZoom.maxZoom, 1.0f, 0); | ||
} | ||
|
||
@Test | ||
public void setZoom_when_no_support_should_not_set_scaler_crop_region() { | ||
final CameraZoom cameraZoom = new CameraZoom(null, null); | ||
final Rect computedZoom = cameraZoom.computeZoom(2f); | ||
|
||
assertNull(computedZoom); | ||
} | ||
|
||
@Test | ||
public void setZoom_when_sensor_size_equals_zero_should_return_crop_region_of_zero() { | ||
final Rect sensorSize = new Rect(0, 0, 0, 0); | ||
final CameraZoom cameraZoom = new CameraZoom(sensorSize, 20f); | ||
final Rect computedZoom = cameraZoom.computeZoom(18f); | ||
|
||
assertNotNull(computedZoom); | ||
assertEquals(computedZoom.left, 0); | ||
assertEquals(computedZoom.top, 0); | ||
assertEquals(computedZoom.right, 0); | ||
assertEquals(computedZoom.bottom, 0); | ||
} | ||
|
||
@Test | ||
public void setZoom_when_sensor_size_is_valid_should_return_crop_region() { | ||
final Rect sensorSize = new Rect(0, 0, 100, 100); | ||
final CameraZoom cameraZoom = new CameraZoom(sensorSize, 20f); | ||
final Rect computedZoom = cameraZoom.computeZoom(18f); | ||
|
||
assertNotNull(computedZoom); | ||
assertEquals(computedZoom.left, 48); | ||
assertEquals(computedZoom.top, 48); | ||
assertEquals(computedZoom.right, 52); | ||
assertEquals(computedZoom.bottom, 52); | ||
} | ||
|
||
@Test | ||
public void setZoom_when_zoom_is_greater_then_max_zoom_clamp_to_max_zoom() { | ||
final Rect sensorSize = new Rect(0, 0, 100, 100); | ||
final CameraZoom cameraZoom = new CameraZoom(sensorSize, 10f); | ||
final Rect computedZoom = cameraZoom.computeZoom(25f); | ||
|
||
assertNotNull(computedZoom); | ||
assertEquals(computedZoom.left, 45); | ||
assertEquals(computedZoom.top, 45); | ||
assertEquals(computedZoom.right, 55); | ||
assertEquals(computedZoom.bottom, 55); | ||
} | ||
|
||
@Test | ||
public void setZoom_when_zoom_is_smaller_then_min_zoom_clamp_to_min_zoom() { | ||
final Rect sensorSize = new Rect(0, 0, 100, 100); | ||
final CameraZoom cameraZoom = new CameraZoom(sensorSize, 10f); | ||
final Rect computedZoom = cameraZoom.computeZoom(0.5f); | ||
|
||
assertNotNull(computedZoom); | ||
assertEquals(computedZoom.left, 0); | ||
assertEquals(computedZoom.top, 0); | ||
assertEquals(computedZoom.right, 100); | ||
assertEquals(computedZoom.bottom, 100); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.