Last active
May 14, 2020 21:42
-
-
Save Danesz/ffbc15a25b1e1b807d8f51b9cfccd3db to your computer and use it in GitHub Desktop.
CameraViewController.swift - init
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var deviceInput: AVCaptureDeviceInput! | |
session.sessionPreset = AVCaptureSession.Preset.vga640x480 | |
// acquire the camera device | |
guard let device = AVCaptureDevice | |
.default(AVCaptureDevice.DeviceType.builtInWideAngleCamera, | |
for: .video, | |
position: AVCaptureDevice.Position.front) else { | |
return | |
} | |
do { | |
deviceInput = try AVCaptureDeviceInput(device: device) | |
guard deviceInput != nil else { | |
print("error: can't get deviceInput") | |
return | |
} | |
// add the Input device to the session | |
if self.session.canAddInput(deviceInput){ | |
self.session.addInput(deviceInput) | |
} | |
// define a Video output device to capture video frames from the camera | |
videoDataOutput = AVCaptureVideoDataOutput() | |
videoDataOutput.alwaysDiscardsLateVideoFrames = true | |
videoDataOutputQueue = DispatchQueue(label: "VideoDataOutputQueue") | |
videoDataOutput.setSampleBufferDelegate(self, queue:self.videoDataOutputQueue) | |
if session.canAddOutput(self.videoDataOutput){ | |
session.addOutput(self.videoDataOutput) | |
} | |
//define a Picture output device to capture still images from the camera | |
stillImageOutput = AVCapturePhotoOutput() | |
if session.canAddOutput(stillImageOutput) { | |
session.addOutput(self.stillImageOutput) | |
} | |
videoDataOutput.connection(with: .video)?.isEnabled = true | |
// define the camera preview view | |
previewLayer = AVCaptureVideoPreviewLayer(session: self.session) | |
previewLayer.videoGravity = AVLayerVideoGravity.resizeAspect | |
// ... | |
// and start running the capture session | |
session.startRunning() | |
} catch let error as NSError { | |
deviceInput = nil | |
print("error: \(error.localizedDescription)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment