2017-03-22 60 views
0

我想掃描條形碼並返回結果。我正在使用ZXing。對於Zxing,當其默認覆蓋時,它會導航到一個新頁面,掃描完美。我想保持在同一頁面上,並希望子視圖啓用相機並開始掃描。任何人都可以建議如何做到這一點?如何使Zebra Xing(Zxing)海關覆蓋作爲Xamarin iOS中的子視圖

mycode的:

MobileBarcodeScanner scanner; 
    CustomOverlayView customOverlay; 
    ZXingScannerView scannerView; 
    UIActivityIndicatorView loadingView; 
    UIView loadingBg; 
    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/4)) { BackgroundColor = UIColor.Clear }; 
     scanner = new MobileBarcodeScanner(); 

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


       camView 
      } 
     }; 


     scannerView = new ZXingScannerView(); 

     camView = scannerView; 
     loadingBg = camView;// new UIView(this.View.Frame) { BackgroundColor = UIColor.Purple, AutoresizingMask = UIViewAutoresizing.FlexibleDimensions }; 
     loadingView = new UIActivityIndicatorView(UIActivityIndicatorViewStyle.Gray) 
     { 
      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; 

     scanner.UseCustomOverlay = true; 
     scanner.CustomOverlay = camView; 
     var options = new MobileBarcodeScanningOptions 
     { 
      AutoRotate = false, 
      TryHarder = true 
     }; 
     Task.Run(async() => 
     { 
      var result = await scanner.Scan(options, false); 
      HandleScanResult(result); 
     }); 


    } 

    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; 
     //originalStatusBarStyle = UIApplication.SharedApplication.StatusBarStyle; 
     var opt = new MobileBarcodeScanningOptions(); 
     opt.DelayBetweenContinuousScans = 3000; 
     ScanningOptions = opt; 

     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(); 
      } 
     }); 
    } 
+0

你想在你的ScanView中啓動攝像頭嗎? –

+0

是的,Zxing提供的掃描條形碼。 – TheDeveloper

回答

2

你必須使用ZXingScannerView並將其添加到您的視圖。你可以看到如何使用ZXingScannerView in ZXingScannerViewController

+0

你能舉一個小例子大衛嗎?我嘗試過但沒有按預期工作。 – TheDeveloper

+0

我用代碼更新了我的問題,你能看看我缺少的東西嗎? – TheDeveloper

+0

你必須開始掃描:https://github.com/Redth/ZXing.Net.Mobile/blob/master/Source/ZXing.Net.Mobile.iOS/ZXingScannerViewController.cs#L143 –