【问题标题】:ZXing.Net.Mobile does not start scanning immediatelyZXing.Net.Mobile 没有立即开始扫描
【发布时间】:2018-11-03 06:37:56
【问题描述】:

我有一个以 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;
        });
    }
}

【问题讨论】:

  • 尝试在代码中设置IsScanning="True" IsAnalyzing="True"。页面加载后
  • 尝试在页面构造函数和OnAppearing中设置,没有效果。
  • 似乎是一个错误初始化,向 git hub 注册一个问题
  • 我会的,即使那个 github 上没有太多开发者的回应。

标签: c# xamarin xamarin.forms


【解决方案1】:

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

    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() 方法调用该代码。

【讨论】:

  • 我的页面只是一个普通页面,但是它不包含任何关于 xaml 中的 Zxing 扫描仪的内容。我发布的代码将直接从代码隐藏创建和处理扫描。 awaitscanner.Scan(options) 会在被调用时自动创建相机视图。
  • 不能在xaml页面添加MobileBarcodeScanningOptions吗??
【解决方案2】:

我遇到了类似的问题。在第一次调用扫描仪页面时,它会尝试扫描但不会识别任何条形码。退出页面后重新打开,问题就消失了。

适用于我的解决方案是在两个地方添加以下内容:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多