Created
January 29, 2017 19:15
-
-
Save anonymous/57e953f7bf13268ef12f3b33056b8efe to your computer and use it in GitHub Desktop.
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
// | |
// ViewController.swift | |
// University | |
// | |
// Created by God on 03/01/2017. | |
// Copyright © 2017 xapp. All rights reserved. | |
// | |
import UIKit | |
import AVFoundation | |
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { | |
@IBOutlet weak var cameraView: UIImageView! | |
var captureSession = AVCaptureSession() | |
var sessionOutput = AVCapturePhotoOutput() | |
var sessionOutputSetting = AVCapturePhotoSettings(format: [AVVideoCodecKey:AVVideoCodecJPEG]) | |
var previewLayer = AVCaptureVideoPreviewLayer() | |
// Bool to manage camera toggle. False = front-face (default) | |
var toggle = false | |
override func viewWillAppear(_ animated: Bool) { | |
super.viewWillAppear(true) | |
pickCamera(which: toggle); | |
} | |
func pickCamera(which: Bool) { | |
if (which == true) { | |
let deviceDescovery = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDualCamera, AVCaptureDeviceType.builtInTelephotoCamera, AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.back) | |
print("back camera starting") | |
startCamera(deviceDesc: deviceDescovery!) | |
toggle = true; | |
} else if (which == false) { | |
let deviceDescovery = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDualCamera, AVCaptureDeviceType.builtInTelephotoCamera, AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.front) | |
print("front camera starting") | |
startCamera(deviceDesc: deviceDescovery!) | |
toggle = false; | |
} | |
} | |
func startCamera(deviceDesc: AVCaptureDeviceDiscoverySession!) { | |
for device in (deviceDesc.devices)! { | |
if (device.position == AVCaptureDevicePosition.back) { | |
do { | |
let input = try AVCaptureDeviceInput(device: device) | |
if (captureSession.canAddInput(input)) { | |
captureSession.addInput(input) | |
if (captureSession.canAddOutput(sessionOutput)) { | |
captureSession.addOutput(sessionOutput) | |
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) | |
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill | |
previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.portrait | |
cameraView.layer.addSublayer(previewLayer) | |
captureSession.startRunning() | |
} else { print("Cannot add input - back") } | |
} | |
} | |
catch { | |
print("Exception") | |
} | |
} else if (device.position == AVCaptureDevicePosition.front) { | |
do { | |
let input = try AVCaptureDeviceInput(device: device) | |
print(input) | |
if (captureSession.canAddInput(input)) { | |
captureSession.addInput(input) | |
if (captureSession.canAddOutput(sessionOutput)) { | |
captureSession.addOutput(sessionOutput) | |
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) | |
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill | |
previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.portrait | |
cameraView.layer.addSublayer(previewLayer) | |
captureSession.startRunning() | |
} | |
} else { print("Cannot add input - front") } | |
} | |
catch { | |
print(error) | |
} | |
} | |
} | |
} | |
@IBOutlet weak var tempImageView: UIImageView! | |
@IBAction func takePhoto(_sender: Any) { | |
print("Taking Photo") | |
} | |
override func viewDidLayoutSubviews() { | |
super.viewDidLayoutSubviews() | |
previewLayer.frame = cameraView.bounds | |
} | |
@IBAction func toggleCamera(_ sender: Any) { | |
if (toggle == false) { | |
print("Changing to back camera") | |
let currentCameraInput: AVCaptureInput = captureSession.inputs[0] as! AVCaptureInput | |
captureSession.removeInput(currentCameraInput) | |
toggle = true; | |
pickCamera(which: toggle) | |
} else if (toggle == true) { | |
print("Changing to front camera") | |
let currentCameraInput: AVCaptureInput = captureSession.inputs[0] as! AVCaptureInput | |
captureSession.removeInput(currentCameraInput) | |
toggle = false; | |
pickCamera(which: toggle) | |
} | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment