输出相机帧缓冲器即将到来

时间:2018-01-31 08:31:55

标签: ios swift

我为相机类实施了AVCaptureVideoDataOutputSampleBufferDelegate

设置所需的视频输入并启动会话,相机工作正常,可以在预览中看到它

我也希望得到像素缓冲区进行一些处理

fileprivate func configureVideoOutput() {
    let videoOutput = AVCaptureVideoDataOutput()
    videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "sample buffer"))

    if self.session.canAddOutput(videoOutput) {
        print("canAddOutput yes")
        self.session.addOutput(videoOutput)
        print("canAddOutput yes added")
    } else {
        print("canAddOutput no")
    }
}

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
    print("Got a frame!")
}

打印"canAddOutput yes added"确定

但它永远不会打印"Got a frame!"

2 个答案:

答案 0 :(得分:1)

您必须先设置输出类型

videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA]

编辑: 将委托功能更改为

public func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
   print("Got a frame 1!") 
}

答案 1 :(得分:0)

通过调用AVCaptureSession,我无法查看您是否已启动start()。接下来,当您使用delegateself设置为setSampleBufferDelegate时,您尝试开始录制,但是在将videoOutput添加到您的会话之前调用它,因此它也可能是一个问题。 我在GitHub上有一个小项目,演示了如何设置自定义相机https://github.com/ChernyshenkoTaras/CustomCamera,你可以将它作为示例使用