CameraManager.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. for (AVCaptureDeviceFormat *format in supportedFormats) {
  24. CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions((CMVideoFormatDescriptionRef)[format formatDescription]);
  25. //分辨率
  26. if (dimensions.width <= desiredWidth) {
  27. if(dimensions.height > 0)
  28. aspect = (CGFloat)dimensions.width / dimensions.height;
  29. bestFormat = format;
  30. }
  31. }
  32. [device lockForConfiguration:nil];
  33. [device setActiveFormat:bestFormat];
  34. [device setActiveVideoMaxFrameDuration:CMTimeMake(1, 10)];
  35. [device setActiveVideoMinFrameDuration:CMTimeMake(1, 10)];
  36. [device unlockForConfiguration];
  37. if(device.position == AVCaptureDevicePositionFront){
  38. self.frontAspect = aspect;
  39. }
  40. else if(device.position == AVCaptureDevicePositionBack){
  41. self.backAspect = aspect;
  42. }
  43. }
  44. - (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position
  45. {
  46. NSArray *devices = nil;
  47. if(@available(iOS 10, *)){
  48. AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera]
  49. mediaType:AVMediaTypeVideo
  50. position:position];
  51. devices = [captureDeviceDiscoverySession devices];
  52. }
  53. else if(@available(iOS 4, *)){
  54. devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
  55. }
  56. if(devices == nil){
  57. return nil;
  58. }
  59. for (AVCaptureDevice *device in devices)
  60. {
  61. if ([device position] == position)
  62. return device;
  63. }
  64. return nil;
  65. }
  66. + (instancetype)shared{
  67. static dispatch_once_t token;
  68. static CameraManager* instance = nil;
  69. dispatch_once(&token, ^{
  70. instance = [[self alloc] init];
  71. });
  72. return instance;
  73. }
  74. - (instancetype)init{
  75. self = [super init];
  76. if(self){
  77. _back = [self cameraWithPosition:AVCaptureDevicePositionBack];
  78. _front = [self cameraWithPosition:AVCaptureDevicePositionFront];
  79. _frontAspect = 1;
  80. _backAspect = 1;
  81. }
  82. return self;
  83. }
  84. - (void)focusOn:(CGPoint)point{
  85. if (self.back == nil) {
  86. return;
  87. }
  88. [self focusOn:point device:self.back];
  89. }
  90. - (void)focusOn:(CGPoint)point device:(AVCaptureDevice *)device{
  91. NSError* error;
  92. [device lockForConfiguration:&error];
  93. if(error != nil){
  94. return;
  95. }
  96. if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]){
  97. [device setFocusPointOfInterest: point];
  98. device.focusMode = AVCaptureFocusModeContinuousAutoFocus;
  99. }
  100. if ([device isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure]) {
  101. [device setExposurePointOfInterest:point];
  102. device.exposureMode = AVCaptureExposureModeContinuousAutoExposure;
  103. }
  104. [device unlockForConfiguration];
  105. }
  106. @end