gpt4 book ai didi

ios - CMSampleBufferGetImageBuffer(sampleBuffer) 返回 nil

转载 作者:搜寻专家 更新时间:2023-10-31 22:09:45 24 4
gpt4 key购买 nike

我使用此代码从相机捕获视频,但 CMSampleBufferGetImageBuffer(sampleBuffer) 始终返回 nil。问题是什么?。这是代码,我修改了此源代码以适应 Swift 4 https://github.com/FlexMonkey/CoreImageHelpers/blob/master/CoreImageHelpers/coreImageHelpers/CameraCaptureHelper.swift

import AVFoundation
import CoreMedia
import CoreImage
import UIKit


class CameraCaptureHelper: NSObject
{
let captureSession = AVCaptureSession()
let cameraPosition: AVCaptureDevice.Position

weak var delegate: CameraCaptureHelperDelegate?

required init(cameraPosition: AVCaptureDevice.Position)
{
self.cameraPosition = cameraPosition

super.init()

initialiseCaptureSession()
}

fileprivate func initialiseCaptureSession()
{
captureSession.sessionPreset = AVCaptureSession.Preset.photo

guard let camera = AVCaptureDevice.default(.builtInWideAngleCamera,
for: .video, position: cameraPosition)
else {
fatalError("Unable to access camera")
}
do
{
let input = try AVCaptureDeviceInput(device: camera)

captureSession.addInput(input)
}
catch
{
fatalError("Unable to access back camera")
}

let videoOutput = AVCaptureVideoDataOutput()

videoOutput.setSampleBufferDelegate(self,
queue: DispatchQueue(label: "sample buffer delegate", attributes: []))

if captureSession.canAddOutput(videoOutput)
{
captureSession.addOutput(videoOutput)
}

captureSession.startRunning()
}
}

extension CameraCaptureHelper: AVCaptureVideoDataOutputSampleBufferDelegate
{

func captureOutput(_ output: AVCaptureOutput, didDrop sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {

connection.videoOrientation = .landscapeRight //AVCaptureVideoOrientation(rawValue: UIApplication.shared.statusBarOrientation.rawValue)!

guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else
{
return
}

DispatchQueue.main.async
{
self.delegate?.newCameraImage(self,
image: CIImage(cvPixelBuffer: pixelBuffer))
}
}

}

protocol CameraCaptureHelperDelegate: class
{
func newCameraImage(_ cameraCaptureHelper: CameraCaptureHelper, image: CIImage)
}

最佳答案

您正在尝试从“刚刚删除样本缓冲区”回调访问像素缓冲区。头文件说:

CMSampleBuffer object passed to this delegate method will contain metadata about the dropped video frame, such as its duration and presentation time stamp, but will contain no actual video data.

您应该从 didOutputSampleBuffer: 委托(delegate)回调中执行此操作。

关于ios - CMSampleBufferGetImageBuffer(sampleBuffer) 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51150267/

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