CameraManager.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. AVCaptureDeviceFormat *bestFormat;
  22. CGFloat aspect = 1;
  23. //分辨率
  24. CGFloat radio = 16.0/9.0;
  25. if(model == SDK_CAPTURE_MODE_SINGLE || model == SDK_CAPTURE_MODE_CONTINUOUS) {
  26. radio = 4.0/3.0;
  27. }
  28. for (AVCaptureDeviceFormat *format in supportedFormats) {
  29. CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions((CMVideoFormatDescriptionRef)[format formatDescription]);
  30. if (((CGFloat)dimensions.width/(CGFloat)dimensions.height) == radio && dimensions.height == desiredHeight) {
  31. bestFormat = format;
  32. break;
  33. }
  34. }
  35. [device lockForConfiguration:nil];
  36. if (!bestFormat) {
  37. //未找到 匹配格式 查找固定匹配格式 (720 1080 2160)
  38. for (AVCaptureDeviceFormat *format in supportedFormats) {
  39. CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions((CMVideoFormatDescriptionRef)[format formatDescription]);
  40. CMVideoDimensions bestVideoDimensions = CMVideoFormatDescriptionGetDimensions((CMVideoFormatDescriptionRef)[bestFormat formatDescription]);
  41. if (((CGFloat)dimensions.width/(CGFloat)dimensions.height) == radio &&
  42. (dimensions.height == 720 || dimensions.height == 1080 || dimensions.height == 2160)) {
  43. if (bestVideoDimensions.height <= dimensions.height) {
  44. bestFormat = format;
  45. }
  46. }
  47. }
  48. }
  49. if (!bestFormat) {
  50. //未找到 固定格式 (720 1080 2160) 查找最佳格式(目标格式最近的分辨率)
  51. for (AVCaptureDeviceFormat *format in supportedFormats) {
  52. CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions((CMVideoFormatDescriptionRef)[format formatDescription]);
  53. CMVideoDimensions bestVideoDimensions = CMVideoFormatDescriptionGetDimensions((CMVideoFormatDescriptionRef)[bestFormat formatDescription]);
  54. if (((CGFloat)dimensions.width/(CGFloat)dimensions.height) == radio && dimensions.height <= desiredHeight ) {
  55. if (bestVideoDimensions.height <= dimensions.height) {
  56. bestFormat = format;
  57. }
  58. }
  59. }
  60. }
  61. if(bestFormat) {
  62. [device setActiveFormat:bestFormat];
  63. }
  64. [device setActiveVideoMaxFrameDuration:CMTimeMake(1, 10)];
  65. [device setActiveVideoMinFrameDuration:CMTimeMake(1, 10)];
  66. [device unlockForConfiguration];
  67. if(device.position == AVCaptureDevicePositionFront){
  68. self.frontAspect = aspect;
  69. }
  70. else if(device.position == AVCaptureDevicePositionBack){
  71. self.backAspect = aspect;
  72. }
  73. }
  74. - (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position
  75. {
  76. NSArray *devices = nil;
  77. if(@available(iOS 10, *)){
  78. AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera]
  79. mediaType:AVMediaTypeVideo
  80. position:position];
  81. devices = [captureDeviceDiscoverySession devices];
  82. }
  83. else if(@available(iOS 4, *)){
  84. devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
  85. }
  86. if(devices == nil){
  87. return nil;
  88. }
  89. for (AVCaptureDevice *device in devices)
  90. {
  91. if ([device position] == position)
  92. return device;
  93. }
  94. return nil;
  95. }
  96. + (instancetype)shared{
  97. static dispatch_once_t token;
  98. static CameraManager* instance = nil;
  99. dispatch_once(&token, ^{
  100. instance = [[self alloc] init];
  101. });
  102. return instance;
  103. }
  104. - (instancetype)init{
  105. self = [super init];
  106. if(self){
  107. _back = [self cameraWithPosition:AVCaptureDevicePositionBack];
  108. _front = [self cameraWithPosition:AVCaptureDevicePositionFront];
  109. _frontAspect = 1;
  110. _backAspect = 1;
  111. }
  112. return self;
  113. }
  114. - (void)focusOn:(CGPoint)point{
  115. if (self.back == nil) {
  116. return;
  117. }
  118. [self focusOn:point device:self.back];
  119. }
  120. - (void)focusOn:(CGPoint)point device:(AVCaptureDevice *)device{
  121. NSError* error;
  122. [device lockForConfiguration:&error];
  123. if(error != nil){
  124. return;
  125. }
  126. if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]){
  127. [device setFocusPointOfInterest: point];
  128. device.focusMode = AVCaptureFocusModeContinuousAutoFocus;
  129. }
  130. if ([device isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure]) {
  131. [device setExposurePointOfInterest:point];
  132. device.exposureMode = AVCaptureExposureModeContinuousAutoExposure;
  133. }
  134. [device unlockForConfiguration];
  135. }
  136. @end