예제 #1
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);
            }
        }
예제 #2
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;
                }
            }));
        }
예제 #3
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);
        }