I couldn't find any working source of Xamarin forms barcode scanner. Is there any working sample of Xamarin forms barcode scanner using zxing library?
You can try the code below. Add the zxing library/component to all the projects in the solution
public class Home : ContentPage
{
string message = "";
public Home()
{
//Intialize the button
Button btnScan = new Button
{
Text = "Start Scan",
BackgroundColor = Color.FromRgb(207, 197, 159),
TextColor = Color.White,
BorderRadius = 5,
TranslationY = 120
};
//Attach the click event
btnScan.Clicked += btnScan_Clicked;
this.Content = new StackLayout
{
BackgroundColor = Color.FromRgb(150, 172, 135),
Spacing = 10,
Padding = 25,
Children =
{
btnScan
}
};
}
async void btnScan_Clicked(object sender, EventArgs e)
{
var scanner = new MobileBarcodeScanner();
scanner.TopText = "Hold the camera up to the barcode\nAbout 6 inches away";
scanner.BottomText = "Wait for the barcode to automatically scan!";
//This will start scanning
ZXing.Result result = await scanner.Scan();
//Show the result returned.
HandleResult(result);
}
void HandleResult(ZXing.Result result)
{
var msg = "No Barcode!";
if (result != null)
{
msg = "Barcode: " + result.Text + " (" + result.BarcodeFormat + ")";
}
DisplayAlert("", msg, "Ok");
}
}
You can use ZXing.Net.Mobile.Forms. But you attention.
ZXing.Net.Mobile.Forms current version is 2.4.1. I used this version and build failed on Xamarin.Forms.Android project. => Crash App.
=> I used version 2.3.2. It working fine.
In Android project file MainActivity.cs, you add following code:
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
global::ZXing.Net.Mobile.Forms.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
It seems, the tutorial code on here is incorrect
Call scanner:
private async void BtnScan_OnClicked(object sender, EventArgs e)
{
ZXingScannerPage scanPage = new ZXingScannerPage();
scanPage.OnScanResult += (result) =>
{
scanPage.IsScanning = false;
Device.BeginInvokeOnMainThread(() =>
{
Navigation.PopAsync();
EtInputCode.Text = "Code: " + result.Text;
});
};
await Navigation.PushAsync(scanPage);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With