CameraManager.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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)desiredWidth{
  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. // CMTime maxFrameDuration;
  24. // float maxSeconds = 15;
  25. // AVFrameRateRange *bestFrameRateRange = nil;//
  26. for (AVCaptureDeviceFormat *format in supportedFormats) {
  27. CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions((CMVideoFormatDescriptionRef)[format formatDescription]);
  28. //分辨率
  29. if (dimensions.width <= desiredWidth) {
  30. if(dimensions.height > 0)
  31. aspect = (CGFloat)dimensions.width / dimensions.height;
  32. bestFormat = format;
  33. }
  34. }
  35. [device lockForConfiguration:nil];
  36. [device setActiveFormat:bestFormat];
  37. [device setActiveVideoMaxFrameDuration:CMTimeMake(1, 10)];
  38. [device setActiveVideoMinFrameDuration:CMTimeMake(1, 10)];
  39. [device unlockForConfiguration];
  40. if(device.position == AVCaptureDevicePositionFront){
  41. self.frontAspect = aspect;
  42. }
  43. else if(device.position == AVCaptureDevicePositionBack){
  44. self.backAspect = aspect;
  45. }
  46. }
  47. - (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position
  48. {
  49. NSArray *devices = nil;
  50. if(@available(iOS 10, *)){
  51. AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera]
  52. mediaType:AVMediaTypeVideo
  53. position:position];
  54. devices = [captureDeviceDiscoverySession devices];
  55. }
  56. else if(@available(iOS 4, *)){
  57. devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
  58. }
  59. if(devices == nil){
  60. return nil;
  61. }
  62. for (AVCaptureDevice *device in devices)
  63. {
  64. if ([device position] == position)
  65. return device;
  66. }
  67. return nil;
  68. }
  69. + (instancetype)shared{
  70. static dispatch_once_t token;
  71. static CameraManager* instance = nil;
  72. dispatch_once(&token, ^{
  73. instance = [[self alloc] init];
  74. });
  75. return instance;
  76. }
  77. - (instancetype)init{
  78. self = [super init];
  79. if(self){
  80. _back = [self cameraWithPosition:AVCaptureDevicePositionBack];
  81. _front = [self cameraWithPosition:AVCaptureDevicePositionFront];
  82. _frontAspect = 1;
  83. _backAspect = 1;
  84. }
  85. return self;
  86. }
  87. - (void)focusOn:(CGPoint)point{
  88. if (self.back == nil) {
  89. return;
  90. }
  91. [self focusOn:point device:self.back];
  92. }
  93. - (void)focusOn:(CGPoint)point device:(AVCaptureDevice *)device{
  94. NSError* error;
  95. [device lockForConfiguration:&error];
  96. if(error != nil){
  97. return;
  98. }
  99. if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]){
  100. [device setFocusPointOfInterest: point];
  101. device.focusMode = AVCaptureFocusModeContinuousAutoFocus;
  102. }
  103. if ([device isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure]) {
  104. [device setExposurePointOfInterest:point];
  105. device.exposureMode = AVCaptureExposureModeContinuousAutoExposure;
  106. }
  107. [device unlockForConfiguration];
  108. }
  109. //
  110. //
  111. //- (void)configureCamera:(AVCaptureDevice *)device forFrameRate:(int)frameRate {
  112. // AVCaptureDeviceFormat *bestFormat = nil;
  113. // AVFrameRateRange *bestFrameRateRange = nil;
  114. // for ( AVCaptureDeviceFormat *format in [device formats] ) {
  115. // for ( AVFrameRateRange *range in format.videoSupportedFrameRateRanges ) {
  116. // //NSLog(@"Format %@",format);
  117. // if ( range.maxFrameRate == frameRate ) {
  118. // bestFormat = format;
  119. // bestFrameRateRange = range;
  120. // }
  121. // }
  122. // }
  123. //
  124. // //NSLog(@"Format %@",bestFormat);
  125. //
  126. // if ( bestFormat ) {
  127. // if ( [device lockForConfiguration:NULL] == YES ) {
  128. // device.activeFormat = bestFormat;
  129. // device.activeVideoMinFrameDuration = bestFrameRateRange.minFrameDuration;
  130. // device.activeVideoMaxFrameDuration = bestFrameRateRange.minFrameDuration;
  131. // [device unlockForConfiguration];
  132. // }
  133. // }
  134. //}
  135. //
  136. //
  137. //- (void)configureCamera{
  138. // /// 参数设置
  139. // // 默认后置摄像头
  140. // AVCaptureDevicePosition position = AVCaptureDevicePositionBack;
  141. // // 帧率
  142. // int frameRate = 25;
  143. // // 显色方案
  144. // OSType videoFormat = kCVPixelFormatType_32BGRA;
  145. // // 分辨率高
  146. // int resolutionHeight = 720;
  147. //
  148. // /// 创建AVCaptureSession对象
  149. // AVCaptureSession *session = [[AVCaptureSession alloc] init];
  150. // /// 设置分辨率
  151. // session.sessionPreset = AVCaptureSessionPreset1280x720;
  152. // /// 获取摄像头
  153. // AVCaptureDevice *captureDevice;
  154. // // 默认AVCaptureDevicePositionBack,后置摄像头
  155. // AVCaptureDeviceDiscoverySession *deviceDiscoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera] mediaType:AVMediaTypeVideo position:position];
  156. // NSArray *devices = deviceDiscoverySession.devices;
  157. // for (AVCaptureDevice *device in devices) {
  158. // if (AVCaptureDevicePositionBack == device.position) {
  159. // captureDevice = device;
  160. // }else if (AVCaptureDevicePositionFront == device.position){
  161. // captureDevice = device;
  162. // }
  163. // }
  164. // /// 设置帧率和分辨率高度
  165. // BOOL isSuccess = NO;
  166. // for(AVCaptureDeviceFormat *vFormat in [captureDevice formats]) {
  167. // CMFormatDescriptionRef description = vFormat.formatDescription;
  168. // float maxRate = ((AVFrameRateRange*) [vFormat.videoSupportedFrameRateRanges objectAtIndex:0]).maxFrameRate;
  169. // if (maxRate >= frameRate && CMFormatDescriptionGetMediaSubType(description) == videoFormat) {
  170. // if ([captureDevice lockForConfiguration:NULL] == YES) {
  171. // // 对比镜头支持的分辨率和当前设置的分辨率
  172. // CMVideoDimensions dims = CMVideoFormatDescriptionGetDimensions(description);
  173. // if (dims.height == resolutionHeight && dims.width == [self.class getResolutionWidthByHeight:resolutionHeight]) {
  174. // [session beginConfiguration];
  175. // if ([captureDevice lockForConfiguration:NULL]){
  176. // captureDevice.activeFormat = vFormat;
  177. // [captureDevice setActiveVideoMinFrameDuration:CMTimeMake(1, frameRate)];
  178. // [captureDevice setActiveVideoMaxFrameDuration:CMTimeMake(1, frameRate)];
  179. // [captureDevice unlockForConfiguration];
  180. // }
  181. // [session commitConfiguration];
  182. // isSuccess = YES;
  183. // }
  184. // }else {
  185. // NSLog(@"%s: 失败",__func__);
  186. // }
  187. // }
  188. // }
  189. //
  190. //
  191. //
  192. //
  193. //}
  194. //
  195. /////需要考虑横竖屏情况,这里暂未考虑
  196. //+ (int)getResolutionWidthByHeight:(int)height {
  197. // switch (height) {
  198. // case 2160:
  199. // return 3840;
  200. // case 1080:
  201. // return 1920;
  202. // case 720:
  203. // return 1280;
  204. // case 480:
  205. // return 640;
  206. // default:
  207. // return -1;
  208. // }
  209. //}
  210. @end