Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.44 KB | None | 0 0
  1. //
  2. // PhotoController.swift
  3. // WrapApp
  4. //
  5. // Created by Bryan Green on 9/1/15.
  6. // green.c.bryan@gmail.com
  7. // Copyright (c) 2015 Bryan Green. All rights reserved.
  8. //
  9.  
  10. import UIKit
  11. import AVFoundation
  12. import ImageIO
  13. import QuartzCore
  14.  
  15.  
  16. class PhotoController : UIViewController,
  17. AVCaptureVideoDataOutputSampleBufferDelegate {
  18.  
  19. @IBOutlet var videoPreview : UIView!
  20. @IBOutlet var takePhotoButton : UIButton!
  21. @IBOutlet var switchDeviceButton: UIButton!
  22. @IBOutlet var continueButton: UIButton!
  23. @IBOutlet var photoPreview : UIImageView!
  24. @IBOutlet var photoStage : UILabel!
  25. @IBOutlet var skipButton: UIButton!
  26.  
  27.  
  28. var photoSession :AVCaptureSession?
  29. var cameraDevice : AVCaptureDevice?
  30. var coreAnimationLayer : CALayer?
  31. var videoPreviewLayer : AVCaptureVideoPreviewLayer?
  32. var queue : dispatch_queue_t?
  33. var photo : AVCaptureStillImageOutput?
  34. var inputDevice : AVCaptureDeviceInput?
  35.  
  36.  
  37.  
  38. override func viewDidLoad() {
  39. super.viewDidLoad()
  40.  
  41.  
  42. //let tap = UILongPressGestureRecognizer(target:self,action:"snapPicture")
  43. //self.view.addGestureRecognizer(tap)
  44.  
  45. initCameraSession()
  46.  
  47. initPhotoPreview()
  48.  
  49. }
  50.  
  51.  
  52.  
  53. @IBAction func skipPhotos()
  54. {
  55. print("skipPhotos")
  56. }
  57.  
  58.  
  59.  
  60. //*********Video Capture**********//
  61.  
  62. func initPhotoPreview()
  63. {
  64. self.photoPreview.hidden = true
  65. self.photoPreview.layer.borderColor = UIColor(red: 153.0/255.0, green: 204.0/255.0, blue: 51.0/255.0, alpha: 1.0).CGColor
  66. self.photoPreview.layer.borderWidth = 2.0
  67.  
  68. self.photoPreview.contentMode = UIViewContentMode.ScaleAspectFill
  69. self.photoPreview.clipsToBounds = true
  70.  
  71. }
  72.  
  73.  
  74. func initCameraSession()
  75. {
  76. //create a new photo session
  77. photoSession = AVCaptureSession()
  78.  
  79. //setup the resolution for photo presets
  80. photoSession?.sessionPreset = AVCaptureSessionPresetPhoto
  81.  
  82. //set default video device to front
  83. cameraDevice = setCamera(AVCaptureDevicePosition.Front)
  84.  
  85. /*set focus
  86. if((cameraDevice?.isFocusModeSupported(AVCaptureFocusMode.ContinuousAutoFocus)) != nil)
  87. {
  88. cameraDevice?.focusMode = AVCaptureFocusMode.ContinuousAutoFocus
  89. }
  90.  
  91.  
  92. //set flash to auto
  93. cameraDevice?.torchMode = AVCaptureTorchMode.Auto
  94.  
  95. */
  96.  
  97. //try to set the input device for the session
  98. inputDevice = AVCaptureDeviceInput.deviceInputWithDevice(cameraDevice, error: nil) as? AVCaptureDeviceInput
  99.  
  100. print("input set to device \(cameraDevice?.description)")
  101. photoSession?.addInput(inputDevice)
  102.  
  103.  
  104.  
  105.  
  106. /* Video Output
  107.  
  108. if
  109. let outputData = AVCaptureVideoDataOutput() as? AVCaptureVideoDataOutput
  110. {
  111.  
  112. photoSession?.addOutput(outputData)
  113. outputData.videoSettings = [kCVPixelBufferPixelFormatTypeKey:kCVPixelFormatType_32BGRA]
  114.  
  115. queue = dispatch_queue_create("PhotoQueue", nil)
  116. outputData.setSampleBufferDelegate(self, queue: queue)
  117.  
  118. }
  119.  
  120. */
  121.  
  122. //setup the preview layers
  123. videoPreviewLayer = AVCaptureVideoPreviewLayer(session: photoSession)
  124.  
  125. coreAnimationLayer = videoPreview.layer
  126.  
  127. videoPreviewLayer?.frame = videoPreview.bounds
  128. videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
  129. coreAnimationLayer?.addSublayer(videoPreviewLayer)
  130.  
  131. //ask for the camera
  132. requestAccessForMedia()
  133.  
  134.  
  135. //setup still image output
  136. photo = AVCaptureStillImageOutput()
  137. photo?.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG ]
  138. photoSession?.addOutput(photo)
  139.  
  140. //start the session
  141. photoSession?.startRunning()
  142.  
  143. print("photo session started: \(photoSession?.self.running)")
  144.  
  145. }
  146.  
  147.  
  148.  
  149. func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
  150.  
  151. //print("captureOutput:didOutputSampleBufferFromConnection")
  152. }
  153.  
  154. //get the first video device with the selected position
  155. func setCamera(devicePosition: AVCaptureDevicePosition) -> AVCaptureDevice?
  156. {
  157.  
  158. if
  159. let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
  160. {
  161. for device in devices
  162. {
  163. let device = device as! AVCaptureDevice
  164.  
  165. if(device.position == devicePosition){
  166. return device;
  167. }
  168. }
  169. }
  170.  
  171. return nil;
  172. }
  173.  
  174.  
  175. /*
  176. @IBAction func retakePhoto() {
  177.  
  178. print("retakePhoto called...")
  179.  
  180. self.photoPreview.hidden = true
  181. self.photoPreview.image = nil
  182.  
  183. /* NATIVE photo chooser
  184.  
  185. //Swift 1.2
  186. var picker : UIImagePickerController = UIImagePickerController()
  187. picker.delegate = self;
  188. picker.allowsEditing = true;
  189. picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
  190.  
  191. self.presentViewController(picker, animated: true, completion: nil);
  192.  
  193.  
  194. //Objective-C
  195. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  196. picker.delegate = self;
  197. picker.allowsEditing = YES;
  198. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  199.  
  200. [self presentViewController:picker animated:YES completion:NULL];
  201.  
  202. */
  203. }
  204. */
  205.  
  206.  
  207. func requestAccessForMedia()
  208. {
  209. AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (result:Bool)->Void in
  210.  
  211. print("access granted for camera: \(result)")
  212.  
  213. })
  214. }
  215.  
  216. func videoConnection() -> AVCaptureConnection
  217. {
  218.  
  219. var captureConnection : AVCaptureConnection?
  220.  
  221. if
  222. let connections = photo?.connections
  223. {
  224. for connection in connections
  225. {
  226. for port in connection.inputPorts as! [AVCaptureInputPort]
  227. {
  228. if(port.mediaType == AVMediaTypeVideo)
  229. {
  230. captureConnection = connection as? AVCaptureConnection
  231. break;
  232. }
  233.  
  234. }
  235. if(captureConnection != nil)
  236. {
  237. break;
  238. }
  239. }
  240. }
  241. else
  242. {
  243. print("videoConnection: unable to get current video connection")
  244. }
  245.  
  246. return captureConnection!
  247.  
  248. }
  249.  
  250.  
  251. @IBAction func savePhoto()
  252. {
  253.  
  254. if let image : UIImage = self.photoPreview.image{
  255. //write the photo to the album
  256. UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
  257.  
  258. }
  259.  
  260. }
  261.  
  262.  
  263. func snapPicture()
  264. {
  265. print("snapPicture:")
  266.  
  267. let captureConnection = videoConnection()
  268.  
  269. //set orientation and mirroring
  270. if(captureConnection.supportsVideoMirroring){
  271. captureConnection.videoMirrored = true
  272. }
  273.  
  274. if(captureConnection.supportsVideoOrientation){
  275. captureConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
  276. }
  277.  
  278. print("requesting a capture from: \(captureConnection)")
  279.  
  280. photo?.captureStillImageAsynchronouslyFromConnection(captureConnection, completionHandler: {(imageSampleBuffer,error)->Void in
  281.  
  282. if let exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, nil)
  283. {
  284. print("attachnents: \(exifAttachments)")
  285. }
  286.  
  287. let imageData : NSData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageSampleBuffer)
  288. let image : UIImage = UIImage(data: imageData)!
  289.  
  290.  
  291. //set the photo preview
  292. self.photoPreview.image = image
  293. self.photoPreview.setNeedsDisplay()
  294. self.photoPreview.hidden = false
  295.  
  296. /*
  297. self.photoPreview.frame = CGRectMake(self.photoPreview.frame.origin.x,
  298. self.photoPreview.frame.origin.y,
  299. image.size.width,
  300. image.size.height)
  301. */
  302.  
  303. })
  304.  
  305. /* NATIVE photo dialog
  306.  
  307. //Swift 1.2
  308. var picker : UIImagePickerController = UIImagePickerController()
  309. picker.delegate = self;
  310. picker.allowsEditing = true;
  311. picker.sourceType = UIImagePickerControllerSourceType.Camera;
  312.  
  313. self.presentViewController(picker, animated: true, completion: nil);
  314.  
  315.  
  316. //Objective C
  317. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  318. picker.delegate = self;
  319. picker.allowsEditing = YES;
  320. picker.sourceType = UIImagePickerControllerSourceTypeCamera;
  321.  
  322. [self presentViewController:picker animated:YES completion:NULL];*/
  323.  
  324. }
  325.  
  326. @IBAction func takePhoto() {
  327. snapPicture()
  328. }
  329.  
  330. @IBAction func switchDevice(){
  331. print("switchDevice:")
  332.  
  333. photoSession?.beginConfiguration()
  334.  
  335. photoSession?.removeInput(inputDevice)
  336.  
  337. if(cameraDevice?.position == AVCaptureDevicePosition.Front)
  338. {
  339. cameraDevice = setCamera(AVCaptureDevicePosition.Back)
  340. }
  341. else
  342. {
  343. cameraDevice = setCamera(AVCaptureDevicePosition.Front)
  344. }
  345.  
  346. inputDevice = AVCaptureDeviceInput.deviceInputWithDevice(cameraDevice, error: nil) as? AVCaptureDeviceInput
  347. print("input set to device \(cameraDevice?.description)")
  348.  
  349. photoSession?.addInput(inputDevice)
  350. photoSession?.commitConfiguration()
  351.  
  352.  
  353. }
  354.  
  355. override func viewDidAppear(animated: Bool) {
  356. photoSession?.startRunning()
  357. }
  358.  
  359. override func viewWillDisappear(animated: Bool) {
  360. photoSession?.stopRunning()
  361. }
  362.  
  363. @IBAction func goBack()
  364. {
  365. self.navigationController?.popViewControllerAnimated(true)
  366. }
  367.  
  368.  
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement