func captureOutput永远不会被调用

时间:2018-07-13 12:12:23

标签: swift

要在我实时记录的每个帧中添加滤镜,并在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)


}

1 个答案:

答案 0 :(得分:0)

您需要设置代表

dataOutput.sampleBufferDelegate = self