AVCaptureOutput 的前世今生

1,110 阅读3分钟

前传 - AVCaptureStillImageOutput

捕获照片-AVCaptureStillImageOutput

在iOS中捕捉静态拍摄画面,可以通过很多方式,像是通过UIImagePickerController的方式,调用系统提供的API来捕捉画面,或是通过AVFoundation提供的AVCaptureStillImageOutpu或AVCapturePhotoOutput来捕捉画面。而本篇文章所采用的方法属于后者通过AVFoundation来实现拍照。

AVCaptureStillImageOutput

AVCaptureStillImageOutput是AVCaptureOutput具体子类,用来拍摄静态照片,该类在iOS 10.中已弃用,不支持较新的相机拍照功能,如RAW图像输出和动态照片。在iOS 10.0及更高版本中改用AVCapturePhotoOutput类。

- 拍照

调用下述方法拍照:

- (void)captureStillImageAsynchronouslyFromConnection:(AVCaptureConnection *)connection
completionHandler:(void (^)(CMSampleBufferRef imageDataSampleBuffer, NSError *error))handler;

参数:

  • connection: 从中捕获图像的连接。
  • handler: 捕获图像后要调用的块,块参数如下:
    imageDataSampleBuffer: 捕获的数据
    error: 如果请求失败,返回NSError对象,否则返回nil

可以使用下述属性查看是否正在拍照:

@property(readonly, getter=isCapturingStillImage) BOOL capturingStillImage;

- 图像输出设置

@property(nonatomic, copy) NSDictionary<NSString *, id> *outputSettings;

当前仅支持AVVideoCodecKey和kCVPixelBufferPixelFormatTypeKey 可以使用 -availableImageDataCVPixelFormatTypes 和 -availableImageDataCodecTypes 获取支持的codec keys 和 pixel formats

  • availableImageDataCVPixelFormatTypes
    支持的图像格式,可以将其作用在outputSettings中kCVPixelBufferPixelFormatTypeKey的值
  • availableImageDataCodecTypes:
    支持的图像编解码格式,可以将其作用在outputSettings中AVVideoCodecKey的值

- 图像格式转换

将静态图像数据CMSampleBufferRef转为NSData

+ (NSData *)jpegStillImageNSDataRepresentation:(CMSampleBufferRef)jpegSampleBuffer;
  • jpegSampleBuffer: 携带 JPEG 图像数据的样本缓冲区,如果jpegSampleBuffer为 NULL 或不是JPEG 格式,则此方法抛出异常NSInvalidArgumentException。

- 设置拍照方向

如果程序只支持纵向,但是如果用户横向拍照时,需要调整结果照片的方向

//判断是否支持设置视频方向
@property(nonatomic, readonly, getter=isVideoOrientationSupported) BOOL supportsVideoOrientation;

设置照片方向:

    // 获取连接
    AVCaptureConnection *connection = [self.imageOutput connectionWithMediaType:AVMediaTypeVideo];
    
    //程序只支持纵向,但是如果用户横向拍照时,需要调整结果照片的方向
    // 判断是否支持设置视频方向
    if (connection.isVideoOrientationSupported) {
        connection.videoOrientation = [self currentVideoOrientation];
    }
    
    [self.imageOutput captureStillImageAsynchronouslyFromConnection:connection completionHandler:^(CMSampleBufferRef  _Nullable imageDataSampleBuffer, NSError * _Nullable error) {
        if (imageDataSampleBuffer != NULL) {
            NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
            UIImage *image = [[UIImage alloc] initWithData:imageData];
            [self writeImageToAssetsLibrary:image];
        } else {
            NSLog(@"NULL imageDataSampleBuffer: %@", error.localizedDescription);
        }
    }];

// 获取方向值
- (AVCaptureVideoOrientation)currentVideoOrientation {
    AVCaptureVideoOrientation orientation;
    
    switch (UIDevice.currentDevice.orientation) {
        case UIDeviceOrientationPortrait:
            orientation = AVCaptureVideoOrientationPortrait;
            break;
        case UIDeviceOrientationLandscapeRight:
            orientation = AVCaptureVideoOrientationLandscapeLeft;
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            orientation = AVCaptureVideoOrientationPortraitUpsideDown;
            break;
            
        default:
            orientation = AVCaptureVideoOrientationLandscapeRight;
            break;
    }
    return orientation;
}

- 实例

AVCaptureStillImageOutput *myStillImageOutput = [[AVCaptureStillImageOutput alloc] init];

myStillImageOutput.outputSettings = @{AVVideoCodecKey: AVVideoCodecTypeJPEG};

今生 - AVCapturePhotoOutput

AVCapturePhotoOutput自定义简单相机

在iOS 10之前,自定义相机一般使用AVCaptureStillImageOutput实现。但是AVCaptureStillImageOutput在iOS 10以后被弃用了。

image.png

所以我们来使用AVCapturePhotoOutput来实现自定义简单相机,AVCapturePhotoOutput 的功能自然会更加强大,不仅支持简单JPEG图片拍摄,还支持Live照片和RAW格式拍摄。

使用:

首先初始化:按照需要参数初始化就行了,和AVCaptureStillImageOutput差别不大。直接上代码:

    self.session = [AVCaptureSession new];
    [self.session setSessionPreset:AVCaptureSessionPresetHigh];
    
    NSArray *devices = [NSArray new];
    devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for (AVCaptureDevice *device in devices) {
        if (isBack) {
            if ([device position] == AVCaptureDevicePositionBack) {
                _device = device;
                break;
            }
        }else {
            if ([device position] == AVCaptureDevicePositionFront) {
                _device = device;
                break;
            }
        }
    }
    
    NSError *error;
    self.input = [[AVCaptureDeviceInput alloc] initWithDevice:self.device error:&error];
    if ([self.session canAddInput:self.input]) {
        [self.session addInput:self.input];
    }
    
    self.imageOutput = [[AVCapturePhotoOutput alloc] init];
    NSDictionary *setDic = @{AVVideoCodecKey:AVVideoCodecJPEG};
    _outputSettings = [AVCapturePhotoSettings photoSettingsWithFormat:setDic];
    [self.imageOutput setPhotoSettingsForSceneMonitoring:_outputSettings];
    [self.session addOutput:self.imageOutput];
    self.preview = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
    [self.preview setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    [self.preview setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [self.layer addSublayer:self.preview];
    [self.session startRunning];

实现照片获取:
AVCaptureStillImageOutput中使用captureStillImageAsynchronouslyFromConnection 在bolck中直接可以获取到图片,AVCapturePhotoOutput需要实现AVCapturePhotoCaptureDelegate协议,在协议中获取。
获取当前屏幕图片输出:

[self.imageOutput capturePhotoWithSettings:_outputSettings delegate:self];

实现AVCapturePhotoCaptureDelegate协议,并在didFinishProcessingPhotoSampleBuffer方法中获取图片:

- (void)captureOutput:(AVCapturePhotoOutput *)captureOutput didFinishProcessingPhotoSampleBuffer:(nullable CMSampleBufferRef)photoSampleBuffer previewPhotoSampleBuffer:(nullable CMSampleBufferRef)previewPhotoSampleBuffer resolvedSettings:(AVCaptureResolvedPhotoSettings *)resolvedSettings bracketSettings:(nullable AVCaptureBracketedStillImageSettings *)bracketSettings error:(nullable NSError *)error {
    
    NSData *data = [AVCapturePhotoOutput JPEGPhotoDataRepresentationForJPEGSampleBuffer:photoSampleBuffer previewPhotoSampleBuffer:previewPhotoSampleBuffer];
    UIImage *image = [UIImage imageWithData:data];
    
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

这样就获取到图片了。