Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // PhotoController.swift
- // WrapApp
- //
- // Created by Bryan Green on 9/1/15.
- // green.c.bryan@gmail.com
- // Copyright (c) 2015 Bryan Green. All rights reserved.
- //
- import UIKit
- import AVFoundation
- import ImageIO
- import QuartzCore
- class PhotoController : UIViewController,
- AVCaptureVideoDataOutputSampleBufferDelegate {
- @IBOutlet var videoPreview : UIView!
- @IBOutlet var takePhotoButton : UIButton!
- @IBOutlet var switchDeviceButton: UIButton!
- @IBOutlet var continueButton: UIButton!
- @IBOutlet var photoPreview : UIImageView!
- @IBOutlet var photoStage : UILabel!
- @IBOutlet var skipButton: UIButton!
- var photoSession :AVCaptureSession?
- var cameraDevice : AVCaptureDevice?
- var coreAnimationLayer : CALayer?
- var videoPreviewLayer : AVCaptureVideoPreviewLayer?
- var queue : dispatch_queue_t?
- var photo : AVCaptureStillImageOutput?
- var inputDevice : AVCaptureDeviceInput?
- override func viewDidLoad() {
- super.viewDidLoad()
- //let tap = UILongPressGestureRecognizer(target:self,action:"snapPicture")
- //self.view.addGestureRecognizer(tap)
- initCameraSession()
- initPhotoPreview()
- }
- @IBAction func skipPhotos()
- {
- print("skipPhotos")
- }
- //*********Video Capture**********//
- func initPhotoPreview()
- {
- self.photoPreview.hidden = true
- self.photoPreview.layer.borderColor = UIColor(red: 153.0/255.0, green: 204.0/255.0, blue: 51.0/255.0, alpha: 1.0).CGColor
- self.photoPreview.layer.borderWidth = 2.0
- self.photoPreview.contentMode = UIViewContentMode.ScaleAspectFill
- self.photoPreview.clipsToBounds = true
- }
- func initCameraSession()
- {
- //create a new photo session
- photoSession = AVCaptureSession()
- //setup the resolution for photo presets
- photoSession?.sessionPreset = AVCaptureSessionPresetPhoto
- //set default video device to front
- cameraDevice = setCamera(AVCaptureDevicePosition.Front)
- /*set focus
- if((cameraDevice?.isFocusModeSupported(AVCaptureFocusMode.ContinuousAutoFocus)) != nil)
- {
- cameraDevice?.focusMode = AVCaptureFocusMode.ContinuousAutoFocus
- }
- //set flash to auto
- cameraDevice?.torchMode = AVCaptureTorchMode.Auto
- */
- //try to set the input device for the session
- inputDevice = AVCaptureDeviceInput.deviceInputWithDevice(cameraDevice, error: nil) as? AVCaptureDeviceInput
- print("input set to device \(cameraDevice?.description)")
- photoSession?.addInput(inputDevice)
- /* Video Output
- if
- let outputData = AVCaptureVideoDataOutput() as? AVCaptureVideoDataOutput
- {
- photoSession?.addOutput(outputData)
- outputData.videoSettings = [kCVPixelBufferPixelFormatTypeKey:kCVPixelFormatType_32BGRA]
- queue = dispatch_queue_create("PhotoQueue", nil)
- outputData.setSampleBufferDelegate(self, queue: queue)
- }
- */
- //setup the preview layers
- videoPreviewLayer = AVCaptureVideoPreviewLayer(session: photoSession)
- coreAnimationLayer = videoPreview.layer
- videoPreviewLayer?.frame = videoPreview.bounds
- videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
- coreAnimationLayer?.addSublayer(videoPreviewLayer)
- //ask for the camera
- requestAccessForMedia()
- //setup still image output
- photo = AVCaptureStillImageOutput()
- photo?.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG ]
- photoSession?.addOutput(photo)
- //start the session
- photoSession?.startRunning()
- print("photo session started: \(photoSession?.self.running)")
- }
- func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
- //print("captureOutput:didOutputSampleBufferFromConnection")
- }
- //get the first video device with the selected position
- func setCamera(devicePosition: AVCaptureDevicePosition) -> AVCaptureDevice?
- {
- if
- let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
- {
- for device in devices
- {
- let device = device as! AVCaptureDevice
- if(device.position == devicePosition){
- return device;
- }
- }
- }
- return nil;
- }
- /*
- @IBAction func retakePhoto() {
- print("retakePhoto called...")
- self.photoPreview.hidden = true
- self.photoPreview.image = nil
- /* NATIVE photo chooser
- //Swift 1.2
- var picker : UIImagePickerController = UIImagePickerController()
- picker.delegate = self;
- picker.allowsEditing = true;
- picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
- self.presentViewController(picker, animated: true, completion: nil);
- //Objective-C
- UIImagePickerController *picker = [[UIImagePickerController alloc] init];
- picker.delegate = self;
- picker.allowsEditing = YES;
- picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- [self presentViewController:picker animated:YES completion:NULL];
- */
- }
- */
- func requestAccessForMedia()
- {
- AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (result:Bool)->Void in
- print("access granted for camera: \(result)")
- })
- }
- func videoConnection() -> AVCaptureConnection
- {
- var captureConnection : AVCaptureConnection?
- if
- let connections = photo?.connections
- {
- for connection in connections
- {
- for port in connection.inputPorts as! [AVCaptureInputPort]
- {
- if(port.mediaType == AVMediaTypeVideo)
- {
- captureConnection = connection as? AVCaptureConnection
- break;
- }
- }
- if(captureConnection != nil)
- {
- break;
- }
- }
- }
- else
- {
- print("videoConnection: unable to get current video connection")
- }
- return captureConnection!
- }
- @IBAction func savePhoto()
- {
- if let image : UIImage = self.photoPreview.image{
- //write the photo to the album
- UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
- }
- }
- func snapPicture()
- {
- print("snapPicture:")
- let captureConnection = videoConnection()
- //set orientation and mirroring
- if(captureConnection.supportsVideoMirroring){
- captureConnection.videoMirrored = true
- }
- if(captureConnection.supportsVideoOrientation){
- captureConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
- }
- print("requesting a capture from: \(captureConnection)")
- photo?.captureStillImageAsynchronouslyFromConnection(captureConnection, completionHandler: {(imageSampleBuffer,error)->Void in
- if let exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, nil)
- {
- print("attachnents: \(exifAttachments)")
- }
- let imageData : NSData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageSampleBuffer)
- let image : UIImage = UIImage(data: imageData)!
- //set the photo preview
- self.photoPreview.image = image
- self.photoPreview.setNeedsDisplay()
- self.photoPreview.hidden = false
- /*
- self.photoPreview.frame = CGRectMake(self.photoPreview.frame.origin.x,
- self.photoPreview.frame.origin.y,
- image.size.width,
- image.size.height)
- */
- })
- /* NATIVE photo dialog
- //Swift 1.2
- var picker : UIImagePickerController = UIImagePickerController()
- picker.delegate = self;
- picker.allowsEditing = true;
- picker.sourceType = UIImagePickerControllerSourceType.Camera;
- self.presentViewController(picker, animated: true, completion: nil);
- //Objective C
- UIImagePickerController *picker = [[UIImagePickerController alloc] init];
- picker.delegate = self;
- picker.allowsEditing = YES;
- picker.sourceType = UIImagePickerControllerSourceTypeCamera;
- [self presentViewController:picker animated:YES completion:NULL];*/
- }
- @IBAction func takePhoto() {
- snapPicture()
- }
- @IBAction func switchDevice(){
- print("switchDevice:")
- photoSession?.beginConfiguration()
- photoSession?.removeInput(inputDevice)
- if(cameraDevice?.position == AVCaptureDevicePosition.Front)
- {
- cameraDevice = setCamera(AVCaptureDevicePosition.Back)
- }
- else
- {
- cameraDevice = setCamera(AVCaptureDevicePosition.Front)
- }
- inputDevice = AVCaptureDeviceInput.deviceInputWithDevice(cameraDevice, error: nil) as? AVCaptureDeviceInput
- print("input set to device \(cameraDevice?.description)")
- photoSession?.addInput(inputDevice)
- photoSession?.commitConfiguration()
- }
- override func viewDidAppear(animated: Bool) {
- photoSession?.startRunning()
- }
- override func viewWillDisappear(animated: Bool) {
- photoSession?.stopRunning()
- }
- @IBAction func goBack()
- {
- self.navigationController?.popViewControllerAnimated(true)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement