我需要同时使用AVCaptureVideoDataOutput和AVCaptureMetadataOutput

时间:2016-03-08 16:37:03

标签: ios avfoundation

我正在编写一个需要查看原始视频的应用程序(自定义边缘检测等)使用元数据条形码阅读器。

即使AVCaptureSession有一个addOutput:方法而不是setOutput:方法,这正是它正在做的 - 第一个是胜利。

如果我首先添加AVCaptureVideoDataOutput作为输出 - 它的委托被调用。 如果我首先添加AVCaptureMetadataOutput作为输出 - 它的委托被调用。

有没有人想出办法解决这个问题? 没有每隔一帧移除另一个?

1 个答案:

答案 0 :(得分:0)

我能够同时添加AVCaptureVideoDataOutputAVCaptureMetadataOutput

NSError *error = nil;

self.captureSession = [[AVCaptureSession alloc] init];
[self.captureSession setSessionPreset:AVCaptureSessionPresetHigh];

// Select a video device, make an input
AVCaptureDevice *captureDevice;
AVCaptureDevicePosition desiredPosition = AVCaptureDevicePositionFront;
// Find the front facing camera
for (AVCaptureDevice *device in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
    if ([device position] == desiredPosition) {
        captureDevice = device;
        break;
    }
}

AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];

if (!error) {

    [self.captureSession beginConfiguration];

    // add the input to the session
    if ([self.captureSession canAddInput:deviceInput]) {
        [self.captureSession addInput:deviceInput];
    }

    AVCaptureMetadataOutput *metadataOutput = [AVCaptureMetadataOutput new];
    if ([self.captureSession canAddOutput:metadataOutput]) {
        [self.captureSession addOutput:metadataOutput];

        self.metaDataOutputQueue = dispatch_queue_create("MetaDataOutputQueue", DISPATCH_QUEUE_SERIAL);
        [metadataOutput setMetadataObjectsDelegate:self queue:self.metaDataOutputQueue];
        [metadataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
    }

    self.videoDataOutput = [AVCaptureVideoDataOutput new];
    if ([self.captureSession canAddOutput:self.videoDataOutput]) {
        [self.captureSession addOutput:self.videoDataOutput];

        NSDictionary *rgbOutputSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCMPixelFormat_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
        [self.videoDataOutput setVideoSettings:rgbOutputSettings];
        [self.videoDataOutput setAlwaysDiscardsLateVideoFrames:YES];

        self.videoDataOutputQueue = dispatch_queue_create("VideoDataOutputQueue", DISPATCH_QUEUE_SERIAL);
        [self.videoDataOutput setSampleBufferDelegate:self queue:self.videoDataOutputQueue];

        [[self.videoDataOutput connectionWithMediaType:AVMediaTypeVideo] setEnabled:YES];
    }

    [self.captureSession commitConfiguration];
    [self.captureSession startRunning];
}