Capturing iSight image using AVFoundation on Mac

心不动则不痛 提交于 2019-12-07 01:15:38

问题


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;
    }
}

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?


回答1:


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.



来源:https://stackoverflow.com/questions/23048230/capturing-isight-image-using-avfoundation-on-mac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!