Skip to content

Commit 1acf379

Browse files
authored
Merge pull request flutter#6 from bottlepay/fix/refactoring
Draft: Refactor camera features into CameraFeature classes
2 parents b55f2e5 + 44a781e commit 1acf379

35 files changed

+1608
-789
lines changed

packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/Camera.java

Lines changed: 314 additions & 518 deletions
Large diffs are not rendered by default.

packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/CameraCaptureCallback.java

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55
import android.hardware.camera2.CaptureRequest;
66
import android.hardware.camera2.CaptureResult;
77
import android.hardware.camera2.TotalCaptureResult;
8+
import android.util.Log;
89
import androidx.annotation.NonNull;
910

1011
class CameraCaptureCallback extends CaptureCallback {
1112
interface CameraCaptureStateListener {
1213
void onConverged();
14+
1315
void onPrecapture();
16+
1417
void onPrecaptureTimeout();
1518
}
1619

1720
private final CameraCaptureStateListener cameraStateListener;
18-
1921
private CameraState cameraState;
2022
private PictureCaptureRequest pictureCaptureRequest;
2123

@@ -24,11 +26,9 @@ public static CameraCaptureCallback create(
2426
return new CameraCaptureCallback(cameraStateListener);
2527
}
2628

27-
private CameraCaptureCallback(
28-
@NonNull CameraCaptureStateListener cameraStateListener) {
29+
private CameraCaptureCallback(@NonNull CameraCaptureStateListener cameraStateListener) {
2930
cameraState = CameraState.STATE_PREVIEW;
3031
this.cameraStateListener = cameraStateListener;
31-
this.pictureCaptureRequest = pictureCaptureRequest;
3232
}
3333

3434
public CameraState getCameraState() {
@@ -37,11 +37,6 @@ public CameraState getCameraState() {
3737

3838
public void setCameraState(@NonNull CameraState state) {
3939
cameraState = state;
40-
41-
if (pictureCaptureRequest != null && state == CameraState.STATE_WAITING_PRECAPTURE_DONE) {
42-
pictureCaptureRequest.setState(
43-
PictureCaptureRequestState.STATE_WAITING_PRECAPTURE_DONE);
44-
}
4540
}
4641

4742
public void setPictureCaptureRequest(@NonNull PictureCaptureRequest pictureCaptureRequest) {
@@ -52,53 +47,64 @@ private void process(CaptureResult result) {
5247
Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
5348
Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
5449

50+
if (cameraState != CameraState.STATE_PREVIEW) {
51+
Log.i(
52+
"Camera",
53+
"CameraCaptureCallback | state: "
54+
+ cameraState
55+
+ " | afState: "
56+
+ afState
57+
+ " | aeState: "
58+
+ aeState);
59+
}
60+
5561
switch (cameraState) {
5662
case STATE_PREVIEW:
57-
{
58-
// We have nothing to do when the camera preview is working normally.
59-
break;
60-
}
63+
{
64+
// We have nothing to do when the camera preview is working normally.
65+
break;
66+
}
6167
case STATE_WAITING_FOCUS:
62-
{
63-
if (afState == null) {
64-
return;
65-
} else if (afState == CaptureRequest.CONTROL_AF_STATE_PASSIVE_SCAN
66-
|| afState == CaptureRequest.CONTROL_AF_STATE_FOCUSED_LOCKED
67-
|| afState == CaptureRequest.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED) {
68-
// CONTROL_AE_STATE can be null on some devices
69-
70-
if (aeState == null || aeState == CaptureRequest.CONTROL_AE_STATE_CONVERGED) {
71-
cameraStateListener.onConverged();
72-
} else {
73-
cameraStateListener.onPrecapture();
68+
{
69+
if (afState == null) {
70+
return;
71+
} else if (afState == CaptureRequest.CONTROL_AF_STATE_FOCUSED_LOCKED
72+
|| afState == CaptureRequest.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED) {
73+
// CONTROL_AE_STATE can be null on some devices
74+
75+
if (aeState == null || aeState == CaptureRequest.CONTROL_AE_STATE_CONVERGED) {
76+
cameraStateListener.onConverged();
77+
} else {
78+
cameraStateListener.onPrecapture();
79+
}
7480
}
81+
break;
7582
}
76-
break;
77-
}
7883

7984
case STATE_WAITING_PRECAPTURE_START:
80-
{
81-
// CONTROL_AE_STATE can be null on some devices
82-
if (aeState == null
83-
|| aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED
84-
|| aeState == CaptureResult.CONTROL_AE_STATE_PRECAPTURE
85-
|| aeState == CaptureResult.CONTROL_AE_STATE_FLASH_REQUIRED) {
86-
setCameraState(CameraState.STATE_WAITING_PRECAPTURE_DONE);
85+
{
86+
// CONTROL_AE_STATE can be null on some devices
87+
if (aeState == null
88+
|| aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED
89+
|| aeState == CaptureResult.CONTROL_AE_STATE_PRECAPTURE
90+
|| aeState == CaptureResult.CONTROL_AE_STATE_FLASH_REQUIRED) {
91+
setCameraState(CameraState.STATE_WAITING_PRECAPTURE_DONE);
92+
}
93+
break;
8794
}
88-
break;
89-
}
9095

9196
case STATE_WAITING_PRECAPTURE_DONE:
92-
{
93-
// CONTROL_AE_STATE can be null on some devices
94-
if (aeState == null || aeState != CaptureResult.CONTROL_AE_STATE_PRECAPTURE) {
95-
cameraStateListener.onConverged();
96-
} else if (pictureCaptureRequest != null && pictureCaptureRequest.hitPreCaptureTimeout()) {
97-
// Log.i(TAG, "===> Hit precapture timeout");
98-
cameraStateListener.onPrecaptureTimeout();
97+
{
98+
// CONTROL_AE_STATE can be null on some devices
99+
if (aeState == null || aeState != CaptureResult.CONTROL_AE_STATE_PRECAPTURE) {
100+
cameraStateListener.onConverged();
101+
} else if (pictureCaptureRequest != null
102+
&& pictureCaptureRequest.hitPreCaptureTimeout()) {
103+
// Log.i(TAG, "===> Hit precapture timeout");
104+
cameraStateListener.onPrecaptureTimeout();
105+
}
106+
break;
99107
}
100-
break;
101-
}
102108
}
103109
}
104110

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public interface CameraProperties {
4242
Rect getSensorInfoPreCorrectionActiveArraySize();
4343

4444
int getSensorOrientation();
45+
46+
int getHardwareLevel();
47+
48+
int[] getAvailableNoiseReductionModes();
4549
}
4650

4751
class CameraPropertiesImpl implements CameraProperties {
@@ -136,4 +140,20 @@ public Rect getSensorInfoPreCorrectionActiveArraySize() {
136140
public int getSensorOrientation() {
137141
return cameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
138142
}
143+
144+
/**
145+
* Returns the hardware level of the camera.
146+
*
147+
* @return
148+
*/
149+
@Override
150+
public int getHardwareLevel() {
151+
return cameraCharacteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);
152+
}
153+
154+
@Override
155+
public int[] getAvailableNoiseReductionModes() {
156+
return cameraCharacteristics.get(
157+
CameraCharacteristics.NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES);
158+
}
139159
}

packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/CameraUtils.java

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
import android.hardware.camera2.CameraManager;
1313
import android.hardware.camera2.CameraMetadata;
1414
import android.hardware.camera2.params.StreamConfigurationMap;
15-
import android.media.CamcorderProfile;
1615
import android.util.Size;
1716
import io.flutter.embedding.engine.systemchannels.PlatformChannel;
18-
import io.flutter.plugins.camera.types.ResolutionPreset;
1917
import java.util.ArrayList;
2018
import java.util.Arrays;
2119
import java.util.Collections;
@@ -86,16 +84,6 @@ static PlatformChannel.DeviceOrientation deserializeDeviceOrientation(String ori
8684
}
8785
}
8886

89-
static Size computeBestPreviewSize(String cameraName, ResolutionPreset preset) {
90-
if (preset.ordinal() > ResolutionPreset.high.ordinal()) {
91-
preset = ResolutionPreset.high;
92-
}
93-
94-
CamcorderProfile profile =
95-
getBestAvailableCamcorderProfileForResolutionPreset(cameraName, preset);
96-
return new Size(profile.videoFrameWidth, profile.videoFrameHeight);
97-
}
98-
9987
static Size computeBestCaptureSize(StreamConfigurationMap streamConfigurationMap) {
10088
// For still image captures, we use the largest available size.
10189
return Collections.max(
@@ -132,45 +120,6 @@ public static List<Map<String, Object>> getAvailableCameras(Activity activity)
132120
return cameras;
133121
}
134122

135-
static CamcorderProfile getBestAvailableCamcorderProfileForResolutionPreset(
136-
String cameraName, ResolutionPreset preset) {
137-
int cameraId = Integer.parseInt(cameraName);
138-
switch (preset) {
139-
// All of these cases deliberately fall through to get the best available profile.
140-
case max:
141-
if (CamcorderProfile.hasProfile(cameraId, CamcorderProfile.QUALITY_HIGH)) {
142-
return CamcorderProfile.get(cameraId, CamcorderProfile.QUALITY_HIGH);
143-
}
144-
case ultraHigh:
145-
if (CamcorderProfile.hasProfile(cameraId, CamcorderProfile.QUALITY_2160P)) {
146-
return CamcorderProfile.get(cameraId, CamcorderProfile.QUALITY_2160P);
147-
}
148-
case veryHigh:
149-
if (CamcorderProfile.hasProfile(cameraId, CamcorderProfile.QUALITY_1080P)) {
150-
return CamcorderProfile.get(cameraId, CamcorderProfile.QUALITY_1080P);
151-
}
152-
case high:
153-
if (CamcorderProfile.hasProfile(cameraId, CamcorderProfile.QUALITY_720P)) {
154-
return CamcorderProfile.get(cameraId, CamcorderProfile.QUALITY_720P);
155-
}
156-
case medium:
157-
if (CamcorderProfile.hasProfile(cameraId, CamcorderProfile.QUALITY_480P)) {
158-
return CamcorderProfile.get(cameraId, CamcorderProfile.QUALITY_480P);
159-
}
160-
case low:
161-
if (CamcorderProfile.hasProfile(cameraId, CamcorderProfile.QUALITY_QVGA)) {
162-
return CamcorderProfile.get(cameraId, CamcorderProfile.QUALITY_QVGA);
163-
}
164-
default:
165-
if (CamcorderProfile.hasProfile(cameraId, CamcorderProfile.QUALITY_LOW)) {
166-
return CamcorderProfile.get(cameraId, CamcorderProfile.QUALITY_LOW);
167-
} else {
168-
throw new IllegalArgumentException(
169-
"No capture session available for current capture session.");
170-
}
171-
}
172-
}
173-
174123
private static class CompareSizesByArea implements Comparator<Size> {
175124
@Override
176125
public int compare(Size lhs, Size rhs) {

packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/DartMessenger.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
import io.flutter.embedding.engine.systemchannels.PlatformChannel;
1212
import io.flutter.plugin.common.BinaryMessenger;
1313
import io.flutter.plugin.common.MethodChannel;
14-
import io.flutter.plugins.camera.types.ExposureMode;
15-
import io.flutter.plugins.camera.types.FocusMode;
14+
import io.flutter.plugins.camera.features.autofocus.FocusMode;
15+
import io.flutter.plugins.camera.features.exposurelock.ExposureMode;
1616
import java.util.HashMap;
1717
import java.util.Map;
1818

19-
class DartMessenger {
19+
public class DartMessenger {
2020
@Nullable private MethodChannel cameraChannel;
2121
@Nullable private MethodChannel deviceChannel;
2222

@@ -46,7 +46,7 @@ public DartMessenger(BinaryMessenger messenger, long cameraId) {
4646
deviceChannel = new MethodChannel(messenger, "flutter.io/cameraPlugin/device");
4747
}
4848

49-
void sendDeviceOrientationChangeEvent(PlatformChannel.DeviceOrientation orientation) {
49+
public void sendDeviceOrientationChangeEvent(PlatformChannel.DeviceOrientation orientation) {
5050
assert (orientation != null);
5151
this.send(
5252
DeviceEventType.ORIENTATION_CHANGED,

packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/MethodCallHandlerImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
import io.flutter.plugin.common.MethodChannel;
1616
import io.flutter.plugin.common.MethodChannel.Result;
1717
import io.flutter.plugins.camera.CameraPermissions.PermissionsRegistry;
18-
import io.flutter.plugins.camera.types.ExposureMode;
19-
import io.flutter.plugins.camera.types.FlashMode;
20-
import io.flutter.plugins.camera.types.FocusMode;
21-
import io.flutter.plugins.camera.types.ResolutionPreset;
18+
import io.flutter.plugins.camera.features.autofocus.FocusMode;
19+
import io.flutter.plugins.camera.features.exposurelock.ExposureMode;
20+
import io.flutter.plugins.camera.features.flash.FlashMode;
21+
import io.flutter.plugins.camera.features.resolution.ResolutionPreset;
2222
import io.flutter.view.TextureRegistry;
2323
import java.util.HashMap;
2424
import java.util.Map;

0 commit comments

Comments
 (0)