Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
c9f7e21
Added platform interface methods for setting auto exposure.
BeMacized Dec 24, 2020
532b22f
Added platform interface methods for setting auto exposure.
BeMacized Dec 24, 2020
1844b24
Remove workspace files
BeMacized Dec 24, 2020
fd8dd40
Added auto exposure implementations for Android and iOS
BeMacized Dec 24, 2020
92966de
Added platform interface methods for managing auto focus.
BeMacized Dec 25, 2020
deea6a0
Formatted code
BeMacized Dec 25, 2020
42a4517
Export focus mode
BeMacized Dec 25, 2020
b595f93
Add Android and iOS implementations (WIP)
BeMacized Dec 25, 2020
bdeed1d
Update platform interface for changes to autofocus methods
BeMacized Dec 25, 2020
e63b537
Merge branch 'feature/camera_focus_auto_platform_interface' into feat…
BeMacized Dec 25, 2020
4e405b0
WIP
BeMacized Dec 25, 2020
798d458
Revert "Update platform interface for changes to autofocus methods"
BeMacized Dec 25, 2020
7a6a21c
Merge branch 'feature/camera_focus_auto_platform_interface' into feat…
BeMacized Dec 25, 2020
68d3e74
Finish android implementation
BeMacized Dec 25, 2020
d830704
Fix iOS implementation
BeMacized Dec 25, 2020
2b98bdd
iOS fix for setting the exposure point
BeMacized Dec 25, 2020
b886e55
Merge branch 'feature/camera_exposure_auto' into feature/camera_focus…
BeMacized Dec 25, 2020
10f3d61
Merge branch 'feature/camera_focus_auto_platform_interface' into feat…
BeMacized Dec 25, 2020
06f436f
Removed unnecessary check
BeMacized Dec 25, 2020
e19c2a5
Merge branch 'feature/camera_exposure_auto' into feature/camera_focus…
BeMacized Dec 25, 2020
73f6798
Merge branch 'feature/camera_focus_auto_platform_interface' into feat…
BeMacized Dec 25, 2020
df1c09b
Updated changelog and pubspec.yaml
BeMacized Dec 25, 2020
ff45da4
Updated changelog and pubspec.yaml
BeMacized Dec 25, 2020
a6e47e8
Merge branch 'feature/camera_focus_auto_platform_interface' into feat…
BeMacized Dec 25, 2020
95b4795
Merge branch 'master' into feature/camera_focus_auto_platform_interface
BeMacized Dec 25, 2020
6d9f9ca
Merge branch 'feature/camera_focus_auto_platform_interface' into feat…
BeMacized Dec 25, 2020
70804a0
Merge branch 'master' into feature/camera_exposure_auto
BeMacized Dec 29, 2020
5f1a730
Merge branch 'feature/camera_exposure_auto' into feature/camera_focus…
BeMacized Dec 29, 2020
58d9411
Merge branch 'feature/camera_focus_auto_platform_interface' into feat…
BeMacized Dec 29, 2020
7b4445d
Merge branch 'master' into feature/camera_exposure_auto
BeMacized Dec 29, 2020
67b37f2
Merge branch 'feature/camera_exposure_auto' into feature/camera_focus…
BeMacized Dec 29, 2020
0842f2c
Merge branch 'feature/camera_focus_auto_platform_interface' into feat…
BeMacized Dec 29, 2020
14c6ab6
Update platform interface dependency
BeMacized Dec 29, 2020
a52f690
Implement PR feedback
BeMacized Dec 29, 2020
1e6ed87
Merge branch 'master' into feature/camera_exposure_auto
BeMacized Dec 29, 2020
9a88bca
Restore test
BeMacized Dec 29, 2020
0b748ef
Merge branch 'feature/camera_exposure_auto' into feature/camera_focus…
BeMacized Dec 30, 2020
171fe0b
Revert test change
BeMacized Dec 30, 2020
6fb50d6
Merge branch 'feature/camera_focus_auto_platform_interface' into feat…
BeMacized Dec 30, 2020
930d901
Update camera pubspec
BeMacized Dec 30, 2020
5ebbca5
Merge branch 'feature/camera_focus_auto_platform_interface' into feat…
BeMacized Dec 30, 2020
60a1137
Update platform interface to prevent breaking changes with current ma…
BeMacized Dec 30, 2020
525d58c
Merge branch 'feature/camera_focus_auto_platform_interface' into feat…
BeMacized Dec 30, 2020
b0149e9
Update test to match platform interface updates
BeMacized Dec 30, 2020
e6367bd
Code format
BeMacized Dec 30, 2020
9473843
Merged with master
mvanbeusekom Jan 6, 2021
4ebae73
Merged with master
mvanbeusekom Jan 6, 2021
23fe0db
Merged with master
mvanbeusekom Jan 7, 2021
971b4e6
Fixed compilation error
mvanbeusekom Jan 7, 2021
6d174fb
Fix formatting
mvanbeusekom Jan 7, 2021
50dcdce
Update version number
mvanbeusekom Jan 7, 2021
5d8c235
Add missing license headers to java source files.
BeMacized Jan 11, 2021
9500a74
Merge branch 'master' into feature/camera_focus_auto
BeMacized Jan 11, 2021
0ba1c90
Update platform interface dependency
BeMacized Jan 11, 2021
bc97fb9
Merge branch 'feature/camera_focus_auto' of github.com:Baseflow/flutt…
BeMacized Jan 11, 2021
a6f4442
Change fps range determination
BeMacized Jan 11, 2021
1a0ced8
Update with master branch
mvanbeusekom Jan 11, 2021
bcacd4b
Fix analysis warnings
mvanbeusekom Jan 11, 2021
c11c46f
Update version number
mvanbeusekom Jan 11, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/camera/camera/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

* Adds auto exposure support for Android and iOS implementations.

## 0.6.3+4

* Revert previous dependency update: Changed dependency on camera_platform_interface to >=1.04 <1.1.0.

## 0.6.3+3

* Updated dependency on camera_platform_interface to ^1.2.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ public class Camera {
private ExposureMode exposureMode;
private FocusMode focusMode;
private PictureCaptureRequest pictureCaptureRequest;
private MeteringRectangle aeMeteringRectangle;
private MeteringRectangle afMeteringRectangle;
private CameraRegions cameraRegions;
private int exposureOffset;

public Camera(
Expand Down Expand Up @@ -171,6 +170,7 @@ public void open() throws CameraAccessException {
public void onOpened(@NonNull CameraDevice device) {
cameraDevice = device;
try {
cameraRegions = new CameraRegions(getRegionBoundaries());
startPreview();
dartMessenger.sendCameraInitializedEvent(
previewSize.getWidth(),
Expand Down Expand Up @@ -704,19 +704,14 @@ public void setExposurePoint(@NonNull final Result result, Double x, Double y)
"setExposurePointFailed", "Device does not have exposure point capabilities", null);
return;
}
// Check if we are doing a reset or not
if (x == null || y == null) {
x = 0.5;
y = 0.5;
}
// Get the current region boundaries.
Size maxBoundaries = getRegionBoundaries();
if (maxBoundaries == null) {
// Check if the current region boundaries are known
if (cameraRegions.getMaxBoundaries() == null) {
result.error("setExposurePointFailed", "Could not determine max region boundaries", null);
return;
}
// Set the metering rectangle
aeMeteringRectangle = getMeteringRectangleForPoint(maxBoundaries, x, y);
if (x == null || y == null) cameraRegions.resetAutoExposureMeteringRectangle();
else cameraRegions.setAutoExposureMeteringRectangleFromPoint(x, y);
// Apply it
initPreviewCaptureBuilder();
this.cameraCaptureSession.setRepeatingRequest(
Expand Down Expand Up @@ -756,19 +751,14 @@ public void setFocusPoint(@NonNull final Result result, Double x, Double y)
result.error("setFocusPointFailed", "Device does not have focus point capabilities", null);
return;
}
// Check if we are doing a reset or not
if (x == null || y == null) {
x = 0.5;
y = 0.5;
}
// Get the current region boundaries.
Size maxBoundaries = getRegionBoundaries();
if (maxBoundaries == null) {
// Check if the current region boundaries are known
if (cameraRegions.getMaxBoundaries() == null) {
result.error("setFocusPointFailed", "Could not determine max region boundaries", null);
return;
}
// Set the metering rectangle
afMeteringRectangle = getMeteringRectangleForPoint(maxBoundaries, x, y);
if (x == null || y == null) cameraRegions.resetAutoFocusMeteringRectangle();
else cameraRegions.setAutoFocusMeteringRectangleFromPoint(x, y);
// Apply the new metering rectangle
initPreviewCaptureBuilder();
switch (focusMode) {
Expand All @@ -792,46 +782,23 @@ public void onCaptureCompleted(
result.success(null);
}

private MeteringRectangle getMeteringRectangleForPoint(Size maxBoundaries, double x, double y) {
// Interpolate the target coordinate
int targetX = (int) Math.round(x * ((double) (maxBoundaries.getWidth() - 1)));
int targetY = (int) Math.round(y * ((double) (maxBoundaries.getHeight() - 1)));
// Determine the dimensions of the metering triangle (1th of the viewport)
int targetWidth = (int) Math.round(((double) maxBoundaries.getWidth()) / 10d);
int targetHeight = (int) Math.round(((double) maxBoundaries.getHeight()) / 10d);
// Adjust target coordinate to represent top-left corner of metering rectangle
targetX -= targetWidth / 2;
targetY -= targetHeight / 2;
// Adjust target coordinate as to not fall out of bounds
if (targetX < 0) targetX = 0;
if (targetY < 0) targetY = 0;
int maxTargetX = maxBoundaries.getWidth() - 1 - targetWidth;
int maxTargetY = maxBoundaries.getHeight() - 1 - targetHeight;
if (targetX > maxTargetX) targetX = maxTargetX;
if (targetY > maxTargetY) targetY = maxTargetY;
// Build the metering rectangle
return new MeteringRectangle(targetX, targetY, targetWidth, targetHeight, 1);
@TargetApi(VERSION_CODES.P)
private boolean supportsDistortionCorrection() throws CameraAccessException {
int[] availableDistortionCorrectionModes =
cameraManager
.getCameraCharacteristics(cameraDevice.getId())
.get(CameraCharacteristics.DISTORTION_CORRECTION_AVAILABLE_MODES);
if (availableDistortionCorrectionModes == null) availableDistortionCorrectionModes = new int[0];
long nonOffModesSupported =
Arrays.stream(availableDistortionCorrectionModes)
.filter((value) -> value != CaptureRequest.DISTORTION_CORRECTION_MODE_OFF)
.count();
return nonOffModesSupported > 0;
}

@SuppressLint("NewApi")
private Size getRegionBoundaries() throws CameraAccessException {
// Check if the device supports distortion correction
boolean supportsDistortionCorrection = false;
if (android.os.Build.VERSION.SDK_INT >= VERSION_CODES.P) {
int[] availableDistortionCorrectionModes =
cameraManager
.getCameraCharacteristics(cameraDevice.getId())
.get(CameraCharacteristics.DISTORTION_CORRECTION_AVAILABLE_MODES);
if (availableDistortionCorrectionModes == null)
availableDistortionCorrectionModes = new int[0];
long nonOffModesSupported =
Arrays.stream(availableDistortionCorrectionModes)
.filter((value) -> value != CaptureRequest.DISTORTION_CORRECTION_MODE_OFF)
.count();
supportsDistortionCorrection = nonOffModesSupported > 0;
}
// No distortion correction support
if (!supportsDistortionCorrection) {
if (android.os.Build.VERSION.SDK_INT < VERSION_CODES.P || !supportsDistortionCorrection()) {
return cameraManager
.getCameraCharacteristics(cameraDevice.getId())
.get(CameraCharacteristics.SENSOR_INFO_PIXEL_ARRAY_SIZE);
Expand Down Expand Up @@ -938,10 +905,10 @@ private void initPreviewCaptureBuilder() {
break;
}
// Applying auto exposure
if (aeMeteringRectangle != null) {
captureRequestBuilder.set(
CaptureRequest.CONTROL_AE_REGIONS, new MeteringRectangle[] {aeMeteringRectangle});
}
MeteringRectangle aeRect = cameraRegions.getAEMeteringRectangle();
captureRequestBuilder.set(
CaptureRequest.CONTROL_AE_REGIONS,
aeRect == null ? new MeteringRectangle[0] : new MeteringRectangle[] {aeRect});
switch (exposureMode) {
case locked:
captureRequestBuilder.set(CaptureRequest.CONTROL_AE_LOCK, true);
Expand All @@ -967,10 +934,10 @@ private void initPreviewCaptureBuilder() {
default:
break;
}
if (afMeteringRectangle != null) {
captureRequestBuilder.set(
CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[] {afMeteringRectangle});
}
MeteringRectangle afRect = cameraRegions.getAFMeteringRectangle();
captureRequestBuilder.set(
CaptureRequest.CONTROL_AF_REGIONS,
afRect == null ? new MeteringRectangle[0] : new MeteringRectangle[] {afRect});
}

public void startPreview() throws CameraAccessException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.flutter.plugins.camera;

import android.hardware.camera2.params.MeteringRectangle;
import android.util.Size;

public final class CameraRegions {
private MeteringRectangle aeMeteringRectangle;
private MeteringRectangle afMeteringRectangle;
private Size maxBoundaries;

public CameraRegions(Size maxBoundaries) {
assert (maxBoundaries == null || maxBoundaries.getWidth() > 0);
assert (maxBoundaries == null || maxBoundaries.getHeight() > 0);
this.maxBoundaries = maxBoundaries;
}

public MeteringRectangle getAEMeteringRectangle() {
return aeMeteringRectangle;
}

public MeteringRectangle getAFMeteringRectangle() {
return afMeteringRectangle;
}

public Size getMaxBoundaries() {
return this.maxBoundaries;
}

public void resetAutoExposureMeteringRectangle() {
this.aeMeteringRectangle = null;
}

public void setAutoExposureMeteringRectangleFromPoint(double x, double y) {
this.aeMeteringRectangle = getMeteringRectangleForPoint(maxBoundaries, x, y);
}

public void resetAutoFocusMeteringRectangle() {
this.afMeteringRectangle = null;
}

public void setAutoFocusMeteringRectangleFromPoint(double x, double y) {
this.afMeteringRectangle = getMeteringRectangleForPoint(maxBoundaries, x, y);
}

public MeteringRectangle getMeteringRectangleForPoint(Size maxBoundaries, double x, double y) {
assert (x >= 0 && x <= 1);
assert (y >= 0 && y <= 1);
if (maxBoundaries == null)
throw new IllegalStateException(
"Functionality for managing metering rectangles is unavailable as this CameraRegions instance was initialized with null boundaries.");

// Interpolate the target coordinate
int targetX = (int) Math.round(x * ((double) (maxBoundaries.getWidth() - 1)));
int targetY = (int) Math.round(y * ((double) (maxBoundaries.getHeight() - 1)));
// Determine the dimensions of the metering triangle (10th of the viewport)
int targetWidth = (int) Math.round(((double) maxBoundaries.getWidth()) / 10d);
int targetHeight = (int) Math.round(((double) maxBoundaries.getHeight()) / 10d);
// Adjust target coordinate to represent top-left corner of metering rectangle
targetX -= targetWidth / 2;
targetY -= targetHeight / 2;
// Adjust target coordinate as to not fall out of bounds
if (targetX < 0) targetX = 0;
if (targetY < 0) targetY = 0;
int maxTargetX = maxBoundaries.getWidth() - 1 - targetWidth;
int maxTargetY = maxBoundaries.getHeight() - 1 - targetHeight;
if (targetX > maxTargetX) targetX = maxTargetX;
if (targetY > maxTargetY) targetY = maxTargetY;

// Build the metering rectangle
return new MeteringRectangle(targetX, targetY, targetWidth, targetHeight, 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package io.flutter.plugins.camera;

import android.hardware.camera2.params.MeteringRectangle;
import android.util.Size;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

@RunWith(RobolectricTestRunner.class)
public class CameraRegionsTest {

CameraRegions cameraRegions;

@Before
public void setUp() {
this.cameraRegions = new CameraRegions(new Size(100, 50));
}

@Test(expected = AssertionError.class)
public void getMeteringRectangleForPoint_should_throw_for_x_upper_bound() {
cameraRegions.getMeteringRectangleForPoint(new Size(10, 10), 1.5, 0);
}

@Test(expected = AssertionError.class)
public void getMeteringRectangleForPoint_should_throw_for_x_lower_bound() {
cameraRegions.getMeteringRectangleForPoint(new Size(10, 10), -0.5, 0);
}

@Test(expected = AssertionError.class)
public void getMeteringRectangleForPoint_should_throw_for_y_upper_bound() {
cameraRegions.getMeteringRectangleForPoint(new Size(10, 10), 0, 1.5);
}

@Test(expected = AssertionError.class)
public void getMeteringRectangleForPoint_should_throw_for_y_lower_bound() {
cameraRegions.getMeteringRectangleForPoint(new Size(10, 10), 0, -0.5);
}

@Test(expected = IllegalStateException.class)
public void getMeteringRectangleForPoint_should_throw_for_null_boundaries() {
cameraRegions.getMeteringRectangleForPoint(null, 0, -0);
}

@Test
public void getMeteringRectangleForPoint_should_return_valid_MeteringRectangle() {
MeteringRectangle r;
// Center
r = cameraRegions.getMeteringRectangleForPoint(cameraRegions.getMaxBoundaries(), 0.5, 0.5);
assertEquals(new MeteringRectangle(45, 23, 10, 5, 1), r);

// Top left
r = cameraRegions.getMeteringRectangleForPoint(cameraRegions.getMaxBoundaries(), 0.0, 0.0);
assertEquals(new MeteringRectangle(0, 0, 10, 5, 1), r);

// Bottom right
r = cameraRegions.getMeteringRectangleForPoint(cameraRegions.getMaxBoundaries(), 1.0, 1.0);
assertEquals(new MeteringRectangle(89, 44, 10, 5, 1), r);

// Top left
r = cameraRegions.getMeteringRectangleForPoint(cameraRegions.getMaxBoundaries(), 0.0, 1.0);
assertEquals(new MeteringRectangle(0, 44, 10, 5, 1), r);

// Top right
r = cameraRegions.getMeteringRectangleForPoint(cameraRegions.getMaxBoundaries(), 1.0, 0.0);
assertEquals(new MeteringRectangle(89, 0, 10, 5, 1), r);
}

@Test(expected = AssertionError.class)
public void constructor_should_throw_for_0_width_boundary() {
new CameraRegions(new Size(0, 50));
}

@Test(expected = AssertionError.class)
public void constructor_should_throw_for_0_height_boundary() {
new CameraRegions(new Size(100, 0));
}

@Test
public void constructor_should_initialize() {
CameraRegions cr = new CameraRegions(new Size(100, 50));
assertEquals(new Size(100, 50), cr.getMaxBoundaries());
assertNull(cr.getAEMeteringRectangle());
assertNull(cr.getAFMeteringRectangle());
}

@Test
public void setAutoExposureMeteringRectangleFromPoint_should_set_aeMeteringRectangle_for_point() {
CameraRegions cr = new CameraRegions(new Size(100, 50));
cr.setAutoExposureMeteringRectangleFromPoint(0, 0);
assertEquals(new MeteringRectangle(0, 0, 10, 5, 1), cr.getAEMeteringRectangle());
}

@Test
public void resetAutoExposureMeteringRectangle_should_reset_aeMeteringRectangle() {
CameraRegions cr = new CameraRegions(new Size(100, 50));
cr.setAutoExposureMeteringRectangleFromPoint(0, 0);
assertNotNull(cr.getAEMeteringRectangle());
cr.resetAutoExposureMeteringRectangle();
assertNull(cr.getAEMeteringRectangle());
}

@Test
public void setAutoFocusMeteringRectangleFromPoint_should_set_afMeteringRectangle_for_point() {
CameraRegions cr = new CameraRegions(new Size(100, 50));
cr.setAutoFocusMeteringRectangleFromPoint(0, 0);
assertEquals(new MeteringRectangle(0, 0, 10, 5, 1), cr.getAFMeteringRectangle());
}

@Test
public void resetAutoFocusMeteringRectangle_should_reset_afMeteringRectangle() {
CameraRegions cr = new CameraRegions(new Size(100, 50));
cr.setAutoFocusMeteringRectangleFromPoint(0, 0);
assertNotNull(cr.getAFMeteringRectangle());
cr.resetAutoFocusMeteringRectangle();
assertNull(cr.getAFMeteringRectangle());
}
}
3 changes: 2 additions & 1 deletion packages/camera/camera/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ dependencies:
#TODO(BeMacized): Replace with reference to pub.dev version once updated platform interface has been published.
camera_platform_interface:
path: ../camera_platform_interface
pedantic: ^1.8.0

dev_dependencies:
path_provider: ^0.5.0
video_player: ^0.10.0
flutter_test:
sdk: flutter
flutter_driver:
sdk: flutter
pedantic: ^1.8.0
mockito: ^4.1.3
plugin_platform_interface: ^1.0.3

Expand Down
3 changes: 2 additions & 1 deletion packages/camera/camera/test/camera_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,8 @@ class MockCameraPlatform extends Mock
: Future.value(mockTakePicture);

@override
Future<XFile> startVideoRecording(int cameraId) =>
Future<XFile> startVideoRecording(int cameraId,
{Duration maxVideoDuration}) =>
Future.value(mockVideoRecordingXFile);
}

Expand Down