在Mac上使用AVFoundation捕获iSight图像 [英] Capturing iSight image using AVFoundation on Mac

查看:108
本文介绍了在Mac上使用AVFoundation捕获iSight图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前有以下代码,可以使用QTKit从Mac的iSight摄像机捕获单个图像:

I previously had this code to capture a single image from a Mac's iSight camera using QTKit:

- (NSError*)takePicture
{    
    BOOL success;
    NSError* error;

    captureSession = [QTCaptureSession new];
    QTCaptureDevice* device = [QTCaptureDevice defaultInputDeviceWithMediaType: QTMediaTypeVideo];

    success = [device open: &error];
    if (!success) { return error; }

    QTCaptureDeviceInput* captureDeviceInput = [[QTCaptureDeviceInput alloc] initWithDevice: device];
    success = [captureSession addInput: captureDeviceInput error: &error];
    if (!success) { return error; }

    QTCaptureDecompressedVideoOutput* captureVideoOutput = [QTCaptureDecompressedVideoOutput new];
    [captureVideoOutput setDelegate: self];

    success = [captureSession addOutput: captureVideoOutput error: &error];
    if (!success) { return error; }

    [captureSession startRunning];
    return nil;
}
- (void)captureOutput: (QTCaptureOutput*)captureOutput
  didOutputVideoFrame: (CVImageBufferRef)imageBuffer
     withSampleBuffer: (QTSampleBuffer*)sampleBuffer
       fromConnection: (QTCaptureConnection*)connection
{
    CVBufferRetain(imageBuffer);

    if (imageBuffer) {
        [captureSession removeOutput: captureOutput];
        [captureSession stopRunning];

        NSCIImageRep* imageRep = [NSCIImageRep imageRepWithCIImage: [CIImage imageWithCVImageBuffer: imageBuffer]];

        _result = [[NSImage alloc] initWithSize: [imageRep size]];
        [_result addRepresentation: imageRep];

        CVBufferRelease(imageBuffer);

        _done = YES;
    }
}

但是,我今天发现QTKit已被弃用,因此我们现在必须使用AVFoundation. 谁能帮助我将此代码转换为等效的AVFoundation?似乎许多方法都具有相同的名称,但同时又有很多不同,在这里我完全不知所措...有什么帮助吗?

However, I found today that QTKit has been deprecated and so we must now use AVFoundation. Can anyone help me convert this code to its AVFoundation equivalent? It seems as though many methods have the same name, but at the same time, a lot is different and I'm at a complete loss here... Any help?

推荐答案

好的,我找到了解决方案!在这里:

Alright, I found the solution!! Here it is:

- (void)takePicture
{
    NSError* error;
    AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo];
    AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice: device error: &error];
    if (!input) {
        _error = error;
        _done = YES;
        return;
    }

    AVCaptureStillImageOutput* output = [AVCaptureStillImageOutput new];
    [output setOutputSettings: @{(id)kCVPixelBufferPixelFormatTypeKey: @(k32BGRAPixelFormat)}];

    captureSession = [AVCaptureSession new];
    captureSession.sessionPreset = AVCaptureSessionPresetPhoto;

    [captureSession addInput: input];
    [captureSession addOutput: output];
    [captureSession startRunning];

    AVCaptureConnection* connection = [output connectionWithMediaType: AVMediaTypeVideo];
    [output captureStillImageAsynchronouslyFromConnection: connection completionHandler: ^(CMSampleBufferRef sampleBuffer, NSError* error) {
        if (error) {
            _error = error;
            _result = nil;
        }
        else {
            CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

            if (imageBuffer) {
                CVBufferRetain(imageBuffer);

                NSCIImageRep* imageRep = [NSCIImageRep imageRepWithCIImage: [CIImage imageWithCVImageBuffer: imageBuffer]];

                _result = [[NSImage alloc] initWithSize: [imageRep size]];
                [_result addRepresentation: imageRep];

                CVBufferRelease(imageBuffer);
            }
        }

        _done = YES;
    }];
}

我希望这可以帮助在做同一件事上有任何问题的任何人.

I hope this helps whoever has any problems in doing this same thing.

这篇关于在Mac上使用AVFoundation捕获iSight图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆