Several enumerated values

Capture the quality properties of the picture

We can change the quality of captured images by setting some AVCaptureSession properties, but note: Size related properties need to be tested first to see if the device supports canSetSessionPreset AVCaptureSessionPresetPhoto, suitable for high resolution picture quality output AVCaptureSessionPresetHigh, suitable for high quality video and audio output AVCaptureSessionPresetMedium - suitable for medium quality output, suitable for WiFi sharing of the output video and audio bitrate AVCaptureSessionPresetLow - is suitable for the low quality of the output, For the fit for Shared by 3 g output video and audio bitrate AVCaptureSessionPreset320x240 - applies to 320 x240 video output AVCaptureSessionPreset352x288 - applies to 352 x288 video output AVCaptureSessionPreset640x480 - applies to 640 x480 video output AVCaptureSessionPreset960x540 - applies to 960 x540 video output AVCaptureSessionPreset1280x720 - applies to 1280 x720 video output AVCaptureSessionPreset1920x1080 - applies to 1920 x1080 video output AVCaptureSessionPreset3840x2160 - applies to 3840 x2160 (UHD 4 k) video output AVCaptureSessionPresetiFrame960x540 - can generate 960 x540 Apple IFrame Video and audio content. To achieve 960x540-quality iFrame H.264 video at ~30 Mbits/SEC using AAC audio. Capture the QuickTime movie in iFrame format is most suitable for editing application AVCaptureSessionPresetiFrame1280x720 - can generate 1280 x720 Apple iFrame video and audio content. To achieve 1280x720 quality iFrame H.264 video at ~40 Mbits/SEC using AAC audio. Capture the QuickTime movie in iFrame format is most suitable for editing application AVCaptureSessionPresetInputPriority -- indicating session input format is preferred, this change indicates that the client choose the input format now decide the quality of service provided by the output level. When the client sets the session preset to anything else, except AVCaptureSessionPresetInputPriority session will continue to be responsible for the configuration of input and output, and can according to need to change its input activeFormatCopy the code

Set the camera direction

typedef NS_ENUM(NSInteger, AVCaptureDevicePosition) { AVCaptureDevicePositionUnspecified = 0, / / the direction of the recently opened, the direction of the records in recent time open AVCaptureDevicePositionBack = 1, / / / / rear camera AVCaptureDevicePositionFront = 2 front-facing cameras} NS_AVAILABLE (10 _7, 4 _0) __TVOS_PROHIBITED;Copy the code

Setting flash Mode

Typedef NS_ENUM(NSInteger, AVCaptureFlashMode) {AVCaptureFlashModeOff = 0, // AVCaptureFlashModeOff = 1, // Enable flash AVCaptureFlashModeAuto = 2 // Automatic mode} NS_AVAILABLE(10_7, 4_0) __TVOS_PROHIBITED;Copy the code

Flashlight mode

Typedef NS_ENUM(NSInteger, AVCaptureTorchMode) {AVCaptureTorchModeOff = 0, // AVCaptureTorchModeOff = 1, } NS_AVAILABLE(10_7, 4_0) __tvos_NLF3;Copy the code

System focus mode

typedef NS_ENUM(NSInteger, AVCaptureAutoFocusSystem) { AVCaptureAutoFocusSystemNone = 0, / / don't set the focus mode AVCaptureAutoFocusSystemContrastDetection = 1, / / contrast detection af AVCaptureAutoFocusSystemPhaseDetection = 2, } NS_AVAILABLE_IOS(8_0) __tvos_nlF3;Copy the code

Video anti-jitter mode

typedef NS_ENUM(NSInteger, AVCaptureVideoStabilizationMode) { AVCaptureVideoStabilizationModeOff = 0, / / video shake mode off AVCaptureVideoStabilizationModeStandard = 1, / / video stabilization standards mode AVCaptureVideoStabilizationModeCinematic = 2, / / video stabilization film pattern AVCaptureVideoStabilizationModeAuto = 1, / / video stabilization automatic mode} NS_AVAILABLE_IOS __TVOS_PROHIBITED _0 (8);Copy the code

The focal length adjustment

Typedef NS_ENUM (NSInteger AVCaptureFocusMode) {AVCaptureFocusModeLocked = 0, / / lock focus AVCaptureFocusModeAutoFocus = 1, / / af mode AVCaptureFocusModeContinuousAutoFocus = 2, / / continuous autofocus} NS_AVAILABLE (10 _7, 4 _0) __TVOS_PROHIBITED;Copy the code

Autofocus range limit

typedef NS_ENUM(NSInteger, AVCaptureAutoFocusRangeRestriction) { AVCaptureAutoFocusRangeRestrictionNone = 0, Does not restrict AVCaptureAutoFocusRangeRestrictionNear = 1 / /, / / close mode AVCaptureAutoFocusRangeRestrictionFar = 2, } NS_AVAILABLE_IOS(7_0) __tvos_NLF3;Copy the code

Exposure Mode Setting

typedef NS_ENUM(NSInteger, AVCaptureExposureMode) { AVCaptureExposureModeLocked = 0, / / lock exposure AVCaptureExposureModeAutoExpose = 1, / / ae model AVCaptureExposureModeContinuousAutoExposure = 2, / / continuous automatic exposure modes AVCaptureExposureModeCustom NS_ENUM_AVAILABLE_IOS _0 (8) = 3, / / custom exposure modes} NS_AVAILABLE (10 _7, 4_0) __TVOS_PROHIBITED;Copy the code

White balance model

typedef NS_ENUM(NSInteger, AVCaptureWhiteBalanceMode) { AVCaptureWhiteBalanceModeLocked = 0, / / lock the white balance mode AVCaptureWhiteBalanceModeAutoWhiteBalance = 1, / / automatic AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance = 2, continuous automatic / /} NS_AVAILABLE (10 _7, 4 _0) __TVOS_PROHIBITED;Copy the code

Authorization Status Indicates whether the user is allowed to enable the device

typedef NS_ENUM(NSInteger, AVAuthorizationStatus) { AVAuthorizationStatusNotDetermined = 0, AVAuthorizationStatusRestricted / / authorization state has not yet been determined, / / authorization restricted AVAuthorizationStatusDenied, / / authorization denied AVAuthorizationStatusAuthorized / / authorized licensee} NS_AVAILABLE_IOS __TVOS_PROHIBITED _0 (7);Copy the code

Enumeration – Playback control mode that represents the current playback mode of the transport control

typedef NS_ENUM(NSInteger, AVCaptureDeviceTransportControlsPlaybackMode) {
 AVCaptureDeviceTransportControlsNotPlayingMode      = 0,
 AVCaptureDeviceTransportControlsPlayingMode         = 1
} NS_AVAILABLE(10_7, NA) __TVOS_PROHIBITED;
Copy the code

Several objects

Understanding the existence of several objects

  • AVCaptureDevice // Hardware device
  • AVCaptureInput // The input device
  • AVCaptureOutput // Output data
  • AVCaotureSession // Assist in input and output data transfer

Relationship: There are many Device inputs and many types of outputs that are controlled and transmitted by a CaptureSession: CaputureDevice ADAPTS to AVCaptureInput, which is input to AVCaptureOutput through Session, thus achieving the purpose of persistent transfer from device to file (such as collecting images from camera device to UIImage).

So if the video input and the corresponding video output, the audio corresponding to the audio, therefore need to establish the corresponding Connections, this connection object is held by AVCaptureSession, This object, called AVCaptureConnection, controls the transfer of input and output data (available through various input ports).

AVCaotureSession

self.captureSession = [[AVCaptureSession alloc] init];
[self.captureSession startRunning];
Copy the code

You want to create a session, send a RUNNING message, respond, and then you’re going to put whatever’s on the input device into the output device.

If you want to replace a new device in an already used session (already startRunning), delete the old device

[session beginConfiguration];
[session commitConfiguration];
Copy the code
- (BOOL) switchcameras{// Determine whether there are multiple cameras if (! [self canSwitchCameras]){ return NO; } // Get the current device's reverse device NSError *error; AVCaptureDevice *videoDevice = [self inactiveCamera]; // Return the currently inactive camera // Encapsulate the input device as AVCaptureDeviceInput AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error]; If (videoInput) {self. CaptureSession beginConfiguration [self. CaptureSession beginConfiguration]; / / will capture session, the original capture input device to remove [self. CaptureSession removeInput: self. ActiveVideoInput]; If ([self.captureSession canAddInput:videoInput]) {// If ([self.captureSession canAddInput:videoInput]) { Then use videoInput as the new video capture device [self.capturesession addInput:videoInput]; ActiveVideoInput = videoInput; }else {// If a new device cannot be added. Will the original video capture device to rejoin the capture session [self. CaptureSession addInput: self. ActiveVideoInput]; } // After the configuration is complete, AVCaptureSession commitConfiguration will batch all changes together. [self.captureSession commitConfiguration]; }else{ return NO; } return YES; }Copy the code

AVCaptureDevice

Device is a one-to-one representation of hardware, an AVCaptureDevice object, corresponding to an actual hardware Device \

/ * create and configure input device * / AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo]; If (device = = nil) {UIAlertController * alert = [UIAlertController alertControllerWithTitle: @ "prompt" message: @ "no camera equipment" preferredStyle:UIAlertControllerStyleAlert]; [alert addAction: [UIAlertAction actionWithTitle: @ "confirmed" style: UIAlertActionStyleDefault handler: ^ (UIAlertAction * _Nonnull  action) { }]]; [self presentViewController:alert animated:YES completion:nil]; return; } // Device _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; // Input _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil]; //Out put AVCaptureStillImageOutput *imageOutput; AVCaptureMovieFileOutput *movieOutput; Self.movieoutput = [[AVCaptureMovieFileOutput alloc]init];Copy the code

Then add input to session mode (check if it can be added)

// Session Before adding I/O devices, check whether the mobile phone (device) can add I/O devices. _session = [[AVCaptureSession alloc]init]; [_session setSessionPreset:AVCaptureSessionPresetHigh]; if ([_session canAddInput:self.input]) { [_session addInput:self.input]; } //if ([_session canAddOutput:self.output]) //{ // [_session addOutput:self.output]; //} if ([self.captureSession canAddOutput:self.movieOutput]){ [self.captureSession addOutput:self.movieOutput]; }Copy the code

AVCaptureOutput

In ios, Output can be divided into MovieFile (Output as movie file), VideoData (applicable to processing of various frames), AudioData (sound collection) and StillImage (StillImage taking). They all inherit from AVCaptureOutput

AVCaptureDeviceInput

    self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
Copy the code