使用Xamarin.IOS在ZXing自定义覆盖上没有相机视图

时间:2016-09-05 17:46:34

标签: xamarin xamarin.ios zxing

我在我的Xamarin.IOS应用程序上使用Zxing,我构建了一个正确显示的自定义覆盖,但完全没有摄像头视图"当我在QR Code前面随机移动手机时,它会扫描它#34;但是新的自定义覆盖图上没有摄像头视图,因此用户可以看到他的摄像头有什么。

这是自定义叠加层

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

1 个答案:

答案 0 :(得分:1)

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

由于:

  1. 确定AVFoundation比iOS上的ZXing更具击球性能;
  2. 如果要自定义UI,使用AVFoundation会更方便;
  3. AVFoundation由Apple提供,我不认为任何第三方框架可以比Apple的更稳定。
  4. 有一个关于如何使用AVFoundation在我的GitHub上实现它的示例,你也可以将它用作库。

    链接:QRCode scanner sample for Xamarin.iOS

    希望它可以帮到你。

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