Skip to content

Instantly share code, notes, and snippets.

@koji
Last active August 29, 2015 14:25
Show Gist options
  • Save koji/97252f9b2a03511b7d84 to your computer and use it in GitHub Desktop.
Save koji/97252f9b2a03511b7d84 to your computer and use it in GitHub Desktop.
import UIKit
import AVFoundation
class ViewController: UIViewController {
// Session
var myFrontSession : AVCaptureSession!
var myBackSession : AVCaptureSession!
var myFrontVideoLayer: AVCaptureVideoPreviewLayer!
var myBackVideoLayer: AVCaptureVideoPreviewLayer!
// device Front Camera & Back Camera
var myFrontDevice : AVCaptureDevice!
var myBackDevice : AVCaptureDevice!
// output of the image
var myFrontImageOutput : AVCaptureStillImageOutput!
var myBackImageOutput : AVCaptureStillImageOutput!
var pickerController = UIImagePickerController()
var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
@IBOutlet weak var frontView: UIView!
//@IBOutlet weak var backView: UIView!
@IBOutlet weak var backImgView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// create session
myFrontSession = AVCaptureSession()
myBackSession = AVCaptureSession()
// get the list of devices
let devices = AVCaptureDevice.devices()
// store back camera in myDevice
for device in devices{
/*
case Unspecified
case Back
case Front
*/
if(device.position == AVCaptureDevicePosition.Front){
//myDevice = device as! AVCaptureDevice
myFrontDevice = device as! AVCaptureDevice
}
if(device.position == AVCaptureDevicePosition.Back){
//myDevice = device as! AVCaptureDevice
myBackDevice = device as! AVCaptureDevice
}
}
print(myFrontDevice)
print(myBackDevice)
startFrontCamera()
startBackCamera()
}
func startFrontCamera() {
print(myFrontDevice)
do {
let videoFrontInput = try AVCaptureDeviceInput(device: myFrontDevice)
// add above to session
myFrontSession.addInput(videoFrontInput)
myFrontImageOutput = AVCaptureStillImageOutput()
myFrontSession.addOutput(myFrontImageOutput)
} catch let error as NSError {
// Handle any errors
print(error)
}
// create layer to display image
myFrontVideoLayer = AVCaptureVideoPreviewLayer(session: myFrontSession)
myFrontVideoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
//myFrontVideoLayer.frame = self.view.bounds
myFrontVideoLayer.frame = frontView.bounds
// add view
frontView.layer.addSublayer(myFrontVideoLayer)
myFrontSession.startRunning()
}
func startBackCamera() {
print(myBackDevice)
do {
//let myDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
let videoBackInput = try AVCaptureDeviceInput(device: myBackDevice)
// add above to session
myBackSession.addInput(videoBackInput)
myBackImageOutput = AVCaptureStillImageOutput()
myBackSession.addOutput(myBackImageOutput)
} catch let error as NSError {
// Handle any errors
print(error)
}
myBackVideoLayer = AVCaptureVideoPreviewLayer(session: myBackSession)
myBackVideoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
//myBackVideoLayer.frame = self.view.bounds
myBackVideoLayer.frame = backImgView.bounds
// add view
backImgView.layer.addSublayer(myBackVideoLayer)
myBackSession.startRunning()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment