From bc07a0cf9a6409ed0b5b446df97d82fe926a300f Mon Sep 17 00:00:00 2001 From: hoonjae Date: Tue, 4 May 2021 21:00:56 +0900 Subject: [PATCH] Limit camera preview size. This is related to the issue, https://github.com/flutter/flutter/issues/46082. This is an important issue to enable real-time image analysis and take a picture in a Flutter app. In my case, I tried to apply head pose estimation and taking a picture in a proper pose automatically, however, when I tried CameraController.startImageStream with ResolutionPreset.max for capture high quality picture, the app crashed unexpectedly. To resolve the problem, I should limit the resolution to ResolutionPreset.high, but in this case, generated pictures also have low resolution (which is 1280x720). In Android, the preview resolution is limited to 1280x720 (resolutionPreset.high) in the function computeBestPreviewSize in CameraUtils.java. Like this, I can resolve the problem by limiting _previewSize in iOS. So, I propose this PR. In the future, I recommend to separately set preview resolution and capture resolution by getting some arguments for flexibilities of the camera package. --- packages/camera/camera/ios/Classes/CameraPlugin.m | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/camera/camera/ios/Classes/CameraPlugin.m b/packages/camera/camera/ios/Classes/CameraPlugin.m index 5c43f78a8c4b..74e2aacd7e08 100644 --- a/packages/camera/camera/ios/Classes/CameraPlugin.m +++ b/packages/camera/camera/ios/Classes/CameraPlugin.m @@ -534,21 +534,19 @@ - (void)setCaptureSessionPreset:(ResolutionPreset)resolutionPreset { if (@available(iOS 9.0, *)) { if ([_captureSession canSetSessionPreset:AVCaptureSessionPreset3840x2160]) { _captureSession.sessionPreset = AVCaptureSessionPreset3840x2160; - _previewSize = CGSizeMake(3840, 2160); + _previewSize = CGSizeMake(1280, 720); // Limit preview resolution to resolutionPreset.high break; } } if ([_captureSession canSetSessionPreset:AVCaptureSessionPresetHigh]) { _captureSession.sessionPreset = AVCaptureSessionPresetHigh; - _previewSize = - CGSizeMake(_captureDevice.activeFormat.highResolutionStillImageDimensions.width, - _captureDevice.activeFormat.highResolutionStillImageDimensions.height); + _previewSize = CGSizeMake(1280, 720); // Limit preview resolution to resolutionPreset.high break; } case veryHigh: if ([_captureSession canSetSessionPreset:AVCaptureSessionPreset1920x1080]) { _captureSession.sessionPreset = AVCaptureSessionPreset1920x1080; - _previewSize = CGSizeMake(1920, 1080); + _previewSize = CGSizeMake(1280, 720); // Limit preview resolution to resolutionPreset.high break; } case high: