CameraManager.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // CameraManager.m
  3. // PanoramicCameraSDK
  4. //
  5. // Created by 王昭威 on 2023/1/14.
  6. //
  7. #import "CameraManager.h"
  8. @interface CameraManager ()
  9. @property (nonatomic, assign) CGFloat backAspect;
  10. @property (nonatomic, assign) CGFloat frontAspect;
  11. @end
  12. @implementation CameraManager
  13. @synthesize front = _front;
  14. @synthesize back = _back;
  15. - (void)setResolutionFor:(AVCaptureDevicePosition)position with: (NSInteger)desiredHeight model:(SDKCaptureModeIndex)model{
  16. AVCaptureDevice* device = [self cameraWithPosition:position];
  17. if(device == nil){
  18. return;
  19. }
  20. NSArray *supportedFormats = device.formats;
  21. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  22. CGFloat height = [UIScreen mainScreen].bounds.size.height;
  23. AVCaptureDeviceFormat *bestFormat;
  24. CGFloat aspect = 1;
  25. //分辨率
  26. CGFloat radio = 16.0/9.0;
  27. if(model == SDK_CAPTURE_MODE_SINGLE || model == SDK_CAPTURE_MODE_CONTINUOUS) {
  28. radio = height/width;
  29. }
  30. [device lockForConfiguration:nil];
  31. for (AVCaptureDeviceFormat *format in supportedFormats) {
  32. // 目标格式与当前预览宽高比一致
  33. CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions((CMVideoFormatDescriptionRef)[format formatDescription]);
  34. // NSLog(@"~~~~~~: H: %d, W:%D, r:%f", dimensions.width, dimensions.height, (CGFloat)dimensions.width/(CGFloat)dimensions.height);
  35. if (((CGFloat)dimensions.width/(CGFloat)dimensions.height) == radio && dimensions.height == desiredHeight) {
  36. bestFormat = format;
  37. break;
  38. }
  39. }
  40. if (!bestFormat) {
  41. //未找到 匹配格式 查找固定匹配格式 (720 1080 2160)
  42. for (AVCaptureDeviceFormat *format in supportedFormats) {
  43. CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions((CMVideoFormatDescriptionRef)[format formatDescription]);
  44. // CMVideoDimensions bestVideoDimensions = CMVideoFormatDescriptionGetDimensions((CMVideoFormatDescriptionRef)[bestFormat formatDescription]);
  45. if (((CGFloat)dimensions.width/(CGFloat)dimensions.height) == radio &&
  46. (dimensions.height == 720 || dimensions.height == 1080 || dimensions.height == 2160)) {
  47. // if (bestVideoDimensions.height <= dimensions.height) {
  48. // bestFormat = format;
  49. // }
  50. if (dimensions.height == 1080) {
  51. bestFormat = format;
  52. }
  53. }
  54. }
  55. }
  56. if (!bestFormat) {
  57. //未找到 固定格式 (720 1080 2160) 查找最佳格式(目标格式最近的分辨率)
  58. for (AVCaptureDeviceFormat *format in supportedFormats) {
  59. CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions((CMVideoFormatDescriptionRef)[format formatDescription]);
  60. CMVideoDimensions bestVideoDimensions = CMVideoFormatDescriptionGetDimensions((CMVideoFormatDescriptionRef)[bestFormat formatDescription]);
  61. if (((CGFloat)dimensions.width/(CGFloat)dimensions.height) == radio && dimensions.height <= desiredHeight ) {
  62. if (bestVideoDimensions.height <= dimensions.height) {
  63. bestFormat = format;
  64. }
  65. }
  66. }
  67. }
  68. if(bestFormat) {
  69. [device setActiveFormat:bestFormat];
  70. }
  71. [device setActiveVideoMaxFrameDuration:CMTimeMake(1, 20)];
  72. [device setActiveVideoMinFrameDuration:CMTimeMake(1, 20)];
  73. [device unlockForConfiguration];
  74. if(device.position == AVCaptureDevicePositionFront){
  75. self.frontAspect = aspect;
  76. }
  77. else if(device.position == AVCaptureDevicePositionBack){
  78. self.backAspect = aspect;
  79. }
  80. }
  81. - (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position
  82. {
  83. NSArray *devices = nil;
  84. if(@available(iOS 10, *)){
  85. AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera]
  86. mediaType:AVMediaTypeVideo
  87. position:position];
  88. devices = [captureDeviceDiscoverySession devices];
  89. }
  90. else if(@available(iOS 4, *)){
  91. devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
  92. }
  93. if(devices == nil){
  94. return nil;
  95. }
  96. for (AVCaptureDevice *device in devices)
  97. {
  98. if ([device position] == position)
  99. return device;
  100. }
  101. return nil;
  102. }
  103. + (instancetype)shared{
  104. static dispatch_once_t token;
  105. static CameraManager* instance = nil;
  106. dispatch_once(&token, ^{
  107. instance = [[self alloc] init];
  108. });
  109. return instance;
  110. }
  111. - (instancetype)init{
  112. self = [super init];
  113. if(self){
  114. _back = [self cameraWithPosition:AVCaptureDevicePositionBack];
  115. _front = [self cameraWithPosition:AVCaptureDevicePositionFront];
  116. _frontAspect = 1;
  117. _backAspect = 1;
  118. }
  119. return self;
  120. }
  121. - (void)focusOn:(CGPoint)point{
  122. if (self.back == nil) {
  123. return;
  124. }
  125. [self focusOn:point device:self.back];
  126. }
  127. - (void)focusOn:(CGPoint)point device:(AVCaptureDevice *)device{
  128. NSError* error;
  129. [device lockForConfiguration:&error];
  130. if(error != nil){
  131. return;
  132. }
  133. if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]){
  134. [device setFocusPointOfInterest: point];
  135. device.focusMode = AVCaptureFocusModeContinuousAutoFocus;
  136. }
  137. if ([device isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure]) {
  138. [device setExposurePointOfInterest:point];
  139. device.exposureMode = AVCaptureExposureModeContinuousAutoExposure;
  140. }
  141. [device unlockForConfiguration];
  142. }
  143. @end