洛阳学员端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
6.7 KiB

7 months ago
  1. //
  2. // CameraService.h
  3. // DTFCameraService
  4. //
  5. // Created by Tommy Li on 2017/8/23.
  6. // Copyright © 2017年 EyeVerify. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import <AVFoundation/AVFoundation.h>
  10. #if defined(__cplusplus)
  11. #define DTF_EXPORT extern "C"
  12. #else
  13. #define DTF_EXPORT extern
  14. #endif
  15. /// NSString keys that are used to specify configuration in DTFCameraService initialization.
  16. typedef NSString* const DTFCameraInitKey;
  17. /// NSString keys that are used to change configurations of DTFCameraService;
  18. typedef NSString* const DTFCameraConfigKey;
  19. /**
  20. Funtion type signature that is used for -takePicture: callbacks after a still image is captured.
  21. @param imageDataSampleBuffer The sample buffer of the still image. It is nil if any error occurred.
  22. @param error NSError that indicates any error during the capture process. It is nil if no error occurred.
  23. */
  24. typedef void(^DTFCameraTakePhotoCompletion)(CMSampleBufferRef imageDataSampleBuffer, NSError *error);
  25. /// Initialization Key for specifying device position during initialization. Associated value must be a AVCaptureDevicePosition.
  26. DTF_EXPORT DTFCameraInitKey DTFCameraInitKeyCaptureDevicePosition;
  27. /// Initialization Key for specifying quality preset during initialization. Associated value must be a AVCaptureSessionPreset.
  28. DTF_EXPORT DTFCameraInitKey DTFCameraInitKeySessionPreset;
  29. /// Initialization Key for specifying video orientation during initialization. Associated value must be a AVCaptureVideoOrientation.
  30. DTF_EXPORT DTFCameraInitKey DTFCameraInitKeyVideoOutputOrientation;
  31. /// Initialization Key for specifying video mode during initialization. Associated value must be a DTFCameraMode
  32. DTF_EXPORT DTFCameraInitKey DTFCameraInitKeyMode;
  33. /**
  34. Configuration Key for specifying focus point of interest. Associated value must be a CGPoint (converted to an NSString).
  35. Note that the associated value will be directly applied to camera without transformations, so regarding valid inputs please review focusPointOfInterest of AVCaptureDevice;
  36. */
  37. DTF_EXPORT DTFCameraConfigKey DTFCameraConfigKeyFocusPointOfInterest;
  38. /// Error domain name for DTFCameraService
  39. DTF_EXPORT NSString *const kDTFCameraErrorDomain;
  40. /// Error codes used by DTFCameraService to indicate the type of errors occured.
  41. typedef NS_ENUM(NSInteger, DTFCameraErrorCode) {
  42. /// Error code indicates that an invalid preset string is given during initialization.
  43. DTFCameraUnsupportedPreset,
  44. /// Error code indicates that no devices are available with the specified configuration.
  45. DTFCameraNotAvailable,
  46. /// Error code indicates that the capture session has failed.
  47. DTFCameraSessionFailure,
  48. };
  49. /// Modes that DTFCameraService supports
  50. typedef NS_ENUM(NSInteger, DTFCameraMode) {
  51. /// Video
  52. DTFCameraModeVideo,
  53. /// Still Image
  54. DTFCameraModeStillImage
  55. };
  56. /**
  57. A structure that stores camera configuration for associated camera service
  58. */
  59. typedef struct camera_configuration_t{
  60. /// Focus Point of Interest
  61. CGPoint focusPointOfInterest;
  62. }camera_configuration;
  63. /**
  64. Delegate to comply in order to receive camera output.
  65. */
  66. @protocol DTFCameraServiceDelegate <NSObject>
  67. @optional
  68. /**
  69. This function will be called by DTFCameraService to pass capture device output to the delegate.
  70. Implement this optional function and call -setDelegate: if you have set DTFCameraModeVideo and wish to receive output.
  71. @param captureOutput Output source that triggered this delegation.
  72. @param sampleBuffer A CMSampleBuffer output from said output source.
  73. @param connection The AVCaptureConnection related to the output.
  74. */
  75. - (void)cameraControllerCaptureOutput:(AVCaptureOutput *)captureOutput
  76. didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
  77. fromConnection:(AVCaptureConnection *)connection;
  78. @end
  79. /**
  80. Main interface of DTFCameraService.
  81. */
  82. @interface DTFCameraService : NSObject <AVCaptureVideoDataOutputSampleBufferDelegate>
  83. /// Property that allows inspections the current configuration of DTFCameraService
  84. @property(nonatomic, assign, readonly)camera_configuration configuration;
  85. /// Preview layer of the capture session. Add this layer as a sublayer to a view to inspect the device output.
  86. @property(nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;
  87. /// Get current video output size
  88. @property (nonatomic, assign, readonly) CGSize videoOutputSize;
  89. /**
  90. Do not use default NSObject initializer. Use initWithConfig:error: instead.
  91. */
  92. - (instancetype)init NS_UNAVAILABLE;
  93. /**
  94. Initialize the camera service.
  95. If DTFCameraInitKeyCaptureDevicePosition is not specified in config, AVCaptureDevicePositionFront will be used as camera position.
  96. If DTFCameraInitKeySessionPreset is not specified in config, AVCaptureSessionPresetHigh will be used as quality preset.
  97. If DTFCameraInitKeyVideoOutputOrientation is not specified in config, output orientation will not be set. Video will be AVCaptureVideoOrientationLandscapeRight, and StillImage will be AVCaptureVideoOrientationPortrait.
  98. If DTFCameraInitKeyMode is not specified in config, DTFCameraModeVideo will be used as video output.
  99. @param config An NSDictionary that contains configuration keys and associated values to specify initialization requirements.
  100. @param errPtr An optional error receiver for initialization failures. Use nil if error information is not needed.
  101. @return If initialization is successful, a instance of DTFCameraService is returned. Anything failture during initialization would cause this initialzer to return nil.
  102. */
  103. - (instancetype)initWithConfig:(NSDictionary *)config error:(NSError **)errPtr;
  104. /**
  105. Set delegate that DTFCameraService can service
  106. @param delegate An instance of class that implements DTFCameraServiceDelegate
  107. */
  108. - (void)setDelegate:(id<DTFCameraServiceDelegate>) delegate;
  109. /**
  110. Change one configuration value in camera_configuration.
  111. @param key Configuration key
  112. @param value Properly formatted in-range value as a string
  113. @return YES if configuration was successfully changed. NO otherwise.
  114. */
  115. - (BOOL)changeConfiguration:(NSString *)key
  116. desiredValue:(NSString *)value;
  117. - (BOOL)setWhiteBalanceMode:(AVCaptureWhiteBalanceMode)mode;
  118. - (BOOL)setExposureMode:(AVCaptureExposureMode)mode;
  119. /**
  120. Start the capture session.
  121. The capture session needs to be started manually. This is a synchronous operation.
  122. */
  123. - (void)startCamera;
  124. /**
  125. Stop the capture session.
  126. This is a synchronous operation.
  127. */
  128. - (void)stopCamera;
  129. /**
  130. Capture a still image asynchronously.
  131. @param completion Completion delegate when the capture process finishes. For required parameters see DTFCameraTakePhotoCompletion.
  132. */
  133. - (void)takePicture:(DTFCameraTakePhotoCompletion)completion;
  134. @end