
This video is only available to subscribers. Start a subscription today to get access to this and 484 other videos.
Camera Preview and Sample Buffer
This episode is part of a series: Camera Capture and Detection.
1. Intro 1 min |
2. Setting Up The Project 9 min |
3. Camera Preview and Sample Buffer 10 min |
4. Drawing on Top of the Video Preview 14 min |
5. Fixing the Coordinates 7 min |
6. Perspective Outlining 9 min |
7. Camera Sounds and UI 12 min |
8. Rendering the Final Image 15 min |
9. Capturing High Resolution Photos 17 min |
10. Adding a Flash Toggle Camera Control 12 min |
Previewing the Camera Input
We can utilize AVVideoCapturePreviewLayer
in order to see what the camera input is capturing. We'll do this in the setupCaptureSession()
method.
let cameraInput = try AVCaptureDeviceInput(device: camera)
captureSession.addInput(cameraInput)
// after adding the input...
let preview = AVCaptureVideoPreviewLayer(session: captureSession)
preview.frame = view.bounds
preview.backgroundColor = UIColor.black.cgColor
preview.videoGravity = .resizeAspect
view.layer.addSublayer(preview)
self.previewLayer = preview
Now we can see live video output!
Capturing the Samples
In order to capture samples we’ll have to add an output to our capture session. One of these output types is an AVCaptureVideoDataOutput
.
let output = AVCaptureVideoDataOutput()
output.alwaysDiscardsLateVideoFrames = true
output.setSampleBufferDelegate(self, queue: sampleBufferQueue)
We'll need to define that sampleBufferQueue
at the top of the class:
let sampleBufferQueue = DispatchQueue.global(qos: .userInteractive)
We also need to conform to this protocol:
extension CaptureViewController : AVCaptureVideoDataOutputSampleBufferDelegate {
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
print("sampleBuffer")
}
}
Finally, we can add this output to the capture session.
captureSession.addOutput(output)
Running this we see sampleBuffer
repeated in the console, letting us know it's working!