CameraManager.m 4.3 KB

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