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;
        }
        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 ZXing.Mobile.ZXingScannerViewController(options, this);
                        viewController.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);
            }
        }
Beispiel #3
0
        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;
        }
        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 ZXing.Mobile.ZXingScannerViewController(options, this);
                        }

                        viewController.OnScannedResult += barcodeResult => {
                            ((UIViewController)viewController).InvokeOnMainThread(() => {
                                viewController.Cancel();
                                ((UIViewController)viewController).DismissViewController(true, () => {
                                    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;
                }
            }));
        }
        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 ZXing.Mobile.ZXingScannerViewController(options, this);
                        }

                        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;
                }
            }));
        }