123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- //
- // CameraManager.m
- // PanoramicCameraSDK
- //
- // Created by 王昭威 on 2023/1/14.
- //
- #import "CameraManager.h"
- @interface CameraManager ()
- @property (nonatomic, assign) CGFloat backAspect;
- @property (nonatomic, assign) CGFloat frontAspect;
- @end
- @implementation CameraManager
- @synthesize front = _front;
- @synthesize back = _back;
- - (void)setResolutionFor:(AVCaptureDevicePosition)position with: (NSInteger)desiredWidth{
-
- AVCaptureDevice* device = [self cameraWithPosition:position];
- if(device == nil){
- return;
- }
-
- NSArray *supportedFormats = device.formats;
-
- AVCaptureDeviceFormat *bestFormat;
- CGFloat aspect = 1;
-
-
- for (AVCaptureDeviceFormat *format in supportedFormats) {
- CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions((CMVideoFormatDescriptionRef)[format formatDescription]);
- //分辨率
- if (dimensions.width <= desiredWidth) {
- if(dimensions.height > 0)
- aspect = (CGFloat)dimensions.width / dimensions.height;
- bestFormat = format;
- }
- }
-
- [device lockForConfiguration:nil];
- [device setActiveFormat:bestFormat];
-
- [device setActiveVideoMaxFrameDuration:CMTimeMake(1, 10)];
- [device setActiveVideoMinFrameDuration:CMTimeMake(1, 10)];
-
- [device unlockForConfiguration];
-
- if(device.position == AVCaptureDevicePositionFront){
- self.frontAspect = aspect;
- }
- else if(device.position == AVCaptureDevicePositionBack){
- self.backAspect = aspect;
- }
- }
- - (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position
- {
- NSArray *devices = nil;
- if(@available(iOS 10, *)){
- AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera]
- mediaType:AVMediaTypeVideo
- position:position];
- devices = [captureDeviceDiscoverySession devices];
- }
- else if(@available(iOS 4, *)){
- devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
- }
- if(devices == nil){
- return nil;
- }
- for (AVCaptureDevice *device in devices)
- {
- if ([device position] == position)
- return device;
- }
- return nil;
- }
- + (instancetype)shared{
- static dispatch_once_t token;
- static CameraManager* instance = nil;
- dispatch_once(&token, ^{
- instance = [[self alloc] init];
- });
-
- return instance;
- }
- - (instancetype)init{
- self = [super init];
- if(self){
- _back = [self cameraWithPosition:AVCaptureDevicePositionBack];
- _front = [self cameraWithPosition:AVCaptureDevicePositionFront];
-
- _frontAspect = 1;
- _backAspect = 1;
- }
-
- return self;
- }
- - (void)focusOn:(CGPoint)point{
- if (self.back == nil) {
- return;
- }
- [self focusOn:point device:self.back];
- }
- - (void)focusOn:(CGPoint)point device:(AVCaptureDevice *)device{
- NSError* error;
- [device lockForConfiguration:&error];
- if(error != nil){
- return;
- }
-
- if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]){
- [device setFocusPointOfInterest: point];
- device.focusMode = AVCaptureFocusModeContinuousAutoFocus;
- }
- if ([device isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure]) {
- [device setExposurePointOfInterest:point];
- device.exposureMode = AVCaptureExposureModeContinuousAutoExposure;
- }
- [device unlockForConfiguration];
- }
- @end
|