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(); } }); }
public void stopScanner(string options) { if (canvas != null) { Deployment.Current.Dispatcher.BeginInvoke(delegate() { BarcodeScannerPage.ScannerPage.cameraDevice.Dispose(); if ((int)ScannerPage.param_OverlayMode == 1) { MWOverlay.removeOverlay(); } else if ((int)ScannerPage.param_OverlayMode == 1) { canvas.Children.Remove(imgOverlay); } (currentPage.FindName("LayoutRoot") as Grid).Children.Remove(canvas); scannerPage.stopCamera(); scannerPage = null; canvas = null; videoBrush = null; }); } }