public override void ViewDidLoad ()
		{
            loadingBg = new UIView (this.View.Frame) { 
                BackgroundColor = UIColor.Black, 
                AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight 
            };
            loadingView = new UIActivityIndicatorView (UIActivityIndicatorViewStyle.WhiteLarge) {
                AutoresizingMask = UIViewAutoresizing.FlexibleMargins
            };
			loadingView.Frame = new CGRect ((this.View.Frame.Width - loadingView.Frame.Width) / 2, 
				(this.View.Frame.Height - loadingView.Frame.Height) / 2,
				loadingView.Frame.Width, 
				loadingView.Frame.Height);
			
			loadingBg.AddSubview (loadingView);
			View.AddSubview (loadingBg);
			loadingView.StartAnimating ();

			scannerView = new AVCaptureScannerView(new CGRect(0, 0, this.View.Frame.Width, this.View.Frame.Height));
			scannerView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
			scannerView.UseCustomOverlayView = this.Scanner.UseCustomOverlay;
			scannerView.CustomOverlayView = this.Scanner.CustomOverlay;
			scannerView.TopText = this.Scanner.TopText;
			scannerView.BottomText = this.Scanner.BottomText;
			scannerView.CancelButtonText = this.Scanner.CancelButtonText;
			scannerView.FlashButtonText = this.Scanner.FlashButtonText;
            scannerView.OnCancelButtonPressed += () => {
                Scanner.Cancel ();
            };

			this.View.AddSubview(scannerView);
			this.View.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
		}
Exemplo n.º 2
0
        public void ScanContinuously(MobileBarcodeScanningOptions options, bool useAVCaptureEngine, Action <Result> scanHandler)
        {
            try
            {
                Version sv = new Version(0, 0, 0);
                Version.TryParse(UIDevice.CurrentDevice.SystemVersion, out sv);

                var is7orgreater = sv.Major >= 7;
                var allRequestedFormatsSupported = true;

                if (useAVCaptureEngine)
                {
                    allRequestedFormatsSupported = AVCaptureScannerView.SupportsAllRequestedBarcodeFormats(options.PossibleFormats);
                }

                this.appController.InvokeOnMainThread(() =>
                {
                    //if (useAVCaptureEngine && is7orgreater && allRequestedFormatsSupported)
                    //{
                    //    viewController = new AVCaptureScannerViewController(options, this);
                    //    viewController.ContinuousScanning = true;
                    //}
                    //else
                    //{
                    //if (useAVCaptureEngine && !is7orgreater)
                    //    Console.WriteLine("Not iOS 7 or greater, cannot use AVCapture for barcode decoding, using ZXing instead");
                    //else if (useAVCaptureEngine && !allRequestedFormatsSupported)
                    //Console.WriteLine("Not all requested barcode formats were supported by AVCapture, using ZXing instead");

                    viewController = new CustomScannerViewController(options, new MobileBarcodeScanner())
                    {
                        ContinuousScanning = true
                    };
                    //}

                    viewController.OnScannedResult += barcodeResult =>
                    {
                        // If null, stop scanning was called
                        if (barcodeResult == null)
                        {
                            ((UIViewController)viewController).InvokeOnMainThread(() =>
                            {
                                ((UIViewController)viewController).DismissViewController(true, null);
                            });
                        }

                        scanHandler(barcodeResult);
                    };

                    appController.PresentViewController((UIViewController)viewController, true, null);
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
		public override void ViewDidLoad ()
		{
			scannerView = new AVCaptureScannerView(new RectangleF(0, 0, this.View.Frame.Width, this.View.Frame.Height));
			scannerView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
			scannerView.UseCustomOverlayView = this.Scanner.UseCustomOverlay;
			scannerView.CustomOverlayView = this.Scanner.CustomOverlay;
			scannerView.TopText = this.Scanner.TopText;
			scannerView.BottomText = this.Scanner.BottomText;
			scannerView.CancelButtonText = this.Scanner.CancelButtonText;
			scannerView.FlashButtonText = this.Scanner.FlashButtonText;

			this.View.AddSubview(scannerView);
			this.View.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
		}
Exemplo n.º 4
0
        public Task <Result> Scan(MobileBarcodeScanningOptions options, bool useAVCaptureEngine)
        {
            return(Task.Factory.StartNew(() =>
            {
                try
                {
                    scanResultResetEvent.Reset();

                    Result result = null;

                    Version sv = new Version(0, 0, 0);
                    Version.TryParse(UIDevice.CurrentDevice.SystemVersion, out sv);

                    var is7orgreater = sv.Major >= 7;
                    var allRequestedFormatsSupported = true;

                    if (useAVCaptureEngine)
                    {
                        allRequestedFormatsSupported = AVCaptureScannerView.SupportsAllRequestedBarcodeFormats(options.PossibleFormats);
                    }

                    this.appController.InvokeOnMainThread(() =>
                    {
                        //if (useAVCaptureEngine && is7orgreater && allRequestedFormatsSupported)
                        //{
                        //    viewController = new AVCaptureScannerViewController(options, this);
                        //}
                        //else
                        //{
                        //if (useAVCaptureEngine && !is7orgreater)
                        //    Console.WriteLine("Not iOS 7 or greater, cannot use AVCapture for barcode decoding, using ZXing instead");
                        //else if (useAVCaptureEngine && !allRequestedFormatsSupported)
                        //Console.WriteLine("Not all requested barcode formats were supported by AVCapture, using ZXing instead");

                        viewController = new CustomScannerViewController(options, new MobileBarcodeScanner());
                        //}

                        viewController.OnScannedResult += barcodeResult =>
                        {
                            ((UIViewController)viewController).InvokeOnMainThread(() =>
                            {
                                viewController.Cancel();

                                // Handle error situation that occurs when user manually closes scanner in the same moment that a QR code is detected
                                try
                                {
                                    ((UIViewController)viewController).DismissViewController(true, () =>
                                    {
                                        result = barcodeResult;
                                        scanResultResetEvent.Set();
                                    });
                                }
                                catch (ObjectDisposedException)
                                {
                                    // In all likelihood, iOS has decided to close the scanner at this point. But just in case it executes the
                                    // post-scan code instead, set the result so we will not get a NullReferenceException.
                                    result = barcodeResult;
                                    scanResultResetEvent.Set();
                                }
                            });
                        };

                        appController.PresentViewController((UIViewController)viewController, true, null);
                    });

                    scanResultResetEvent.WaitOne();
                    ((UIViewController)viewController).Dispose();

                    return result;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    return null;
                }
            }));
        }
Exemplo n.º 5
0
        protected override void OnElementChanged(ElementChangedEventArgs <ZXingScannerView> e)
        {
            AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;

            formsView = Element;

            if (zxingView == null)
            {
                // Process requests for autofocus
                formsView.AutoFocusRequested += (x, y) => {
                    if (zxingView != null)
                    {
                        if (x < 0 && y < 0)
                        {
                            zxingView.AutoFocus();
                        }
                        else
                        {
                            zxingView.AutoFocus(x, y);
                        }
                    }
                };

                if (formsView.Options.UseNativeScanning)
                {
                    Version sv = new Version(0, 0, 0);
                    Version.TryParse(UIDevice.CurrentDevice.SystemVersion, out sv);

                    var is7orgreater = sv.Major >= 7;
                    var allRequestedFormatsSupported = true;
                    allRequestedFormatsSupported = AVCaptureScannerView.SupportsAllRequestedBarcodeFormats(formsView.Options.PossibleFormats);

                    if (is7orgreater && allRequestedFormatsSupported)
                    {
                        Console.WriteLine("Using AVCapture for barcode decoding");
                        zxingView = new AVCaptureScannerView();
                    }
                    else
                    {
                        if (!is7orgreater)
                        {
                            Console.WriteLine("Not iOS 7 or greater, cannot use AVCapture for barcode decoding, using ZXing instead");
                        }
                        else
                        {
                            Console.WriteLine("Not all requested barcode formats were supported by AVCapture, using ZXing instead");
                        }

                        zxingView = new ZXing.Mobile.ZXingScannerView();
                    }
                }
                else
                {
                    Console.WriteLine("Using ZXing for barcode decoding");
                    zxingView = new ZXing.Mobile.ZXingScannerView();
                }


                zxingView.UseCustomOverlayView = true;
                zxingView.CustomOverlayView    = new UIView();
                zxingView.AutoresizingMask     = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;

                base.SetNativeControl(zxingView);

                if (formsView.IsScanning)
                {
                    zxingView.StartScanning(formsView.RaiseScanResult, formsView.Options);
                }

                if (!formsView.IsAnalyzing)
                {
                    zxingView.PauseAnalysis();
                }

                if (formsView.IsTorchOn)
                {
                    zxingView.Torch(formsView.IsTorchOn);
                }
            }

            base.OnElementChanged(e);
        }