【问题标题】:xamarin barcode scanning with torchxamarin 条码扫描与手电筒
【发布时间】:2020-08-10 15:47:48
【问题描述】:

我正在尝试在我的 xamarin android 应用程序中使用 xzing 条码扫描仪,但我还需要打开手电筒以便在光线不足的条件下进行扫描。

我正在使用 xamarin Essentials 尝试打开手电筒,但我不断收到以下错误消息。

     Torch for camera "0" is not available due to an existing camera user

我有一个包含我的扫描仪的 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                                      
                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>

然后我在命令后面有以下代码来切换手电筒

public Command FlashToggleCommand
    {
        get { return new Command(async () =>
        {
            try
            {
                   
                // Turn On
                await Flashlight.TurnOnAsync();

                //// Turn Off
                //await Flashlight.TurnOffAsync();
            }
            catch (FeatureNotSupportedException fnsEx)
            {
                // Handle not supported on device exception
            }
            catch (PermissionException pEx)
            {
                // Handle permission exception
            }
            catch (Exception ex)
            {
                // Unable to turn on/off flashlight
            }

        }); }
    }

但我继续使用上面的错误消息进入异常块

我假设条形码扫描仪正在使用相机,有谁知道如何在 zxing 扫描仪也在运行时打开手电筒/手电筒?

【问题讨论】:

  • ZXingScannerView 有一个 IsTorchOn 属性,你可以切换

标签: android xamarin zxing


【解决方案1】:

您可以在扫描布局中添加按钮,实现按钮点击事件,如下代码。

 private void Button_Clicked(object sender, System.EventArgs e)
        {
            zxingView.ToggleTorch();

        }

注意:zxingView

<zxing:ZXingScannerView x:Name="zxingView" Grid.Row="1" OnScanResult="Handle_OnScanResult" IsScanning="true" WidthRequest="200" HeightRequest="200" />

正如 Jason 所说,我注意到您在 forms:ZXingScannerView 中使用了 MVVM。您可以使用IsTorchOn 属性。绑定一个属性,使用Button点击命令来控制它。

<Button Text="click" Command="{Binding FlashToggleCommand}"></Button>
<zxing:ZXingScannerView x:Name="zxingView" Grid.Row="1" OnScanResult="Handle_OnScanResult" IsScanning="true" IsTorchOn="{Binding TouchON}"
                                WidthRequest="200" HeightRequest="200" />

背景代码。

public PartialScreenScanning()
        {
            InitializeComponent();
            this.BindingContext = new MyViewModel();
        }

然后创建一个MyViewModel.cs

   public class MyViewModel: INotifyPropertyChanged
    {
        public MyViewModel()
        {
           

              
        }
        bool _touchON=false;
        public Command FlashToggleCommand
        {
            get
            {
                return new Command(async () =>
                {

                    TouchON = !TouchON;
                });
            }
        }
       
        public bool TouchON
        {
            set
            {
                if (_touchON != value)
                {
                    _touchON = value;
                    OnPropertyChanged("TouchON");

                }
            }
            get
            {
                return _touchON;
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

这是正在运行的 GIF。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多