AVFoundation:如何在sampleBuffer(AVCaptureSessionPreset)中调整帧大小不会影响它

时间:2016-11-14 13:55:43

标签: macos avfoundation video-capture video-processing

我正在尝试从我的macBook相机捕获视频帧并动态处理它们(以便以后进行人脸检测)。为减少内存使用量,我想将捕获分辨率从预设值1200x720降低到640x480。

以下是设置Capture Session的代码:

_session = [[AVCaptureSession alloc] init];

    if ([_session canSetSessionPreset:AVCaptureSessionPreset640x480]){
        [_session setSessionPreset:AVCaptureSessionPreset640x480];
        NSLog(@"resolution preset changed");
    }

    // configure input
    _camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    _deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:_camera error:nil];


    // configure output
    _videoOutput = [[AVCaptureVideoDataOutput alloc] init];
    NSDictionary* newSettings = @{ (NSString*) kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA)};
    _videoOutput.videoSettings = newSettings;
    //discard if the data output queue is blocked
    [_videoOutput setAlwaysDiscardsLateVideoFrames:YES];

    // process frames on another queue
    dispatch_queue_t videoDataOutputQueue;
    videoDataOutputQueue = dispatch_queue_create("VideoDataOutputQueue", DISPATCH_QUEUE_SERIAL);
    [_videoOutput setSampleBufferDelegate:videoBufferDelegate queue:videoDataOutputQueue];

    [_session addInput:_deviceInput];
    [_session addOutput:_videoOutput];

    [_session startRunning];

此后,会话设置正确,它会正确记录“更改预设分辨率”,并将视频数据转发给另一个队列上的委托进行处理。当我检查session.sessionPreset时,它表示预设为AVCaptureSessionPreset640x480

现在在代表中:

- (void)captureOutput:(AVCaptureOutput *)captureOutput
        didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
        fromConnection:(AVCaptureConnection *)connection
{

    //get the image from buffer, transform it to CIImage
    CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    self.image = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer];

检查self.image.extent.size时,显示1200x720的大小不正确,好像我之前没有更改过预设。即使在检查方法的争论sampleBuffer时,它也会将尺寸显示为1200x720。

我现在浏览了几个小时的互联网和苹果参考,但无法找到解决方案。我希望你能救我!

1 个答案:

答案 0 :(得分:0)

我似乎已经找到了自己的解决方案(或者至少是一种解决方法)。注释掉以下几行会导致缓冲区分辨率的预期变化:

    NSDictionary* settings = @{ (NSString*) kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA)};
    _videoOutput.videoSettings = settings;

我的假设是,设置这些压缩设置会导致覆盖AVCaptureSessionPreset。但是,我发现为什么会出现这种情况并不完全清楚(压缩设置不应该对分辨率设置产生影响,对吧?)。