<commit_before>//
// Copyright (C) 2015 Twitter, Inc. and other contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//         http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import UIKit
import MobileCoreServices
import TwitterKit
import AVFoundation

class CameraViewController: UIViewController,
UINavigationControllerDelegate, UIImagePickerControllerDelegate /*, UITextViewDelegate */ {
    
    @IBOutlet weak var cameraButton: UIButton!
    @IBOutlet weak var canvasImage: UIImageView!
    @IBOutlet weak var navbar: UINavigationItem!
    @IBOutlet weak var countdown: UILabel!
    
    let captureSession = AVCaptureSession()
    var previewLayer : AVCaptureVideoPreviewLayer?
    var stillImageOutput : AVCaptureStillImageOutput?
    var startTime = NSTimeInterval()
    var timer = NSTimer()
    var snapTime:Double = 4
    var logoView: UIImageView!
    var captureDevice : AVCaptureDevice?

    func startSnap() {
        
        let aSelector : Selector = "updateTime"
        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: aSelector, userInfo: nil, repeats: true)
        startTime = NSDate.timeIntervalSinceReferenceDate()
        
    }
    
    func updateTime() {
        var currentTime = NSDate.timeIntervalSinceReferenceDate()
        var elapsedTime = currentTime - startTime
        var seconds = snapTime - elapsedTime
        if seconds > 0 {
            elapsedTime -= NSTimeInterval(seconds)
            self.countdown.alpha = 0.6
            self.countdown.hidden = false
            self.countdown.text = "\(Int(seconds+1))"
            
        } else {
            self.countdown.hidden = true
            timer.invalidate()
            
            // wow we are ready to save some photos
            // setup still OutPut to save
            if let stillOutput = self.stillImageOutput {
                
                // we do this on another thread so we don't hang the UI
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
                    
                    // find video connection
                    var videoConnection : AVCaptureConnection?
                    for connection in stillOutput.connections {
                        // find a matching input port
                        for port in connection.inputPorts! {
                            // and matching type
                            if port.mediaType == AVMediaTypeVideo {
                                videoConnection = connection as? AVCaptureConnection
                                break
                            }
                        }
                        if videoConnection != nil {
                            break // for connection
                        }
                    }
                    
                    if videoConnection != nil {
                        // found the video connection, let's get the image
                        stillOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
                            (imageSampleBuffer:CMSampleBuffer!, _) in
                            
                            let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageSampleBuffer)
                            self.didTakePhoto(imageData)
                            
                        }
                    }
                }
            }
        }
    }
    
    func didTakePhoto(imageData: NSData) {

        println("did take photo")
        let image = UIImage(data: imageData)
        let flippedImage = UIImage(CGImage: image!.CGImage, scale: 1.0, orientation: .LeftMirrored)
        self.canvasImage.image = flippedImage
        let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
        let destinationPath = documentsPath.stringByAppendingPathComponent("photobooth.jpg")
        UIImageJPEGRepresentation(flippedImage,1.0).writeToFile(destinationPath, atomically: true)
        self.canvasImage.hidden = false
        //captureSession.stopRunning()
        self.view.bringSubviewToFront(canvasImage)
        
        //store photo
        
//        if let recognizers = self.view.gestureRecognizers {
//            for recognizer in recognizers {
//                self.view.removeGestureRecognizer(recognizer as! UIGestureRecognizer)
//            }
//        }

        // after photo, go directly to preview
        preview()
        
//        var tap = UITapGestureRecognizer(target:self, action:Selector("preview"))
//        self.view.addGestureRecognizer(tap)
        
        
    }
    
    func preview() {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
            //self.captureSession.stopRunning()
            self.canvasImage.hidden = true
        }
        self.performSegueWithIdentifier("preview", sender: self);
    }
    
    func setupCam() {
        
        captureSession.sessionPreset = AVCaptureSessionPresetHigh
        let devices = AVCaptureDevice.devices()
        // Loop through all the capture devices on this phone
        for device in devices {
            // Make sure this particular device supports video
            if (device.hasMediaType(AVMediaTypeVideo)) {
                // Finally check the position and confirm we've got the back camera
                if(device.position == AVCaptureDevicePosition.Front) {
                    captureDevice = device as? AVCaptureDevice
                    if captureDevice != nil {
                        println("Capture device found")
                        beginSession()<commit_msg>minor tweaks<commit_after>//
// Copyright (C) 2015 Twitter, Inc. and other contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//         http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import UIKit
import MobileCoreServices
import TwitterKit
import AVFoundation

class CameraViewController: UIViewController,
UINavigationControllerDelegate, UIImagePickerControllerDelegate /*, UITextViewDelegate */ {
    
    @IBOutlet weak var cameraButton: UIButton!
    @IBOutlet weak var canvasImage: UIImageView!
    @IBOutlet weak var navbar: UINavigationItem!
    @IBOutlet weak var countdown: UILabel!
    
    let captureSession = AVCaptureSession()
    var previewLayer : AVCaptureVideoPreviewLayer?
    var stillImageOutput : AVCaptureStillImageOutput?
    var startTime = NSTimeInterval()
    var timer = NSTimer()
    var snapTime:Double = 4
    var logoView: UIImageView!
    var captureDevice : AVCaptureDevice?

    func startSnap() {
        
        let aSelector : Selector = "updateTime"
        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: aSelector, userInfo: nil, repeats: true)
        startTime = NSDate.timeIntervalSinceReferenceDate()
        
    }
    
    func updateTime() {
        var currentTime = NSDate.timeIntervalSinceReferenceDate()
        var elapsedTime = currentTime - startTime
        var seconds = snapTime - elapsedTime
        if seconds > 0 {
            elapsedTime -= NSTimeInterval(seconds)
            self.countdown.alpha = 0.6
            self.countdown.hidden = false
            self.countdown.text = "\(Int(seconds+1))"
            
        } else {
            self.countdown.hidden = true
            timer.invalidate()
            
            // wow we are ready to save some photos
            // setup still OutPut to save
            if let stillOutput = self.stillImageOutput {
                
                // we do this on another thread so we don't hang the UI
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
                    
                    // find video connection
                    var videoConnection : AVCaptureConnection?
                    for connection in stillOutput.connections {
                        // find a matching input port
                        for port in connection.inputPorts! {
                            // and matching type
                            if port.mediaType == AVMediaTypeVideo {
                                videoConnection = connection as? AVCaptureConnection
                                break
                            }
                        }
                        if videoConnection != nil {
                            break // for connection
                        }
                    }
                    
                    if videoConnection != nil {
                        // found the video connection, let's get the image
                        stillOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
                            (imageSampleBuffer:CMSampleBuffer!, _) in
                            
                            let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageSampleBuffer)
                            self.didTakePhoto(imageData)
                            
                        }
                    }
                }
            }
        }
    }
    
    func didTakePhoto(imageData: NSData) {

        println("did take photo")
        let image = UIImage(data: imageData)
        let flippedImage = UIImage(CGImage: image!.CGImage, scale: 1.0, orientation: .LeftMirrored)
        self.canvasImage.image = flippedImage
        let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
        let destinationPath = documentsPath.stringByAppendingPathComponent("photobooth.jpg")
        UIImageJPEGRepresentation(flippedImage,1.0).writeToFile(destinationPath, atomically: true)
        self.canvasImage.hidden = false
        //captureSession.stopRunning()
        self.view.bringSubviewToFront(canvasImage)
        
        //store photo
        
//        if let recognizers = self.view.gestureRecognizers {
//            for recognizer in recognizers {
//                self.view.removeGestureRecognizer(recognizer as! UIGestureRecognizer)
//            }
//        }

        // after photo, go directly to preview
        preview()
        
//        var tap = UITapGestureRecognizer(target:self, action:Selector("preview"))
//        self.view.addGestureRecognizer(tap)
        
        
    }
    
    func preview() {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
            //self.captureSession.stopRunning()
            self.canvasImage.hidden = true
        }
        self.performSegueWithIdentifier("preview", sender: self);
    }
    

    

    func setupCam() {
        
        captureSession.sessionPreset = AVCaptureSessionPresetHigh
        let devices = AVCaptureDevice.devices()
        // Loop through all the capture devices on this phone
        for device in devices {
            // Make sure this particular device supports video
            if (device.hasMediaType(AVMediaTypeVideo)) {
                // Finally check the position and confirm we've got the back camera
                if(device.position == AVCaptureDevicePosition.Front) {
                    captureDevice = device as? AVCaptureDevice
                    if captureDevice != nil {
                        println("Capture device found")
                        beginSession()
{'fraction_non_alphanumeric': 0.06904412962058297, 'fraction_numerical': 0.003419638495359062, 'mean_word_length': 1.7708098353259643, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6257666', 'n_tokens_mistral': 3020, 'n_tokens_neox': 2906, 'n_words': 802}