I'm trying to capture the video frames from camera and display flood of image on UIImageView
in realtime.
I tried to adapt AVCaptureVideoDataOutputSampleBufferDelegate
for my viewcontroller
.
I also implemented captureOutput
, but captureOutput
never get called.
Here is my code:
import UIKit
import AVFoundation
class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet var cameraView: UIView!
var selectedImage :UIImage!
let captureSession = AVCaptureSession()
var captureDevice : AVCaptureDevice?
var videoCaptureOutput : AVCaptureVideoDataOutput!
override func viewDidLoad() {
super.viewDidLoad()
captureSession.sessionPreset = AVCaptureSessionPresetLow
self.captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
if(captureDevice != nil){
beginSession()
}
}
func beginSession() {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), {
self.videoCaptureOutput = AVCaptureVideoDataOutput()
self.videoCaptureOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey:kCVPixelFormatType_32BGRA]
self.videoCaptureOutput.alwaysDiscardsLateVideoFrames = true
self.captureSession.addOutput(self.videoCaptureOutput)
var err : NSError? = nil
self.captureSession.addInput(AVCaptureDeviceInput(device: self.captureDevice, error: &err))
self.captureSession.sessionPreset = AVCaptureSessionPresetPhoto
if err != nil {
println("error: \(err?.localizedDescription)")
}
var previewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)
previewLayer?.frame = self.cameraView.layer.bounds
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
dispatch_async(dispatch_get_main_queue(), { // 2
// 3
self.cameraView.layer.addSublayer(previewLayer)
self.captureSession.startRunning()
});
});
}
func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
println("capture delegation called")
var imageProcessor = ImageProcessor()
imageView.image = imageProcessor.imageFromSampleBuffer(sampleBuffer)
}
}
As you can see, I'm trying to process image and display realtime capture frame on an imageview
, assume my ImageProcessor()
works perfectly....
Any help would be much appreciated, Thanks.
Your delegate method its not called because you are not setting the delegate to videoCaptureOutput at all. To do so you must call videoCaptureOutput setSampleBufferDelegate(self ,queue : queue )
method where queue parameter is queue on which callbacks should be invoked
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With