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

Feature/vc/android camera recalibration #2810

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ SurfaceTextureEntry getFlutterTexture() {
return flutterTexture;
}

public void takePicture(String filePath, @NonNull final Result result) {
public void takePicture(String filePath, boolean shouldAutoRotate, @NonNull final Result result) {
final File file = new File(filePath);

if (file.exists()) {
Expand All @@ -248,7 +248,10 @@ public void takePicture(String filePath, @NonNull final Result result) {
final CaptureRequest.Builder captureBuilder =
cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureBuilder.addTarget(pictureImageReader.getSurface());
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getMediaOrientation());
int mediaOrientation = shouldAutoRotate
? getMediaOrientation()
: (isFrontFacing ? -90 : 90);
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, mediaOrientation);

cameraCaptureSession.capture(
captureBuilder.build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull final Result result)
}
case "takePicture":
{
camera.takePicture(call.argument("path"), result);
camera.takePicture(call.argument("path"), call.argument("shouldAutoRotate"), result);
break;
}
case "prepareForVideoRecording":
Expand Down
11 changes: 10 additions & 1 deletion packages/camera/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
VideoPlayerController videoController;
VoidCallback videoPlayerListener;
bool enableAudio = true;
bool enableAutoRotate = true;

@override
void initState() {
Expand Down Expand Up @@ -153,6 +154,14 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
}
},
),
const Text('Enable Auto Rotate:'),
Switch(
value: enableAutoRotate,
onChanged: (bool value) {
enableAutoRotate = value;
onNewCameraSelected(controller.description);
},
),
],
),
);
Expand Down Expand Up @@ -451,7 +460,7 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
}

try {
await controller.takePicture(filePath);
await controller.takePicture(filePath, shouldAutoRotate: enableAutoRotate);
} on CameraException catch (e) {
_showCameraException(e);
return null;
Expand Down
31 changes: 25 additions & 6 deletions packages/camera/ios/Classes/CameraPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ @interface FLTSavePhotoDelegate : NSObject <AVCapturePhotoCaptureDelegate>
@property(readonly, nonatomic) FlutterResult result;
@property(readonly, nonatomic) CMMotionManager *motionManager;
@property(readonly, nonatomic) AVCaptureDevicePosition cameraPosition;
@property(assign, nonatomic) BOOL shouldAutoRotate;

- initWithPath:(NSString *)filename
result:(FlutterResult)result
motionManager:(CMMotionManager *)motionManager
cameraPosition:(AVCaptureDevicePosition)cameraPosition;
cameraPosition:(AVCaptureDevicePosition)cameraPosition
shouldAutoRotate:(BOOL)shouldAutoRotate;
@end

@interface FLTImageStreamHandler : NSObject <FlutterStreamHandler>
Expand Down Expand Up @@ -52,13 +54,15 @@ @implementation FLTSavePhotoDelegate {
- initWithPath:(NSString *)path
result:(FlutterResult)result
motionManager:(CMMotionManager *)motionManager
cameraPosition:(AVCaptureDevicePosition)cameraPosition {
cameraPosition:(AVCaptureDevicePosition)cameraPosition
shouldAutoRotate:(BOOL)shouldAutoRotate {
self = [super init];
NSAssert(self, @"super init cannot be nil");
_path = path;
_result = result;
_motionManager = motionManager;
_cameraPosition = cameraPosition;
_shouldAutoRotate = shouldAutoRotate;
selfReference = self;
return self;
}
Expand Down Expand Up @@ -90,6 +94,9 @@ - (void)captureOutput:(AVCapturePhotoOutput *)output
}

- (UIImageOrientation)getImageRotation {
if (!self.shouldAutoRotate) {
return UIImageOrientationRight;
}
float const threshold = 45.0;
BOOL (^isNearValue)(float value1, float value2) = ^BOOL(float value1, float value2) {
return fabsf(value1 - value2) < threshold;
Expand Down Expand Up @@ -204,7 +211,13 @@ - (void)startVideoRecordingAtPath:(NSString *)path result:(FlutterResult)result;
- (void)stopVideoRecordingWithResult:(FlutterResult)result;
- (void)startImageStreamWithMessenger:(NSObject<FlutterBinaryMessenger> *)messenger;
- (void)stopImageStream;
- (void)captureToFile:(NSString *)filename result:(FlutterResult)result;
/// Captures a photo and save to the specified file path.
/// @param filename The path where the photo should be saved to.
/// @param shouldAutoRotate Whether to automatically rotate the captured photo.
/// If enabled, Saves the EXIF data according to device accelerometer.
- (void)captureToFile:(NSString *)path
shouldAutoRotate:(BOOL)shouldAutoRotate
result:(FlutterResult)result;
@end

@implementation FLTCam {
Expand Down Expand Up @@ -272,7 +285,9 @@ - (void)stop {
[_captureSession stopRunning];
}

- (void)captureToFile:(NSString *)path result:(FlutterResult)result {
- (void)captureToFile:(NSString *)path
shouldAutoRotate:(BOOL)shouldAutoRotate
result:(FlutterResult)result {
AVCapturePhotoSettings *settings = [AVCapturePhotoSettings photoSettings];
if (_resolutionPreset == max) {
[settings setHighResolutionPhotoEnabled:YES];
Expand All @@ -282,7 +297,8 @@ - (void)captureToFile:(NSString *)path result:(FlutterResult)result {
delegate:[[FLTSavePhotoDelegate alloc] initWithPath:path
result:result
motionManager:_motionManager
cameraPosition:_captureDevice.position]];
cameraPosition:_captureDevice.position
shouldAutoRotate:shouldAutoRotate]];
}

- (void)setCaptureSessionPreset:(ResolutionPreset)resolutionPreset {
Expand Down Expand Up @@ -882,7 +898,10 @@ - (void)handleMethodCallAsync:(FlutterMethodCall *)call result:(FlutterResult)re
NSUInteger textureId = ((NSNumber *)argsMap[@"textureId"]).unsignedIntegerValue;

if ([@"takePicture" isEqualToString:call.method]) {
[_camera captureToFile:call.arguments[@"path"] result:result];
BOOL shouldAutoRotate = [call.arguments[@"shouldAutoRotate"] boolValue];
[_camera captureToFile:call.arguments[@"path"]
shouldAutoRotate:shouldAutoRotate
result:result];
} else if ([@"dispose" isEqualToString:call.method]) {
[_registry unregisterTexture:textureId];
[_camera close];
Expand Down
8 changes: 6 additions & 2 deletions packages/camera/lib/camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ class CameraController extends ValueNotifier<CameraValue> {
/// The file can be read as this function returns.
///
/// Throws a [CameraException] if the capture fails.
Future<void> takePicture(String path) async {
Future<void> takePicture(String path, {bool shouldAutoRotate = false}) async {
if (!value.isInitialized || _isDisposed) {
throw CameraException(
'Uninitialized CameraController.',
Expand All @@ -356,7 +356,11 @@ class CameraController extends ValueNotifier<CameraValue> {
value = value.copyWith(isTakingPicture: true);
await _channel.invokeMethod<void>(
'takePicture',
<String, dynamic>{'textureId': _textureId, 'path': path},
<String, dynamic>{
'textureId': _textureId,
'path': path,
'shouldAutoRotate': shouldAutoRotate,
},
);
value = value.copyWith(isTakingPicture: false);
} on PlatformException catch (e) {
Expand Down