gpt4 book ai didi

swift - func captureOutput 永远不会被调用

转载 作者:搜寻专家 更新时间:2023-11-01 05:32:58 24 4
gpt4 key购买 nike

添加喜欢为我实时记录的每一帧添加过滤器并将过滤后的图像显示在 UIImageView 中,如果有人可以帮助它会很好。但从未调用 captureoutput,这是我的代码。

class Measurement: UIViewController , AVCaptureVideoDataOutputSampleBufferDelegate {
@IBOutlet weak var cameraPreview: UIView!
@IBOutlet weak var imageView: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()

setupCameraSession()
toggleTorch(on: true)

}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

view.layer.addSublayer(previewLayer)

cameraSession.startRunning()
}

lazy var cameraSession: AVCaptureSession = {
let s = AVCaptureSession()

s.sessionPreset = AVCaptureSession.Preset.low
return s
}()

lazy var previewLayer: AVCaptureVideoPreviewLayer = {
let preview = AVCaptureVideoPreviewLayer(session: self.cameraSession)
preview.position = CGPoint(x:182,y: 485)
preview.videoGravity = AVLayerVideoGravity.resizeAspectFill
preview.connection?.videoOrientation = AVCaptureVideoOrientation.portrait

preview.bounds = imageView.bounds
//preview.position = CGPoint(x:self.view.bounds.midX,y: self.view.bounds.midY)
imageView.layer.addSublayer(preview)

return preview
}()

func toggleTorch(on: Bool) {
guard let device = AVCaptureDevice.default(for: .video) else { return }

if device.hasTorch {
do {
try device.lockForConfiguration()

if on == true {
device.torchMode = .on
} else {
device.torchMode = .off
}

device.unlockForConfiguration()
} catch {
print("Torch could not be used")
}
} else {
print("Torch is not available")
}
}
func setupCameraSession() {
let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)

do {
let deviceInput = try AVCaptureDeviceInput(device: captureDevice!)

cameraSession.beginConfiguration()

if (cameraSession.canAddInput(deviceInput) == true) {
cameraSession.addInput(deviceInput)
print("Processing Data.")
}

let dataOutput = AVCaptureVideoDataOutput()
dataOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString) : NSNumber(value: kCVPixelFormatType_32BGRA as UInt32)] as [String : AnyObject]
dataOutput.alwaysDiscardsLateVideoFrames = true

print("Processing Data.")
if (cameraSession.canAddOutput(dataOutput) == true) {
cameraSession.addOutput(dataOutput)
print("Processing Data.")
}

cameraSession.commitConfiguration()

let queue = DispatchQueue(label: "com.invasivecode.videoQueue")
dataOutput.setSampleBufferDelegate(self, queue: queue)

}
catch let error as NSError {
print("\(error), \(error.localizedDescription)")
}
}

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {


print("Processing Data.")


guard let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
//let chromaKeyFilter = colorCubeFilterForChromaKey(hueAngle: 120)
let ciImage = CIImage(cvPixelBuffer: imageBuffer)

let context = CIContext()
guard let cgImage = context.createCGImage(ciImage, from: ciImage.extent) else { return }

let image = UIImage(cgImage: cgImage)
if let chromaKeyFilter = CIFilter(name: "CISepiaTone") {
let beginImage = CIImage(image: image)
chromaKeyFilter.setValue(beginImage, forKey: kCIInputImageKey)
chromaKeyFilter.setValue(0.5, forKey: kCIInputIntensityKey)

if let output = chromaKeyFilter.outputImage {
if let cgimg = context.createCGImage(output, from: output.extent) {
let processedImage = UIImage(cgImage: cgimg)
// do something interesting with the processed image
imageView.image = processedImage
}
}
}
}








func captureOutput(_ captureOutput: AVCaptureOutput, didDrop sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
// Here you can count how many frames are dopped
}
func startCapture() {
print("\(self.classForCoder)/" + #function)
if cameraSession.isRunning {

print("already running")
return
}
cameraSession.startRunning()
toggleTorch(on: true)


}

最佳答案

你需要设置委托(delegate)

dataOutput.sampleBufferDelegate = self

关于swift - func captureOutput 永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51324851/

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