【发布时间】:2023-03-04 16:46:02
【问题描述】:
您好,我对 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(with: 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! 并且转换方法是自定义类中的函数。我将如何将这两种 capture(...) 方法的功能集成到一个应用程序中?
【问题讨论】:
-
最好是完成方法名` capture(_ captureOutput: AVCapturePhotoOutput, ...) `, 是func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?,已解决设置:AVCaptureResolvedPhotoSettings,括号设置:AVCaptureBrackedStillImageSettings?,错误:错误?)
-
正确。这是完整方法的名称。
标签: swift image-processing computer-vision avfoundation