2017-03-16 7 views
0

Ich analysiere Live-Bilder in einer Capture-Session vom Typ AVMediaTypeVideo. Wie kann ich ein qualitativ hochwertiges-Standbild (nicht der niedrigaufgelöste Probenpuffer), bei bestimmten Ereignissen =AVFoundation: Hochqualitatives Standbild in Video-Session nehmen

var videoCaptureDevice: AVCaptureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) 
var device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) 
var previewLayer: AVCaptureVideoPreviewLayer?  
var captureSession = AVCaptureSession() 
let cameraOutput = AVCapturePhotoOutput() 


//called in view did load 
private func setupCamera() { 

    let input = try? AVCaptureDeviceInput(device: videoCaptureDevice)   
    captureSession.sessionPreset = AVCaptureSessionPreset640x480 
    if self.captureSession.canAddInput(input) { 
     self.captureSession.addInput(input) 
    } 
    self.previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 


    let videoDataOutput = AVCaptureVideoDataOutput() 
    if self.captureSession.canAddOutput(videoDataOutput){ 
     self.captureSession.addOutput(videoDataOutput) 
     videoDataOutput.setSampleBufferDelegate(self, queue: DispatchQueue.main) 
    } 

} 

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

// sampleBuffer is analysed 
// if result is positive, I would like to take a high quality picture 

} 

Antwort

0

Hier ist ein kleiner Ausschnitt, den ich in meiner Apps nutzen. Ich hoffe, das

private func takePhoto() -> Void 
{ 
    if let videoConnection = stillImageOutput!.connection(withMediaType: AVMediaTypeVideo) 
    { 
     videoConnection.videoOrientation = AVCaptureVideoOrientation.portrait 

     stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (sampleBuffer, error) in 
      guard let buffer = sampleBuffer else 
      { 
       return 
      } 

      let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer) 
      let dataProvider = CGDataProvider(data: imageData as! CFData) 
      let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, 
       decode: nil, 
       shouldInterpolate: true, 
       intent: CGColorRenderingIntent.defaultIntent) 

      // The image taked 
      let image: UIImage = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.right) 

      // Detenemos la captura de imagenes 
      self.captureSession!.stopRunning() 
     }) 
    } 
} 

hilfreich sein, wenn Sie nicht über die sessionPreset Eigentum Ihrer captureSession Variable gesetzt, die standardmäßig seine Werte AVCapture​Session​Preset​High wird. Die einzige Voreinstellung, die höher als diese ist, ist AVCaptureSessionPresetPhoto

+0

danke für die Antwort! Ich habe Ihre Methode aufgerufen, wenn die Bedingungen erfüllt sind, und auch das AVCapture-Session-Preset auf Photo in Ihrer Methode setzen (weil ich das Foto in hoher Auflösung, aber die Vorschau in niedriger Auflösung brauche). Das Bild ist jedoch sehr dunkel. Weißt du, warum ich tun kann, damit es genug Licht bekommt? – Pascal

Verwandte Themen