ZXing.Net.Mobile无法立即开始扫描

时间:2018-05-24 06:33:44

标签: c# xamarin xamarin.forms

我有一个ScannerView作为MainPage的页面。当我启动应用程序时,它无法扫描条形码。我必须将另一个页面设置为主页,然后导航到扫描仪页面,然后才能扫描条形码。或锁定然后解锁手机,然后它将开始扫描。

App.xaml.cs:

MainPage = new NavigationPage(new ScannerPage());

ScannerPage.xaml:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyApp.Pages.ScannerPage"
             xmlns:zxing="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms">
    <ContentPage.Content>
        <Grid>
            <zxing:ZXingScannerView x:Name="ScannerView"
                                    IsScanning="True"
                                    IsAnalyzing="True" />
            <zxing:ZXingDefaultOverlay x:Name="ScannerOverlay"
                                       TopText="Hold your phone up to the QR code"
                                       BottomText="Scanning will happen automatically"
                                       ShowFlashButton="True"/>
        </Grid>
    </ContentPage.Content>
</ContentPage>

ScannerPage.xaml.cs:

public partial class ScannerPage : ContentPage
{
    public ScannerPage ()
    {
        InitializeComponent ();

        ScannerView.Options = new MobileBarcodeScanningOptions
        {
            PossibleFormats = new List<BarcodeFormat>
            {
                BarcodeFormat.DATA_MATRIX,
            },
            TryHarder = true
        };

        ScannerView.OnScanResult += (result) => Device.BeginInvokeOnMainThread(async () =>
        {
            ScannerView.IsAnalyzing = false;
            await DisplayAlert("Scanned", result.Text, "Ok");
            ScannerView.IsAnalyzing = true;
        });
    }
}

2 个答案:

答案 0 :(得分:0)

我最终以编程方式使用以下代码:

    public static async Task ScanConnection()
    {
            MobileBarcodeScanningOptions options = new ZXing.Mobile.MobileBarcodeScanningOptions()
                {
                    TryHarder = true,
                    PossibleFormats = new List<ZXing.BarcodeFormat>() { ZXing.BarcodeFormat.QR_CODE }
                };


                MobileBarcodeScanner scanner = new ZXing.Mobile.MobileBarcodeScanner();

                ZXing.Result result = await scanner.Scan(options);

                if (result != null && !string.IsNullOrEmpty(result.Text))
                {
                    ...
                }
    }

您可以从派生页面中的重写OnAppearing()方法调用该代码。

答案 1 :(得分:0)

我有类似的经历。第一次调用扫描仪页面,它将尝试扫描但不识别任何条形码。退出页面,重新调用,以后没有问题。 在我的情况下有效的解决方案是添加ZXing.Mobile.MobileBarcodeScanner.Initialize(Application);在两个地方。

  1. 在AndroidManifest.xml中
ZXing.Net.Mobile.Forms.Android.Platform.Init();
ZXing.Mobile.MobileBarcodeScanner.Initialize(Application);
</manifest>
  1. 也MainActivity.cs ..(您可能有所不同)

     public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            base.OnCreate(savedInstanceState);
            ZXing.Net.Mobile.Forms.Android.Platform.Init();
            ZXing.Mobile.MobileBarcodeScanner.Initialize(Application);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }
        //needed for zxing scanner..
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
        {
            global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }