public void startScanner(string options)
        {
            BarcodeHelper.resultAvailable = false;

            Deployment.Current.Dispatcher.BeginInvoke(delegate()
            {
                var root   = Application.Current.RootVisual as PhoneApplicationFrame;
                mwbScanner = this;
                kallbackID = JsonHelper.Deserialize <string[]>(options)[0];
                root.Navigate(new System.Uri("/Plugins/manateeworks-barcodescanner/ScannerPage.xaml", UriKind.Relative));

                root.Navigated += new System.Windows.Navigation.NavigatedEventHandler(root_Navigated);
            });
        }
        public void startScanner(string options)
        {

            BarcodeHelper.resultAvailable = false;

            Deployment.Current.Dispatcher.BeginInvoke(delegate()
            {

                var root = Application.Current.RootVisual as PhoneApplicationFrame;
                mwbScanner = this;
                kallbackID = JsonHelper.Deserialize<string[]>(options)[0];
                root.Navigate(new System.Uri("/Plugins/com.manateeworks.barcodescanner/ScannerPage.xaml", UriKind.Relative));

                root.Navigated += new System.Windows.Navigation.NavigatedEventHandler(root_Navigated);
            });


        }
        public void startScannerView(string options)
        {
            Deployment.Current.Dispatcher.BeginInvoke(delegate()
            {
                if (canvas == null)
                {
                    string[] paramsList = JsonHelper.Deserialize <string[]>(options);
                    mwbScanner          = this;
                    kallbackID          = paramsList[4];

                    bool firstTimePageLoad = false;
                    if (currentPage == null)
                    {
                        firstTimePageLoad = true;
                        currentPage       = (((PhoneApplicationFrame)Application.Current.RootVisual).Content as PhoneApplicationPage);
                    }

                    var screenWidth  = (currentPage.Orientation == PageOrientation.Portrait || currentPage.Orientation == PageOrientation.PortraitUp || currentPage.Orientation == PageOrientation.PortraitDown) ? Application.Current.Host.Content.ActualWidth : Application.Current.Host.Content.ActualHeight;
                    var screenHeight = (currentPage.Orientation == PageOrientation.Landscape || currentPage.Orientation == PageOrientation.LandscapeLeft || currentPage.Orientation == PageOrientation.LandscapeRight) ? Application.Current.Host.Content.ActualWidth : Application.Current.Host.Content.ActualHeight;


                    var pX      = Convert.ToInt32(paramsList[0]);
                    var pY      = Convert.ToInt32(paramsList[1]);
                    var pWidth  = Convert.ToInt32(paramsList[2]);
                    var pHeight = Convert.ToInt32(paramsList[3]);

                    var x  = (float)pX / 100 * screenWidth;
                    var y  = (float)pY / 100 * screenHeight;
                    width  = (float)pWidth / 100 * screenWidth;
                    height = (float)pHeight / 100 * screenHeight;

                    videoBrush = new System.Windows.Media.VideoBrush();

                    canvas = new Canvas()
                    {
                        Background          = videoBrush,
                        VerticalAlignment   = VerticalAlignment.Top,
                        HorizontalAlignment = HorizontalAlignment.Left
                    };
                    float heightTmp = (float)height;
                    float widthTmp  = (float)width;
                    heightClip      = 0;
                    widthClip       = 0;

                    float AR = (float)screenHeight / (float)screenWidth;
                    if (width * AR >= height)
                    {
                        heightTmp  = (int)(width * AR);
                        heightClip = (float)(heightTmp - height) / 2;
                    }
                    else
                    {
                        widthTmp  = (int)(height / AR);
                        widthClip = (float)(widthTmp - width) / 2;
                    }
                    canvas.Width  = widthTmp;
                    canvas.Height = heightTmp;

                    RectangleGeometry rg = new RectangleGeometry();
                    rg.Rect       = new System.Windows.Rect(widthClip, heightClip, width, height);
                    canvas.Clip   = rg;
                    canvas.Margin = new Thickness(x - widthClip, y - heightClip, 0, 0);

                    if (pX == 0 && pY == 0 && pWidth == 1 && pHeight == 1)
                    {
                        canvas.Visibility = Visibility.Collapsed;
                    }
                    (currentPage.FindName("LayoutRoot") as Grid).Children.Add(canvas);

                    scannerPage            = new ScannerPage();
                    scannerPage.videoBrush = videoBrush;
                    ScannerPage.isPage     = false;
                    scannerPage.InitializeCamera(CameraSensorLocation.Back);
                    scannerPage.fixOrientation(currentPage.Orientation);
                    setAutoRect();
                    if ((int)ScannerPage.param_OverlayMode == 1)
                    {
                        MWOverlay.addOverlay(canvas);
                    }
                    else if ((int)ScannerPage.param_OverlayMode == 2)
                    {
                        imgOverlay = new Image()
                        {
                            Stretch = Stretch.Fill,
                            Width   = width,
                            Height  = height,
                            Margin  = new Thickness(widthClip, heightClip, 0, 0)
                        };

                        BitmapImage BitImg = new BitmapImage(new Uri("/Plugins/manateeworks-barcodescanner/overlay_mw.png", UriKind.Relative));
                        imgOverlay.Source  = BitImg;
                        canvas.Children.Add(imgOverlay);
                    }
                    if (ScannerPage.param_EnableFlash)
                    {
                        flashButton = new Button()
                        {
                            Width           = 70,
                            Height          = 70,
                            BorderThickness = new Thickness(0, 0, 0, 0),
                            Margin          = new Thickness(x + 5, y + 5, 0, 0),
                            Background      = new ImageBrush()
                            {
                                ImageSource = new BitmapImage(new Uri("/Plugins/manateeworks-barcodescanner/flashbuttonoff.png", UriKind.Relative))
                            },
                            HorizontalAlignment = HorizontalAlignment.Left,
                            VerticalAlignment   = VerticalAlignment.Top
                        };
                        flashButton.Click += delegate
                        {
                            scannerPage.flashButton_Click(null, null);
                        };

                        (currentPage.FindName("LayoutRoot") as Grid).Children.Add(flashButton);
                    }

                    if (firstTimePageLoad)
                    {
                        currentPage.NavigationService.Navigating += delegate
                        {
                            stopScanner("");
                        };


                        currentPage.OrientationChanged += delegate
                        {
                            Deployment.Current.Dispatcher.BeginInvoke(delegate()
                            {
                                if (canvas != null)
                                {
                                    if ((int)ScannerPage.param_OverlayMode == 1)
                                    {
                                        MWOverlay.removeOverlay();
                                    }


                                    screenWidth  = (currentPage.Orientation == PageOrientation.Portrait || currentPage.Orientation == PageOrientation.PortraitUp || currentPage.Orientation == PageOrientation.PortraitDown) ? Application.Current.Host.Content.ActualWidth : Application.Current.Host.Content.ActualHeight;
                                    screenHeight = (currentPage.Orientation == PageOrientation.Landscape || currentPage.Orientation == PageOrientation.LandscapeLeft || currentPage.Orientation == PageOrientation.LandscapeRight) ? Application.Current.Host.Content.ActualWidth : Application.Current.Host.Content.ActualHeight;
                                    x            = (float)pX / 100 * screenWidth;
                                    y            = (float)pY / 100 * screenHeight;
                                    width        = (float)pWidth / 100 * screenWidth;
                                    height       = (float)pHeight / 100 * screenHeight;
                                    heightTmp    = (float)height;
                                    widthTmp     = (float)width;
                                    heightClip   = 0;
                                    widthClip    = 0;

                                    AR = (float)screenHeight / (float)screenWidth;
                                    if (width * AR >= height)
                                    {
                                        heightTmp  = (float)(width * AR);
                                        heightClip = (float)(heightTmp - height) / 2;
                                    }
                                    else
                                    {
                                        widthTmp  = (float)(height / AR);
                                        widthClip = (float)(widthTmp - width) / 2;
                                    }

                                    canvas.Width  = widthTmp;
                                    canvas.Height = heightTmp;
                                    rg            = new RectangleGeometry();
                                    rg.Rect       = new System.Windows.Rect(widthClip, heightClip, width, height);
                                    canvas.Clip   = rg;
                                    canvas.Margin = new Thickness(x - widthClip, y - heightClip, 0, 0);

                                    if (flashButton != null)
                                    {
                                        flashButton.Margin = new Thickness(x + 5, y + 5, 0, 0);
                                    }
                                    scannerPage.fixOrientation(currentPage.Orientation);
                                    setAutoRect();
                                    if ((int)ScannerPage.param_OverlayMode == 1)
                                    {
                                        Observable
                                        .Timer(TimeSpan.FromMilliseconds(100))
                                        .SubscribeOnDispatcher()
                                        .Subscribe(_ =>
                                        {
                                            Deployment.Current.Dispatcher.BeginInvoke(delegate()
                                            {
                                                MWOverlay.addOverlay(canvas);
                                            });
                                        });
                                    }
                                    else if ((int)ScannerPage.param_OverlayMode == 2)
                                    {
                                        imgOverlay.Width  = width;
                                        imgOverlay.Height = height;
                                        imgOverlay.Margin = new Thickness(widthClip, heightClip, 0, 0);
                                    }
                                }
                            });
                        };
                    }
                }
                else
                {
                    setAutoRect();
                }
            });
        }
        public void startScannerView(string options)
        {

            Deployment.Current.Dispatcher.BeginInvoke(delegate()
            {
                if (canvas == null)
                {
                    string[] paramsList = JsonHelper.Deserialize<string[]>(options);
                    mwbScanner = this;
                    kallbackID = paramsList[4];

                    bool firstTimePageLoad = false;
                    if (currentPage == null)
                    {
                        firstTimePageLoad = true;
                        currentPage = (((PhoneApplicationFrame)Application.Current.RootVisual).Content as PhoneApplicationPage);
                    }

                    var screenWidth = (currentPage.Orientation == PageOrientation.Portrait || currentPage.Orientation == PageOrientation.PortraitUp || currentPage.Orientation == PageOrientation.PortraitDown) ? Application.Current.Host.Content.ActualWidth : Application.Current.Host.Content.ActualHeight;
                    var screenHeight = (currentPage.Orientation == PageOrientation.Landscape || currentPage.Orientation == PageOrientation.LandscapeLeft || currentPage.Orientation == PageOrientation.LandscapeRight) ? Application.Current.Host.Content.ActualWidth : Application.Current.Host.Content.ActualHeight;


                    var pX = Convert.ToInt32(paramsList[0]);
                    var pY = Convert.ToInt32(paramsList[1]);
                    var pWidth = Convert.ToInt32(paramsList[2]);
                    var pHeight = Convert.ToInt32(paramsList[3]);

                    var x = (float)pX / 100 * screenWidth;
                    var y = (float)pY / 100 * screenHeight;
                    width = (float)pWidth / 100 * screenWidth;
                    height = (float)pHeight / 100 * screenHeight;

                    videoBrush = new System.Windows.Media.VideoBrush();

                    canvas = new Canvas()
                    {
                        Background = videoBrush,
                        VerticalAlignment = VerticalAlignment.Top,
                        HorizontalAlignment = HorizontalAlignment.Left
                    };
                    float heightTmp = (float)height;
                    float widthTmp = (float)width;
                    heightClip = 0;
                    widthClip = 0;

                    float AR = (float)screenHeight / (float)screenWidth;
                    if (width * AR >= height)
                    {
                        heightTmp = (int)(width * AR);
                        heightClip = (float)(heightTmp - height) / 2;
                    }
                    else
                    {
                        widthTmp = (int)(height / AR);
                        widthClip = (float)(widthTmp - width) / 2;
                    }
                    canvas.Width = widthTmp;
                    canvas.Height = heightTmp;

                    RectangleGeometry rg = new RectangleGeometry();
                    rg.Rect = new System.Windows.Rect(widthClip, heightClip, width, height);
                    canvas.Clip = rg;
                    canvas.Margin = new Thickness(x - widthClip, y - heightClip, 0, 0);

                    (currentPage.FindName("LayoutRoot") as Grid).Children.Add(canvas);

                    scannerPage = new ScannerPage();
                    scannerPage.videoBrush = videoBrush;
                    ScannerPage.isPage = false;
                    scannerPage.InitializeCamera(CameraSensorLocation.Back);
                    scannerPage.fixOrientation(currentPage.Orientation);
                    setAutoRect();
                    if ((int)ScannerPage.param_OverlayMode == 1)
                    {
                        MWOverlay.addOverlay(canvas);
                    }
                    else if((int)ScannerPage.param_OverlayMode == 2)
                    {
                        imgOverlay = new Image() { Stretch = Stretch.Fill,
                        Width = width,
                        Height = height,
                        Margin = new Thickness(widthClip,heightClip,0,0)
                        };
                        
                        BitmapImage BitImg = new BitmapImage(new Uri("/Plugins/com.manateeworks.barcodescanner/overlay_mw.png", UriKind.Relative));
                        imgOverlay.Source = BitImg;
                        canvas.Children.Add(imgOverlay);
                    }
                    if (firstTimePageLoad)
                    {

                        currentPage.NavigationService.Navigating += delegate
                        {
                            stopScanner("");
                        };

                        currentPage.OrientationChanged += delegate
                        {
                            Deployment.Current.Dispatcher.BeginInvoke(delegate()
                            {
                                if (canvas != null)
                                {
                                    if ((int)ScannerPage.param_OverlayMode == 1)
                                    {
                                        MWOverlay.removeOverlay();
                                    } 
                                   

                                    screenWidth = (currentPage.Orientation == PageOrientation.Portrait || currentPage.Orientation == PageOrientation.PortraitUp || currentPage.Orientation == PageOrientation.PortraitDown) ? Application.Current.Host.Content.ActualWidth : Application.Current.Host.Content.ActualHeight;
                                    screenHeight = (currentPage.Orientation == PageOrientation.Landscape || currentPage.Orientation == PageOrientation.LandscapeLeft || currentPage.Orientation == PageOrientation.LandscapeRight) ? Application.Current.Host.Content.ActualWidth : Application.Current.Host.Content.ActualHeight;
                                    x = (float)pX / 100 * screenWidth;
                                    y = (float)pY / 100 * screenHeight;
                                    width = (float)pWidth / 100 * screenWidth;
                                    height = (float)pHeight / 100 * screenHeight;
                                    heightTmp = (float)height;
                                    widthTmp = (float)width;
                                    heightClip = 0;
                                    widthClip = 0;

                                    AR = (float)screenHeight / (float)screenWidth;
                                    if (width * AR >= height)
                                    {
                                        heightTmp = (float)(width * AR);
                                        heightClip = (float)(heightTmp - height) / 2;
                                    }
                                    else
                                    {
                                        widthTmp = (float)(height / AR);
                                        widthClip = (float)(widthTmp - width) / 2;
                                    }

                                    canvas.Width = widthTmp;
                                    canvas.Height = heightTmp;
                                    rg = new RectangleGeometry();
                                    rg.Rect = new System.Windows.Rect(widthClip, heightClip, width, height);
                                    canvas.Clip = rg;
                                    canvas.Margin = new Thickness(x - widthClip, y - heightClip, 0, 0);


                                    scannerPage.fixOrientation(currentPage.Orientation);
                                    setAutoRect();
                                    if ((int)ScannerPage.param_OverlayMode == 1) {
                                        Observable
                                         .Timer(TimeSpan.FromMilliseconds(100))
                                         .SubscribeOnDispatcher()
                                         .Subscribe(_ =>
                                         {
                                             Deployment.Current.Dispatcher.BeginInvoke(delegate()
                                             {
                                                 MWOverlay.addOverlay(canvas);
                                             });
                                         });
                                    }
                                    else if ((int)ScannerPage.param_OverlayMode == 2)
                                    {
                                         imgOverlay.Width = width;
                                         imgOverlay.Height = height;
                                         imgOverlay.Margin = new Thickness(widthClip, heightClip, 0, 0);
                                    }
                                    
                                }
                                
                            });
                        };
                    }
                    
                }
                else
                {
                    setAutoRect();
                }

            });



        }