xamarin xzing 条码扫描仪重新扫描 [英] xamarin xzing barcode scanner re-scan

查看:16
本文介绍了xamarin xzing 条码扫描仪重新扫描的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 xamarin android 表单中使用 zxing 条码扫描仪,我可以让它毫无问题地扫描一个条码,但我希望能够放弃他们进行的扫描并能够进行另一次扫描.

I'm using the zxing barcode scanner in xamarin android forms and I can get it to scan one barcode with no issues, but I want to be able to discard the scan they have taken and have the ability to take a another scan.

我也在使用 MVVM.这是我的 xaml...

I'm also using MVVM. Here is my xaml...

 <Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
            </Grid.RowDefinitions>
          
            <forms:ZXingScannerView  x:Name="zxingView" 
                                     IsTorchOn="{Binding TorchON}"       
                                     IsScanning="{Binding IsScanning}" 
                                     IsAnalyzing="{Binding IsAnalyzing}"
                                     Result="{Binding Result, Mode=TwoWay}" 
                                     ScanResultCommand="{Binding ScanCommand}"
                                     />
            <forms:ZXingDefaultOverlay               
                x:Name="scannerOverlay"                                                       
                BottomText="Place the red line over the barcode you'd like to scan." />
            <Button Grid.Row="1" Text="Toggle Flash"  Command="{Binding FlashToggleCommand}"></Button>
        </Grid>

这是我的页面模型

private string barcode = string.Empty;

    public string Barcode
    {
        get { return barcode; }
        set { barcode = value; }
    }

    private bool _isAnalyzing = true;

    public bool IsAnalyzing
    {
        get { return _isAnalyzing; }
        set
        {
            if (!Equals(_isAnalyzing, value))
            {
                _isAnalyzing = value;
                OnPropertyChanged("IsAnalyzing");
            }
        }
    }

    private bool _isScanning = true;
    private bool _torchON = false;
    private DynamicContainerPageModel _hhtScreen;
    private readonly IDeviceManager _deviceManager;

    public ScanningViewPageModel(IDeviceManager deviceManager)
    {
        _deviceManager = deviceManager;
    }

    public override void Init(object initData)
    {
        base.Init(initData);
        _hhtScreen = initData as DynamicContainerPageModel;

    }


    public bool IsScanning
    {
        get { return _isScanning; }
        set
        {
            if (!Equals(_isScanning, value))
            {
                _isScanning = value;
                OnPropertyChanged("IsScanning");
            }
        }
    }

    public bool TorchON
    {
        set
        {
            if (_torchON != value)
            {
                _torchON = value;
                OnPropertyChanged("TorchON");
            }
        }
        get { return _torchON; }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public Command ScanCommand
    {
        get
        {
            return new Command(() =>

            {
                IsAnalyzing = false;
                IsScanning = false;

                Device.BeginInvokeOnMainThread(async () =>
                {
                    Barcode = Result.Text;
                    var response = await CoreMethods.DisplayAlert("Barcode found", "Found: " + Result.Text, "Keep",
                        "Scan Again");

                    if (response)
                    {
                        //Save the value into the model
                        _deviceManager.BeginInvokeOnMainThread(() =>
                                    {
                                        _hhtScreen.SelectedControl.Text = barcode;
                                    });

                        //close page
                        await this.CoreMethods.PopPageModel(false);
                    }
                    else
                    {
                        Result = null;
                        IsAnalyzing = true;
                        IsScanning = true;
                    }
                });

                IsAnalyzing = true;
                IsScanning = true;
            });
        }
    }

    public Command FlashToggleCommand
    {
        get { return new Command(async () => { TorchON = !TorchON; }); }
    }

    public Result Result { get; set; }

当我在弹出窗口中再次按下 scan 时,我发现它有点受打击,并且会错过扫描摄像头是否再次激活,大部分时间它只是冻结.难道我做错了什么?有没有更好的方法让控件重新扫描?

When I press scan again on my pop up, I find it a bit hit and miss whether the scanning camera activates again or not, majority of the time it just freezes. Am I doing something wrong? Is there a better way to get the control to rescan?

推荐答案

我如何解决这个问题:

Xaml

<coreControls:ContainerLayout.Content>
            <Grid>
                <ContentView
                    x:Name="contentViewCamera"/>
                <coreControls:SvgImage
                    SvgSource="img_qr_background"
                    VerticalOptions="FillAndExpand"
                    Aspect="AspectFill"/>
                <Grid
                    x:Name="mainLayout"
                    RowDefinitions="48,*,*,*">
                    <contentViews:HeaderView Title="Canjear cupon"
                                             TitleColor="{AppThemeBinding Light={StaticResource LightWhiteColor}, Dark={StaticResource DarkWhiteColor}}"
                                             RightIconSvg="ic_flash_w"
                                             Margin="6,0" />
                    <material:MaterialEntry
                        Grid.Row="3"
                        HorizontalOptions="Center"
                        VerticalOptions="Center"
                        WidthRequest="240"
                        HeightRequest="42"
                        BackgroundColor="#70000000"
                        BorderColor="#70000000"
                        Placeholder="Ingresa el codigo"/>
                </Grid>
            </Grid>
        </coreControls:ContainerLayout.Content>

背后的代码

public partial class RedeemCouponPage
{
    private ZXingScannerView _scannerView;

    public RedeemCouponPage()
    {
        InitializeComponent();
    }
    protected override void OnAppearing()
    {
        base.OnAppearing();

        //mainLayout.Padding = PageHelper.GetPageSafeArea(new Thickness(0, 20), this);
    }
    protected override void OnViewModelSet()
    {
        base.OnViewModelSet();
        var options = new MobileBarcodeScanningOptions
        {
            AutoRotate = false,
            TryHarder = true,
            TryInverted = true,
            DelayBetweenAnalyzingFrames = 5,
            DelayBetweenContinuousScans = 5,
            PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE }
        };

        ViewModel.InitializeCameraCommand = new MvxCommand(() =>
        {
            _scannerView = new ZXingScannerView()
            {
                Options = options,
                ScanResultCommand = ViewModel.ScanResultCommand
            };

            _scannerView.SetBinding(ZXingScannerView.IsScanningProperty, nameof(ViewModel.IsBusy));
            _scannerView.SetBinding(ZXingScannerView.IsAnalyzingProperty, nameof(ViewModel.IsBusy));
            _scannerView.SetBinding(ZXingScannerView.IsTorchOnProperty, nameof(ViewModel.IsFlashActive));
            _scannerView.SetBinding(ZXingScannerView.ResultProperty, nameof(ViewModel.Result), BindingMode.TwoWay);
            contentViewCamera.Content = _scannerView;
        });
    }
}

视图模型

public class RedeemCouponViewModel: BaseViewModel
{
    private readonly ICouponService CouponService = Mvx.IoCProvider.Resolve<ICouponService>();

    public IMvxCommand InitializeCameraCommand { get; set; }

    private ZXing.Result _result;
    public ZXing.Result Result
    {
        get => _result;
        set
        {
            SetProperty(ref _result, value);
            BarCodeText = value.Text;
        }
    }

    private bool _isFlashActive;
    public bool IsFlashActive
    {
        get => _isFlashActive;
        set => SetProperty(ref _isFlashActive, value);
    }

    private string _barCodeText;
    public string BarCodeText
    {
        get => _barCodeText;
        set => SetProperty(ref _barCodeText, value);
    }

    private Coupon _coupon;
    public Coupon Coupon
    {
        get => _coupon;
        set => SetProperty(ref _coupon, value);
    }

    public override void ViewAppeared()
    {
        base.ViewAppeared();

        if (DeviceInfo.DeviceType == DeviceType.Physical)
            InitializeCameraCommand.Execute();

        IsBusy = true;
    }

    public IMvxAsyncCommand ScanResultCommand => new MvxAsyncCommand(async () =>
    {
        if (Result == null)
        {
            IsBusy = true;
            return;
        }
        else
            IsBusy = false;

        Coupon = await CouponService.Get(BarCodeText);
        PopupIsVisible = true;


    }, () => IsBusy);

    public IMvxCommand ConfirmRedeemCommand => new MvxCommand(()=>
    {
        DisplaySuccessView("Canjeado!","El cupon ha sido canjeado con exito.");
        if (DeviceInfo.DeviceType == DeviceType.Physical)
            InitializeCameraCommand.Execute();
        PopupIsVisible = false;
        IsBusy = true;
    });

    public IMvxCommand BackToScanerCommand => new MvxCommand(() =>
    {
        PopupIsVisible = false;
        if (DeviceInfo.DeviceType == DeviceType.Physical)
            InitializeCameraCommand.Execute();

        IsBusy = true;
    });
}

这篇关于xamarin xzing 条码扫描仪重新扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆