1

我正在创建带有滤镜的自定义相机。当我添加以下行时,它会崩溃而不显示任何异常。

//设置视频输出

func setupBuffer() {
    videoBuffer = AVCaptureVideoDataOutput()
    videoBuffer?.alwaysDiscardsLateVideoFrames = true
    videoBuffer?.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString): NSNumber(value: kCVPixelFormatType_32RGBA)]
    videoBuffer?.setSampleBufferDelegate(self, queue: DispatchQueue.main)
    captureSession?.addOutput(videoBuffer)
}


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

    if connection.videoOrientation != .portrait {
        connection.videoOrientation = .portrait
    }
        guard let image = GMVUtility.sampleBufferTo32RGBA(sampleBuffer) else {
            print("No Image ")
            return
        }

    pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
    ciImage = CIImage(cvImageBuffer: pixelBuffer!, options: CMCopyDictionaryOfAttachments(kCFAllocatorDefault, sampleBuffer, kCMAttachmentMode_ShouldPropagate)as! [String : Any]?)

    CameraView.filter = CIFilter(name: "CIPhotoEffectProcess")
    CameraView.filter?.setValue(ciImage, forKey: kCIInputImageKey)
    let cgimg = CameraView.context.createCGImage(CameraView.filter!.outputImage!, from: ciImage.extent)

    DispatchQueue.main.async  {
        self.preview.image = UIImage(cgImage: cgimg!)
    }
}

但它正在崩溃 -

  guard let image = GMVUtility.sampleBufferTo32RGBA(sampleBuffer) else {
                print("No Image ")
                return
            }

当我传递从 CIImage 创建的图像时,它无法识别图像中的人脸。完整的代码文件是https://www.dropbox.com/s/y1ewd1sh18h3ezj/CameraView.swift.zip?dl=0

在此处输入图像描述

4

1 回答 1

0

1)为缓冲区创建单独的队列。

 fileprivate var videoDataOutputQueue = DispatchQueue(label: "VideoDataOutputQueue")

2)用这个设置缓冲区

        let videoBuffer = AVCaptureVideoDataOutput()
        videoBuffer?.alwaysDiscardsLateVideoFrames = true
        videoBuffer?.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString): NSNumber(value: kCVPixelFormatType_32BGRA)]
        videoBuffer?.setSampleBufferDelegate(self, queue: videoDataOutputQueue ) //
        captureSession?.addOutput(videoBuffer)
于 2017-11-09T13:08:20.763 回答