     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)!
+                .filter({ $0.hasMediaType(AVMediaType.video) && $0.position == .front}).first)!
         } else {
-            captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
+            captureDevice = AVCaptureDevice.default(for: AVMediaType.video)
         }
 
         do {

import AVFoundation

@objc(EmbeddedBarcodeReader) class EmbeddedBarcodeReader: CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {
    
    class CameraPreview: UIView {
        var videoPreviewLayer:AVCaptureVideoPreviewLayer?
        var qrCodeFrameView:UIView?
        
        func interfaceOrientationToVideoOrientation(_ orientation : UIInterfaceOrientation) -> AVCaptureVideoOrientation {
            switch (orientation) {
            case UIInterfaceOrientation.portrait:

            self.webView!.superview!.bringSubview(toFront: self.webView)
//            self.webView!.superview!.sendSubview(toBack: cameraPreview!)
            cameraPreview.addPreviewLayer();

            // Start video capture.
            captureSession?.startRunning()

            // Move the message label and top bar to the front
            //            view.bringSubview(toFront: messageLabel)
            //            view.bringSubview(toFront: topbar)

            // Initialize QR Code Frame to highlight the QR code
            cameraPreview.qrCodeFrameView = UIView()

            cameraPreview.qrCodeFrameView?.layer.borderColor = UIColor.green.cgColor
            cameraPreview.qrCodeFrameView?.layer.borderWidth = 2
            self.webView?.addSubview(cameraPreview.qrCodeFrameView!)
            self.webView?.bringSubview(toFront: cameraPreview.qrCodeFrameView!)

        } catch {
            // If any error occurs, simply print it out and don't continue any more.
            print(error)
        }

        let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: "Camera Started")
        commandDelegate!.send(pluginResult, callbackId:command.callbackId)
    }

    func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {

        var newReadTime: Date
        var milsSinceLastRead: Double

        // Check if the metadataObjects array is not nil and it contains at least one object.
        if metadataObjects == nil || metadataObjects.count == 0 {
            cameraPreview.qrCodeFrameView?.frame = CGRect.zero
            return
        }

        // Get the metadata object.
        let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject

        if metadataObj.type == AVMetadataObject.ObjectType.qr {
            // If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds
            let barCodeObject = cameraPreview.videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
            cameraPreview.qrCodeFrameView?.frame = CGRect.init(x: self.xPoint + barCodeObject!.bounds.minX, y: self.yPoint + barCodeObject!.bounds.minY, width: barCodeObject!.bounds.width, height: barCodeObject!.bounds.height)

            if metadataObj.stringValue != nil {
                if (self.barcodeReadCallback != nil) {
                    newReadTime = Date.init()
