I am trying to implement barcode scanning in my Xamarin form, but no success.
I am able to get the camera working, but I don't see the red line on the screen and it simply refuses to scan anything
I tried this answer. I can see thru my camera, but no red line appears. But I can put the torch on and off
XAML code:
<Button BackgroundColor="Chocolate" Clicked="Button_Clicked"/>
<zxing:ZXingScannerView
x:Name="_scanView"
OnScanResult="Handle_OnScanResult"
IsScanning="true"
IsAnalyzing="true"
WidthRequest="200"
HeightRequest="200" />
C# Code:
private void Button_Clicked(object sender, EventArgs e)
{
_scanView.ToggleTorch();
}
private void Handle_OnScanResult(ZXing.Result result)
{
ChassisEntry.Text = result.Text;
}
// this is in the constructor of the page
MobileBarcodeScanningOptions options = new ZXing.Mobile.MobileBarcodeScanningOptions()
{
TryHarder = true,
PossibleFormats = new List<ZXing.BarcodeFormat>() { ZXing.BarcodeFormat.All_1D }
};
_scanView.Options = options;
What am I missing ?
EDIT
I have this in my MainActivity.cs
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
ZXing.Net.Mobile.Forms.Android.Platform.Init();
LoadApplication(new App());
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
ZXing.Net.Mobile.Forms.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
Addition to the answer
@Rafeal 's answer is working for me, the only problem is that there is no red line on the scan view.
I found this workaround
for that problem.
However, it is a workaround because I notice that the scan already happens if the barcode is anywhere in the view.
Though this might not appear a problem, in my case I have paper forms with up to 20 barcodes on it, and when the user wants to aim at a particular barcode he might get the wrong one.
So if anybody knows a better solution I would like to hear it.
I will make a separate question on SO about this problem.
This is the workaround I am using now
<Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<zxing:ZXingScannerView
x:Name="_scanView"
OnScanResult="Handle_OnScanResult"
IsScanning="true"
WidthRequest="200"
HeightRequest="200"/>
<zxing:ZXingDefaultOverlay
x:Name="scannerOverlay"
BottomText="Place the red line over the barcode you'd like to scan.">
</zxing:ZXingDefaultOverlay>
</Grid>