zxing意图处理非Activity类

时间:2016-05-09 14:27:25

标签: android android-intent xamarin zxing

我想在非Activity类中启动和处理ZXing意图。但我不知道如何在这堂课中抓住结果。

此代码在Xamarin中输入,但并不重要。

ScannerZXing.cs:

Intent intenScanner;

public ScannerZXing()
{
     intentScanner = ScanIntent.GetZXingScanIntent();
}

public override void Scan(Activity current)
{
     //this.StartActivityForResult(this.intentScanner, 0); // error: Java.Lang.NullPointerExceptionn

    //current.StartActivityForResult(this.intentScanner, 0); // OK, but I have to handle result in MainActivity (I don't want it.)
}

protected override void OnActivityResult(int requestCode, [Android.Runtime.GeneratedEnumAttribute()] Android.App.Result resultCode, Android.Content.Intent intent)
{
     base.OnActivityResult(requestCode, resultCode, intent);
     //....
}

MainClass.cs:

protected override void OnCreate(Bundle bundle)
{

     //....         

     var scanner = new ScannerZXing();

     Button btnScan = this.FindViewById<Button>(Resource.Id.btnScan);
     btnScan.Click += delegate
     {
          scanner.Scan(this);
     }
     //....
}

如果我在MainActivity中执行OnActivityResult,它可以正常工作。但我想将扫描逻辑移到分离的类中。

1 个答案:

答案 0 :(得分:0)

你可以得到zxing的nuget。

https://www.nuget.org/packages/ZXing.Net.Mobile

    private async Task<string> Scan()
    {
        var scanner = new ZXing.Mobile.MobileBarcodeScanner() { BottomText = "Place red line on QR image to scan", TopText = "Place red line on QR image to scan", CancelButtonText = "Cancel scan", CameraUnsupportedMessage = "Camera not availabe on device" };
        var scanResults = await scanner.Scan();
        return scanresults;
    }