                 try device.lockForConfiguration();
 
                 if(flashState == 0){
-                    if(backCameraDevice!.isFlashModeSupported(AVCaptureDevice.FlashMode.off)){
-                        device.flashMode = AVCaptureDevice.FlashMode.off;
+                    if(backCameraDevice!.isFlashModeSupported(AVCaptureFlashMode.off)){
+                        device.flashMode = AVCaptureFlashMode.off;
                         let image = UIImage(named: "camera_flash_off.png") as UIImage!
                         flashUIButton.setImage(image, for: UIControlState())
                     }
-                    if(backCameraDevice!.isTorchModeSupported(AVCaptureDevice.TorchMode.off)){
-                        device.torchMode = AVCaptureDevice.TorchMode.off;
+                    if(backCameraDevice!.isTorchModeSupported(AVCaptureTorchMode.off)){
+                        device.torchMode = AVCaptureTorchMode.off;
                     }
                     flashState = 1;
                 }
 
                 else if(flashState == 1){
-                    if(backCameraDevice!.isFlashModeSupported(AVCaptureDevice.FlashMode.auto)){
-                        device.flashMode = AVCaptureDevice.FlashMode.auto;
+                    if(backCameraDevice!.isFlashModeSupported(AVCaptureFlashMode.auto)){
+                        device.flashMode = AVCaptureFlashMode.auto;
                         let image = UIImage(named: "camera_flash_auto.png") as UIImage!
                         flashUIButton.setImage(image, for: UIControlState())
                     }
-                    if(backCameraDevice!.isTorchModeSupported(AVCaptureDevice.TorchMode.off)){
-                        device.torchMode = AVCaptureDevice.TorchMode.off;
+                    if(backCameraDevice!.isTorchModeSupported(AVCaptureTorchMode.off)){
+                        device.torchMode = AVCaptureTorchMode.off;
                     }
                     flashState = 2
                 }
                 else if(flashState == 2){
-                    if(backCameraDevice!.isFlashModeSupported(AVCaptureDevice.FlashMode.off)){
-                        device.flashMode = AVCaptureDevice.FlashMode.off;
+                    if(backCameraDevice!.isFlashModeSupported(AVCaptureFlashMode.off)){
+                        device.flashMode = AVCaptureFlashMode.off;
                         let image = UIImage(named: "camera_flash_on.png") as UIImage!
                         flashUIButton.setImage(image, for: UIControlState())
                     }
-                    if(backCameraDevice!.isTorchModeSupported(AVCaptureDevice.TorchMode.on)){
-                        device.torchMode = AVCaptureDevice.TorchMode.on;
+                    if(backCameraDevice!.isTorchModeSupported(AVCaptureTorchMode.on)){
+                        device.torchMode = AVCaptureTorchMode.on;
                     }
 
                     flashState = 0;


        let possibleCameraInput: AnyObject?
        do {
            possibleCameraInput = try AVCaptureDeviceInput(device: backCameraDevice!)
        } catch let error1 as NSError {
            error = error1
            possibleCameraInput = nil
        }
        let backCameraInput = possibleCameraInput as? AVCaptureDeviceInput
        if captureSession.canAddInput(backCameraInput!) {
            captureSession.addInput(backCameraInput!)
        }

        stillImageOutput = AVCaptureStillImageOutput()
        if captureSession.canAddOutput(stillImageOutput!) {
            stillImageOutput?.isHighResolutionStillImageOutputEnabled = true;
            stillImageOutput?.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG];
            captureSession.addOutput(stillImageOutput!)
        }
        captureSession.sessionPreset = AVCaptureSession.Preset.photo
        setFlashModeToAuto(backCameraDevice!)
        beginSession()
    }


        previewLayer?.frame = CGRect(x: 0, y: 0, width: width,height: height)
        previewLayer?.bounds = CGRect(x: 0, y: 0,width: width,height: height)
        previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill;
        
        previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation(rawValue: orientation.rawValue)!;
        camPreview.layer.addSublayer(previewLayer!)

        captureSession.startRunning()
