gpt4 book ai didi

ios - captureOutput() 函数从未被调用 swift4

转载 作者:行者123 更新时间:2023-11-29 05:40:02 25 4
gpt4 key购买 nike

我正在尝试实时捕获相机帧,以便使用 Firebase ML KIT 进行处理。我已成功显示相机 View ,但似乎无法调用 captureOutput 委托(delegate)函数。

P.s 我是 iOS 开发新手。

private func startLiveVideo() {

self.session.sessionPreset = AVCaptureSession.Preset.photo
let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)
let deviceInput = try! AVCaptureDeviceInput(device: captureDevice!)

self.session.addInput(deviceInput)

let deviceOutput = AVCaptureVideoDataOutput()

deviceOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_32BGRA)]
deviceOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))

self.session.addOutput(AVCaptureVideoDataOutput())
let imageLayer = AVCaptureVideoPreviewLayer(session: session)


imageLayer.frame = CGRect(x: 0, y: 0, width: self.imageView.frame.size.width + 100, height: self.imageView.frame.size.height)
imageLayer.videoGravity = .resizeAspectFill
imageView.layer.addSublayer(imageLayer)

self.session.startRunning()

}

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
print("Frame captured")
}

最佳答案

您添加代表

let deviceOutput = AVCaptureVideoDataOutput() 
deviceOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_32BGRA)]
deviceOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))

但在此处添加另一个实例

self.session.addOutput(AVCaptureVideoDataOutput())

所以将其替换为

self.session.addOutput(deviceOutput)

关于ios - captureOutput() 函数从未被调用 swift4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56650493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com