快速avfoundation AVCapturePhotoCaptureDelegate捕获方法

时间:2017-07-28 15:20:40

标签: swift image-processing computer-vision avfoundation

您好我对swift和avfoundation框架相对较新。我目前正在尝试实施一个使用图像处理的自定义玩具相机应用程序。我的问题是我正在尝试使用我的图像处理算法自动处理每个帧或每个X帧然后将一个图层应用于预览,并且还允许用户捕获预览图像以及使用输出该图像的IBAction ui按钮屏幕。图像处理步骤将捕获未输出到屏幕的较低分辨率图像,而ibaction捕获应捕获标准jpeg并将其输出到屏幕。

我的问题是实现这样的事情的最佳方法是什么?

我注意到逻辑可能会进入AVCapturePhotoCaptureDelegate方法capture(_ captureOutput: AVCapturePhotoOutput, ...),但也会有两个不同的AVCapturePhotoOutput()对象调用capturePhoto(with: photoSettings, delegate: self)调用capture(_ captureOutput: AVCapturePhotoOutput, ...),我从了解。我会在capture(_ captureOutput: AVCapturePhotoOutput, ...)方法中检查哪个captureOutput对象是从不同的AVCapturePhotoOutput()对象调用的capturePhoto(带:photoSettings,delegate:self)方法发送的吗?

capture(_ captureOutput: AVCapturePhotoOutput, ...)的完整方法签名是

capture(_ captureOutput: AVCapturePhotoOutput,
                 didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?,previewPhotoSampleBuffer: CMSampleBuffer?,
                 resolvedSettings: AVCaptureResolvedPhotoSettings,
                 bracketSettings: AVCaptureBracketedStillImageSettings?,
                 error: Error?)

或者这两个不同的捕获函数是否可以在两个不同的线程上完成?

为了进一步说明,我有两个工作相机应用程序,它们有两种不同的功能。一个是图像处理和图层实现,另一个是预览捕获并输出到UI视图。相应的AVCapturePhotoCaptureDelegate捕获方法如下:

图像处理相机:

  public func capture(_ captureOutput: AVCapturePhotoOutput,
                      didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?,
                      previewPhotoSampleBuffer: CMSampleBuffer?,
                      resolvedSettings: AVCaptureResolvedPhotoSettings,
                      bracketSettings: AVCaptureBracketedStillImageSettings?,
                      error: Error?) {

    var imageTexture: MTLTexture?
    var previewImage: UIImage?
    if error == nil {
      imageTexture = convertToMTLTexture(sampleBuffer: photoSampleBuffer)
      previewImage = convertToUIImage(sampleBuffer: previewPhotoSampleBuffer)
    }
    delegate?.videoCapture(self, didCapturePhotoTexture: imageTexture, previewImage: previewImage)
  }

将图像捕获到UI相机:

func capture(_ captureOutput: AVCapturePhotoOutput,
             didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?,previewPhotoSampleBuffer: CMSampleBuffer?,
             resolvedSettings: AVCaptureResolvedPhotoSettings,
             bracketSettings: AVCaptureBracketedStillImageSettings?,
             error: Error?) {

    if let error = error {
        print("Error capturing photo: \(error)")
    } else {
        if let sampleBuffer = photoSampleBuffer,
           let previewBuffer = previewPhotoSampleBuffer,
        let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) {

            if let image = UIImage(data: dataImage) {
                self.capturedImage.image = image
            }
        }
    }

}

其中captureImage为@IBOutlet weak var capturedImage: UIImageView!,转换方法是自定义类中的函数。我如何将这两种捕获(...)方法的功能集成到一个应用程序中?

1 个答案:

答案 0 :(得分:0)

我认为苹果的文件AVCapturePhotoOutput很清楚。你应该看看。

我不知道两个不同的捕获函数是否在两个不同的线程上完成。由于我知道AVFoundation Kit不是开源的。

我觉得这不重要。

作为Apple的文档,您可以通过capturePhoto(with:delegate:)注册代理,并通过默认情况下实现的回调来获取图片photoOutput(_:didFinishProcessingPhoto:error:)

顺便说一下,方法photoOutput(_:didFinishProcessingPhoto:previewPhoto:resolvedSettings:bracketSettings:error:)已被弃用。你可以查看this page