0

I posted couple other questions in regards to Zxing please DONT mark it duplicate just because they are about Zxing..

So, in my Xamarin iOS app, I am using Zxing to detect the barcode. I am using https://github.com/Redth/ZXing.Net.Mobile/tree/master/Samples/iOS as example.

I am using a subview to scan for barcodes. The custom overlay n everything works fine but it is unable to detect the barcode when I'm trying to scan. Can anyone please help me out where I'm doing wrong or missing something.

CODE

public UIView camView;

        AVCaptureScannerView scannerView;
        UIActivityIndicatorView loadingView;
        UIView loadingBg;
        UIView topBg;
        UIView bottomBg;
        MobileBarcodeScanner scanner;
        public event Action<ZXing.Result> OnScannedResult;
        public MobileBarcodeScanningOptions ScanningOptions { get; set; }

public override void ViewDidLoad()
        {
            camView = new UIView(new CGRect(0, 0, this.View.Frame.Width, this.View.Frame.Height / 3)) { BackgroundColor = UIColor.Clear };
            scanner = new MobileBarcodeScanner();

            Root = new RootElement("ZXingDwatNet.Mobile") {
                new Section {


                    camView
                }
            };

            scannerView = new AVCaptureScannerView(camView.Frame);

            camView = scannerView;

            loadingBg = camView;// new UIView(this.View.Frame) { BackgroundColor = UIColor.Purple, AutoresizingMask = UIViewAutoresizing.FlexibleDimensions };
            loadingView = new UIActivityIndicatorView(UIActivityIndicatorViewStyle.White)
            {
                AutoresizingMask = UIViewAutoresizing.FlexibleMargins
            };
            loadingView.Frame = new CGRect((this.View.Frame.Width - loadingView.Frame.Width) / 4,
                (this.View.Frame.Height - loadingView.Frame.Height) / 4,
                loadingView.Frame.Width / 4,
                loadingView.Frame.Height / 4);

            loadingBg.AddSubview(loadingView);
            View.AddSubview(loadingBg);
            loadingView.StartAnimating();

            this.View.InsertSubviewBelow(scannerView, loadingView);

            this.View.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;


        }

        void HandleScanResult(ZXing.Result result)
        {
            string msg = "";

            if (result != null && !string.IsNullOrEmpty(result.Text))
                msg = "Found Barcode: " + result.Text;
            else
                msg = "Scanning Canceled!";

            this.InvokeOnMainThread(() =>
            {
                var av = new UIAlertView("Barcode Result", msg, null, "OK", null);
                av.Show();
            });
        }

        public override void ViewDidAppear(bool animated)
        {
            //scannerView.OnScannerSetupComplete += HandleOnScannerSetupComplete;
            //camView = scannerView;
            var options = new MobileBarcodeScanningOptions
            {
                AutoRotate = false,
                UseFrontCameraIfAvailable = false,
                TryHarder = true
            };
            ScanningOptions = options;

            if (UIDevice.CurrentDevice.CheckSystemVersion(7, 0))
            {
                UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.Default;
                SetNeedsStatusBarAppearanceUpdate();
            }
            else
                UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.BlackTranslucent, false);

            Console.WriteLine("Starting to scan...");

            Task.Factory.StartNew(() =>
           {
               BeginInvokeOnMainThread(() => scannerView.StartScanning(result =>
               {

                       //if (!ContinuousScanning)
                       //{
                       // Console.WriteLine("Stopping scan...");
                       // scannerView.StopScanning();
                       //}

                       var evt = this.OnScannedResult;
                   if (evt != null)
                       evt(result);

               }, this.ScanningOptions));
           });
        }

        void HandleOnScannerSetupComplete()
        {
            BeginInvokeOnMainThread(() =>
           {
               if (loadingView != null && loadingBg != null && loadingView.IsAnimating)
               {
                   loadingView.StopAnimating();

                   UIView.BeginAnimations("zoomout");

                   UIView.SetAnimationDuration(2.0f);
                   UIView.SetAnimationCurve(UIViewAnimationCurve.EaseOut);

                   loadingBg.Transform = CGAffineTransform.MakeScale(2.0f, 2.0f);
                   loadingBg.Alpha = 0.0f;

                   UIView.CommitAnimations();


                   loadingBg.RemoveFromSuperview();
               }
           });
        }
TheDeveloper
  • 1,127
  • 1
  • 18
  • 55
  • 1
    it will fire OnScannedResult when it detects a barcode, but you have not assigned a handler for that event so it does nothing. – Jason Mar 23 '17 at 21:26
  • Thanks. Works now.. – TheDeveloper Mar 24 '17 at 01:30
  • Hey Jason, posted similar question but for android, can you please advise http://stackoverflow.com/questions/43034381/how-to-make-zebra-xing-zxing-as-subview-in-xamarin-android – TheDeveloper Mar 26 '17 at 20:49

0 Answers0