Xamarin.IOS ZXing 自定义叠加层上没有相机视图

No camera view on ZXing custom overlay with Xamarin.IOS

我在我的 Xamarin.IOS 应用程序上使用 Zxing,我构建了一个自定义叠加层,它可以正确显示,但根本没有摄像头视图 "when i move the phone randomly on front of QR Code it scans it",但是新的自定义叠加层上没有摄像头视图,所以用户可以看到他的相机有什么。

这是自定义叠加层class

public class CustomOverlayView : ZXing.Mobile.ZXingScannerView
{
    public UIButton ButtonTorch;
    public UIButton ButtonCancel;
    nfloat TopMargin = 200;
    nfloat SideMargins = 50;
    public CustomOverlayView()
    {
        ButtonCancel = UIButton.FromType(UIButtonType.RoundedRect);
        ButtonCancel.Frame = new CGRect(0, this.Frame.Height - 60, this.Frame.Width / 2 - 100, 100);
        ButtonCancel.AutoresizingMask = UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleRightMargin;
        ButtonCancel.SetTitle("Cancel", UIControlState.Normal);
        this.AddSubview(ButtonCancel);


    }
    public void HandleScan(ZXing.Result _res)
    {
        //return _res;
    }
    public override void Draw(CGRect rect)
    {
        this.StartScanning(HandleScan);
        TopMargin = this.Frame.Height / 3;
        SideMargins = this.Frame.Width / 6;
        using (CGContext g = UIGraphics.GetCurrentContext())
        {

            //set up drawing attributes
            g.SetLineWidth(0);
            UIColor.DarkGray.SetFill();
            UIColor.Black.SetStroke();

            //Create top Rect
            var path = new CGPath();

            path.AddLines(new CGPoint[]{
                new CGPoint (0, 0),
                new CGPoint (this.Frame.Width, 0),
                new CGPoint (this.Frame.Width, TopMargin),
                new CGPoint (0, TopMargin)});

            path.CloseSubpath();

            g.AddPath(path);
            g.DrawPath(CGPathDrawingMode.FillStroke);

            //Create bottom Rect
            path = new CGPath();

            path.AddLines(new CGPoint[]{
                new CGPoint (0, this.Frame.Height),
                new CGPoint (this.Frame.Width, this.Frame.Height),
                new CGPoint (this.Frame.Width, this.Frame.Height-TopMargin),
                new CGPoint (0, this.Frame.Height-TopMargin)});

            path.CloseSubpath();

            g.AddPath(path);
            g.DrawPath(CGPathDrawingMode.FillStroke);


            //Create left rect
            path = new CGPath();

            path.AddLines(new CGPoint[]{
                new CGPoint (0, TopMargin),
                new CGPoint (SideMargins, TopMargin),
                new CGPoint (SideMargins, this.Frame.Height-TopMargin),
                new CGPoint (0, this.Frame.Height-TopMargin)});

            path.CloseSubpath();

            //add geometry to graphics context and draw it
            g.AddPath(path);
            g.DrawPath(CGPathDrawingMode.FillStroke);

            //Create right rect
            path = new CGPath();

            path.AddLines(new CGPoint[]{
                new CGPoint (this.Frame.Width-SideMargins, TopMargin),
                new CGPoint (this.Frame.Width, TopMargin),
                new CGPoint (this.Frame.Width, this.Frame.Height-TopMargin),
                new CGPoint (this.Frame.Width-SideMargins, this.Frame.Height-TopMargin)});

            path.CloseSubpath();

            //add geometry to graphics context and draw it
            g.AddPath(path);
            g.DrawPath(CGPathDrawingMode.FillStroke);
        }

    }
    public override void LayoutSubviews()
    {
        ButtonCancel.Frame = new CGRect(this.Frame.Width/2-75, this.Frame.Height - TopMargin +40, 150, 40);
        ButtonCancel.SetTitleColor(UIColor.White, UIControlState.Normal);
        ButtonCancel.Layer.BorderWidth = 1;
        ButtonCancel.Layer.BorderColor = new CGColor(255, 255, 255);
    }

}

我在这里调用扫描仪启动

scanner = new ZXing.Mobile.MobileBarcodeScanner();
        scanner.UseCustomOverlay = true;
        scanner.CustomOverlay = customOverlay;
        customOverlay.ButtonCancel.TouchUpInside += (sender, e) =>
        {
            scanner.Cancel();
        };
        //scanner.ScanContinuously(StartScanner_);
        result = await scanner.Scan();

如果你只想运行你的应用程序在iOS系统上,我建议你使用AVFoundation框架来实现QRCode扫描器。

因为:

  1. 可以肯定的是,AVFoundation 在 iOS;
  2. 上的性能比 ZXing 更好
  3. 如果要自定义UI,使用AVFoundation更方便;
  4. AVFoundation是Apple提供的,我认为没有任何第三方框架能比Apple的更稳定

在我的 GitHub 上有一个关于如何使用 AVFoundation 实现它的示例,您也可以将它用作一个库。

Link : QRCode scanner sample for Xamarin.iOS

希望对您有所帮助。

如果有什么原因让你不得不使用ZXing框架,你能告诉我一些细节吗?我愿意帮助你。