使用手电筒进行 xamarin 条形码扫描
xamarin barcode scanning with torch
我正在尝试在我的 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 扫描仪也是 运行 时打开 flashlight/torch?
您可以在扫描布局中添加按钮,实现按钮点击事件,如下代码。
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" />
而且我注意到您在 forms:ZXingScannerView
中使用了 MVVM,正如 Jason 所说。你可以使用 IsTorchOn
属性。绑定一个属性并使用Button click命令来控制它。
<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。
我正在尝试在我的 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 扫描仪也是 运行 时打开 flashlight/torch?
您可以在扫描布局中添加按钮,实现按钮点击事件,如下代码。
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" />
而且我注意到您在 forms:ZXingScannerView
中使用了 MVVM,正如 Jason 所说。你可以使用 IsTorchOn
属性。绑定一个属性并使用Button click命令来控制它。
<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。