diff --git a/camera/example/lib/main.dart b/camera/example/lib/main.dart index 2fa2ae6..c5721d7 100644 --- a/camera/example/lib/main.dart +++ b/camera/example/lib/main.dart @@ -4,6 +4,7 @@ import 'dart:async'; import 'dart:io'; +import 'dart:ui' as ui; import 'package:camera/camera.dart'; import 'package:flutter/foundation.dart'; @@ -646,7 +647,7 @@ class _CameraExampleHomeState extends State final CameraController cameraController = CameraController( cameraDescription, - kIsWeb ? ResolutionPreset.max : ResolutionPreset.medium, + ResolutionPreset.max, enableAudio: enableAudio, imageFormatGroup: ImageFormatGroup.jpeg, ); @@ -729,11 +730,27 @@ class _CameraExampleHomeState extends State }); if (file != null) { showInSnackBar('Picture saved to ${file.path}'); + + _loadImage(file.path).then((image) { + debugPrint( + 'Picture resolution is ${image.height} x ${image.width}', + ); + }); } } }); } + Future _loadImage(String imagePath) async { + final Uint8List imageBytes = await File(imagePath).readAsBytes(); + final Completer imageCompleter = Completer(); + ui.decodeImageFromList(imageBytes, (ui.Image image) { + imageCompleter.complete(image); + }); + + return imageCompleter.future; + } + void onFlashModeButtonPressed() { if (_flashModeControlRowAnimationController.value == 1) { _flashModeControlRowAnimationController.reverse(); diff --git a/camera_avfoundation/ios/Classes/FLTCam.m b/camera_avfoundation/ios/Classes/FLTCam.m index 329fe4b..82eb84a 100644 --- a/camera_avfoundation/ios/Classes/FLTCam.m +++ b/camera_avfoundation/ios/Classes/FLTCam.m @@ -162,6 +162,9 @@ - (instancetype)initWithCameraName:(NSString *)cameraName _motionManager = [[CMMotionManager alloc] init]; [_motionManager startAccelerometerUpdates]; + _videoCaptureSession.sessionPreset = AVCaptureSessionPresetInputPriority; + + [self setCaptureSessionPreset:_resolutionPreset]; [self updateOrientation]; @@ -340,10 +343,19 @@ - (NSString *)getTemporaryFilePathWithExtension:(NSString *)extension - (void)setCaptureSessionPreset:(FLTResolutionPreset)resolutionPreset { switch (resolutionPreset) { case FLTResolutionPresetMax: - if ([_videoCaptureSession canSetSessionPreset:AVCaptureSessionPresetPhoto]) { - _videoCaptureSession.sessionPreset = AVCaptureSessionPresetPhoto; - _previewSize = CGSizeMake(4032, 3024); - break; + { + AVCaptureDeviceFormat *bestFormat = [self getHighestResolutionFormatFor:_captureDevice]; + if ( bestFormat ) { + _videoCaptureSession.sessionPreset = AVCaptureSessionPresetInputPriority; + if ( [_captureDevice lockForConfiguration:NULL] == YES ) { + _captureDevice.activeFormat = bestFormat; + [_captureDevice unlockForConfiguration]; + _previewSize = + CGSizeMake(_captureDevice.activeFormat.highResolutionStillImageDimensions.width, + _captureDevice.activeFormat.highResolutionStillImageDimensions.height); + break; + } + } } if ([_videoCaptureSession canSetSessionPreset:AVCaptureSessionPreset3840x2160]) { _videoCaptureSession.sessionPreset = AVCaptureSessionPreset3840x2160; @@ -405,6 +417,22 @@ - (void)setCaptureSessionPreset:(FLTResolutionPreset)resolutionPreset { _audioCaptureSession.sessionPreset = _videoCaptureSession.sessionPreset; } +- (AVCaptureDeviceFormat *)getHighestResolutionFormatFor:(AVCaptureDevice*)captureDevice { + AVCaptureDeviceFormat *bestFormat = nil; + NSUInteger maxPixelCount = 0; + for ( AVCaptureDeviceFormat *format in [_captureDevice formats] ) { + CMVideoDimensions res = CMVideoFormatDescriptionGetDimensions(format.formatDescription); + NSUInteger height = res.height; + NSUInteger width = res.width; + NSUInteger pixelCount = height * width; + if ( pixelCount > maxPixelCount ) { + maxPixelCount = pixelCount; + bestFormat = format; + } + } + return bestFormat; +} + - (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {