swift 中有没有办法使用 AVCaptureSession 对象逐帧获取?

Is there any way to get frame by frame using AVCaptureSession object in swift?

我必须使用我的 C++ 函数处理 iPhone 相机捕获的帧。所以我使用 startRunning() 函数开始数据流,但是我可以用什么方式处理每一帧?

是的,它非常简单。你需要

  1. 创建一个 AVCaptureVideoDataOutput 对象来生成视频帧
  2. 为 AVCaptureVideoDataOutput 对象实现一个委托来处理视频帧
  3. 在委托 class 中,实现写入示例缓冲区时调用的方法 (captureOutput:didOutputSampleBuffer:fromConnection:)。

有关更多信息,您可以 read this part AVFoundation 编程指南。代码示例在 Objective-C 而不是 swift 中,但我想你明白了。

我创建了一个 class 可以帮助你:CameraCaptureHelper 包装了所有 AVFoundation 的东西并在它的委托上调用一个方法,它在每个帧中传递一个 CIImage