Barcode scanning with .net maui

2.7k Views Asked by At

I am working to read barcode using .Net Maui and I found something which uses the device's default camera. However the app that I am working needs to use device's integrated barcode reader instead of camera. How this can be achieved either with .Net MAUI or XAMARIN.

3

There are 3 best solutions below

0
Štěpán Obrátil On

For Zebra devices, it is recommended to use Zebra DataWedge.

See https://techdocs.zebra.com/datawedge/latest/guide/api/softscantrigger/ here for example.

(manual is written for Xamarin, but usage in Maui is pretty much the same.)

0
Eves On

Take a look at xzing barcodescanner

From their website: The successor to ZXing.Net.Mobile: barcode scanning and generation for .NET MAUI applications. First, we need to add the ZXing.Net.MAUI library to our project as a dependency. To do this, open the NuGet Package Manager and search for "ZXing.Net.MAUI". Install the package in your .NET MAUI project.

// Add the using to the top
using ZXing.Net.Maui;
// ... other code
public static MauiApp Create() {
    var builder = MauiApp.CreateBuilder();
    builder.UseMauiApp & lt;
    App & gt;
    ().UseBarcodeReader(); // Make sure to add this line
    return builder.Build();
}
0
Tochi On

Besides keyboard mode, most Android devices with built-in barcode scanners support Broadcast mode. When the barcode scanner picks up a barcode, it sends a broadcast message, and your app registers to listen to these broadcast messages. Make sure to change scanner barcode output to Broadcast mode in the phone's settings. Here is a code snippet

// MainActivity.cs in Platforms/Android folder
// BroadcastName and KeyValueName are device model specific. Replace them with yours
public class MainActivity : MauiAppCompatActivity
{
    private readonly BroadcastReceiver barcodeReceiver = new BarcodeBroadcastReceiver();

    protected override void OnResume()
    {
        base.OnResume();
        RegisterReceiver(barcodeReceiver, new IntentFilter("BroadcastName"));
    }

    protected override void OnPause()
    {
        UnregisterReceiver(barcodeReceiver);
        base.OnPause();
    }
}

public class BarcodeBroadcastReceiver : BroadcastReceiver
{
    public override async void OnReceive(Context context, Intent intent)
    {
        // Get the received message from the Intent
        var barcode = intent.GetStringExtra("KeyValueName");

        // call your method of .Net MAUI
    }
}