                     layer.frame = self.bounds;
                 }
             }
-            
-            self.videoPreviewLayer?.connection.videoOrientation = interfaceOrientationToVideoOrientation(UIApplication.shared.statusBarOrientation);
-        }
-        
+
+            self.videoPreviewLayer?.connection?.videoOrientation = interfaceOrientationToVideoOrientation(UIApplication.shared.statusBarOrientation);
+        }
+
         func addPreviewLayer() {
             videoPreviewLayer?.frame = self.bounds
             self.backgroundColor = UIColor.clear
             self.layer.addSublayer(videoPreviewLayer!)
         }
-        
+
         func removePreviewLayer() {
             self.videoPreviewLayer?.removeFromSuperlayer()
             self.videoPreviewLayer = nil
         }
     }
-    
+
     var cameraPreview: CameraPreview!
     var captureSession:AVCaptureSession?
     var barcodeReadCallback:String?
-    
+
     var xPoint:CGFloat = 0.0
     var yPoint:CGFloat = 0.0
     var width:CGFloat = 0.0

    var lastRead:String = ""
    var lastTimeRead: Date = Date.init()
    var captureDevice: AVCaptureDevice?
    
    override func pluginInitialize() {
        super.pluginInitialize()
        self.webView!.backgroundColor = UIColor.clear
        self.webView!.isOpaque = false
    }
    
    func startReading(_ command: CDVInvokedUrlCommand) {
//        let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
        let cameraDefault = (command.arguments[4] as AnyObject!) as? String ?? "back"
        
        if (cameraDefault == "front") {
            captureDevice = ((AVCaptureDevice.devices() as? [AVCaptureDevice])?
                .filter({ $0.hasMediaType(AVMediaTypeVideo) && $0.position == .front}).first)!
        } else {
            captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
        }

        do {

                        return
                    }
                    lastTimeRead = newReadTime
                    lastRead = metadataObj.stringValue!

                    let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: metadataObj.stringValue)
                    pluginResult!.setKeepCallbackAs(true)
                    commandDelegate!.send(pluginResult, callbackId:self.barcodeReadCallback)
