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 1 commit
Commits
Show all changes
28 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
bdeed1d
Update platform interface for changes to autofocus methods
BeMacized Dec 25, 2020
798d458
Revert "Update platform interface for changes to autofocus methods"
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
06f436f
Removed unnecessary check
BeMacized Dec 25, 2020
e19c2a5
Merge branch 'feature/camera_exposure_auto' into feature/camera_focus…
BeMacized Dec 25, 2020
df1c09b
Updated changelog and pubspec.yaml
BeMacized Dec 25, 2020
95b4795
Merge branch 'master' into feature/camera_focus_auto_platform_interface
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
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
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
930d901
Update camera pubspec
BeMacized Dec 30, 2020
60a1137
Update platform interface to prevent breaking changes with current ma…
BeMacized Dec 30, 2020
07c4339
Merged with master
mvanbeusekom Jan 6, 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
Prev Previous commit
Next Next commit
Implement PR feedback
  • Loading branch information
BeMacized committed Dec 29, 2020
commit a52f690c42a937c909810d86e6835872bd1b4be3
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public class Camera {
private FlashMode flashMode;
private ExposureMode exposureMode;
private PictureCaptureRequest pictureCaptureRequest;
private MeteringRectangle aeMeteringRectangle;
private CameraRegions cameraRegions;
private int exposureOffset;

public Camera(
Expand Down Expand Up @@ -167,6 +167,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 @@ -702,56 +703,38 @@ public void setExposurePoint(@NonNull final Result result, Double x, Double y)
x = 0.5;
y = 0.5;
}
// Get the current exposure point boundaries.
Size maxBoundaries = getExposureBoundaries();
// Get the current region boundaries.
Size maxBoundaries = getRegionBoundaries();
if (maxBoundaries == null) {
result.error("setExposurePointFailed", "Could not determine max region boundaries", null);
return;
}
// 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;
// Set the metering rectangle
aeMeteringRectangle = new MeteringRectangle(targetX, targetY, targetWidth, targetHeight, 1);
cameraRegions.setAutoExposureMeteringRectangleFromPoint(x, y);
// Apply it
initPreviewCaptureBuilder();
this.cameraCaptureSession.setRepeatingRequest(
captureRequestBuilder.build(), pictureCaptureCallback, null);
result.success(null);
}

@SuppressLint("NewApi")
private Size getExposureBoundaries() 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;
}
@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;
}

private Size getRegionBoundaries() throws CameraAccessException {
// 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 @@ -850,10 +833,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,
new MeteringRectangle[] {cameraRegions.getAEMeteringRectangle()});
switch (exposureMode) {
case locked:
captureRequestBuilder.set(CaptureRequest.CONTROL_AE_LOCK, true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.flutter.plugins.camera;

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

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

public CameraRegions(Size maxBoundaries) {
assert (maxBoundaries != null);
assert (maxBoundaries.getWidth() > 0);
assert (maxBoundaries.getHeight() > 0);
this.maxBoundaries = maxBoundaries;
setAutoExposureMeteringRectangleFromPoint(0.5, 0.5);
}

public MeteringRectangle getAEMeteringRectangle() {
return aeMeteringRectangle;
}

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

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

public MeteringRectangle getMeteringRectangleForPoint(Size maxBoundaries, double x, double y) {
assert (x >= 0 && x <= 1);
assert (y >= 0 && y <= 1);
assert (maxBoundaries != null);

// 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,99 @@
package io.flutter.plugins.camera;

import static org.junit.Assert.assertEquals;

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;

@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 = AssertionError.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_null_boundaries() {
new CameraRegions(null);
}

@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());
assertEquals(new MeteringRectangle(45, 23, 10, 5, 1), cr.getAEMeteringRectangle());
}

@Test
public void setAutoExposureMeteringRectangleFromPoin_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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.StandardMethodCodec;
import io.flutter.plugins.camera.types.ExposureMode;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
Expand Down
2 changes: 1 addition & 1 deletion packages/camera/camera/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ dependencies:
flutter:
sdk: flutter
camera_platform_interface: ^1.2.0
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