2016-03-30 9 views
1

Ich versuche, eine Lichtintensität Leser mit der Kamera auf einem iPhone in Swift erstellen. Die Idee, die es ist, nimmt die Intensitätskomponente für alle Pixel und mittelt sie, um mir einen einzigen Wert zu geben. Ich brauche keine Vorschau der Kamera. Ich habe ein paar Tutorials zusammengestellt, um es zum Laufen zu bringen, und bin bis jetzt mit dem untenstehenden Code gekommen. camDeviceSetup() wird auf ViewDidLoad ausgeführt, cameraSetup() wird bei einem Tastendruck ausgeführt.Sample Buffer Delegieren Swift 2 für Echtzeit Video Filter

Ich laufe auf einen Fehler in der Zeile, die startet "videoDeviceOutput! .setSampleBufferDelegate", es sagt, es kann nicht konvertieren Wert vom Typ FirstViewController (der View-Controller) zu erwarteten Argument.

let captureSession = AVCaptureSession() 
// If we find a device we'll store it here for later use 
var captureDevice : AVCaptureDevice? 
var videoDeviceOutput: AVCaptureVideoDataOutput? 
// AVCaptureVideoPreviewLayer is a subclass of CALayer that you use to display video as it is being captured by an input device. 
var previewLayer = AVCaptureVideoPreviewLayer() 

func camDeviceSetup() { 
    captureSession.sessionPreset = AVCaptureSessionPreset640x480 
    let devices = AVCaptureDevice.devices() 
    for device in devices { 
     // Make sure this particular device supports video 
     if (device.hasMediaType(AVMediaTypeVideo)) { 
      // Finally check the position and confirm we've got the back camera 
      if(device.position == AVCaptureDevicePosition.Back) { 
       captureDevice = device as? AVCaptureDevice 
      } 
     } 
    } 
    if captureDevice != nil { 
     let err : NSError? = nil 
     captureSession.addInput(try! AVCaptureDeviceInput(device: captureDevice)) 

     if err != nil { 
      print("error: \(err?.localizedDescription)") 
     } 

    } 
} 

func cameraSetup() { 
    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 
    previewLayer.frame = view.bounds 
    view.layer.addSublayer(previewLayer) 

    videoDeviceOutput = AVCaptureVideoDataOutput() 
    videoDeviceOutput!.videoSettings = [kCVPixelBufferPixelFormatTypeKey:Int(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)] 
    videoDeviceOutput!.alwaysDiscardsLateVideoFrames = true 

//This is the line that gets stuck and not sure why 
    videoDeviceOutput!.setSampleBufferDelegate(self, queue: dispatch_queue_create("VideoBuffer", DISPATCH_QUEUE_SERIAL)) 

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

    captureSession.startRunning() 
} 

func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) { 
    // Think once the delegate is correctly set my algorithm for finding light intensity goes here 

} 
+0

Die Probleme in dieser Zeile lag daran, dass ich AVCaptureVideoDataOutputSampleBufferDelegate nicht in der Klasse oben in meinem ViewController deklarierte. – rmaspero

Antwort

0

Die Themen auf dieser Linie waren bis auf mich nicht an der Spitze meines Viewcontroller AVCaptureVideoDataOutputSampleBufferDelegate in der Klasse deklariert.

+0

hast du den AVCaptureVideoDataOutputSampleBufferDelegate dem vc zugewiesen? –