Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: ios camera best resolution #3

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 18 additions & 1 deletion camera/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -646,7 +647,7 @@ class _CameraExampleHomeState extends State<CameraExampleHome>

final CameraController cameraController = CameraController(
cameraDescription,
kIsWeb ? ResolutionPreset.max : ResolutionPreset.medium,
ResolutionPreset.max,
enableAudio: enableAudio,
imageFormatGroup: ImageFormatGroup.jpeg,
);
Expand Down Expand Up @@ -729,11 +730,27 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
});
if (file != null) {
showInSnackBar('Picture saved to ${file.path}');

_loadImage(file.path).then((image) {
debugPrint(
'Picture resolution is ${image.height} x ${image.width}',
);
});
}
}
});
}

Future<ui.Image> _loadImage(String imagePath) async {
final Uint8List imageBytes = await File(imagePath).readAsBytes();
final Completer<ui.Image> imageCompleter = Completer();
ui.decodeImageFromList(imageBytes, (ui.Image image) {
imageCompleter.complete(image);
});

return imageCompleter.future;
}

void onFlashModeButtonPressed() {
if (_flashModeControlRowAnimationController.value == 1) {
_flashModeControlRowAnimationController.reverse();
Expand Down
36 changes: 32 additions & 4 deletions camera_avfoundation/ios/Classes/FLTCam.m
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ - (instancetype)initWithCameraName:(NSString *)cameraName
_motionManager = [[CMMotionManager alloc] init];
[_motionManager startAccelerometerUpdates];

_videoCaptureSession.sessionPreset = AVCaptureSessionPresetInputPriority;


[self setCaptureSessionPreset:_resolutionPreset];
[self updateOrientation];

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down