.NET MAUI Installation
1. Add the package #
Add the NuGet package #
In your app's folder, add the package from NuGet. A new app comes from the .NET MAUI template (it needs the .NET MAUI workload: dotnet workload install maui); BarkoderSample is the name the General example's code uses:
dotnet new maui -n BarkoderSample
cd BarkoderSample
dotnet add package Plugin.Maui.Barkoder
Register the handler #
Register BarkoderViewHandler in MauiProgram.cs. Add the namespace:
using Plugin.Maui.Barkoder.Controls;
and the handler, in ConfigureMauiHandlers:
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
// Your custom fonts
})
.ConfigureMauiHandlers(handlers =>
{
// Your other maui handlers
handlers.AddHandler(typeof(BarkoderView), typeof(BarkoderViewHandler));
});
2. Allow the camera #
Camera permission #
The scanner needs the camera. On Android the package's manifest asks for it. On iOS, add the camera key to Platforms/iOS/Info.plist:
<key>NSCameraUsageDescription</key>
<string>Camera permission</string>
3. Add the scanner view #
Add BarkoderView #
The namespace #
In the XAML of the page that shows the scanner (MainPage.xaml), declare the plugin's namespace on the ContentPage:
xmlns:barkoder="clr-namespace:Plugin.Maui.Barkoder.Controls;assembly=Plugin.Maui.Barkoder"
In its code-behind (MainPage.xaml.cs), add these using lines:
using Plugin.Maui.Barkoder.Handlers;
using Plugin.Maui.Barkoder.Interfaces;
using Barkoder;
The view #
Then put the view in the page's layout:
<barkoder:BarkoderView
x:Name="BKDView"
barkoder:LicenseKey="YOUR_LICENSE_KEY"
HeightRequest="200"/>
LicenseKey takes your license key; step 4 says where to get one.
4. Set the license key and barcode types #
Settings and barcode types #
In your code-behind file (MainPage.xaml.cs) configure the BarkoderView control and turn on the barcode types to read. Call SetBarkoderSettings() once the view is on screen, as the General example does (half a second after the page is made):
using Plugin.Maui.Barkoder.Enums;
...
private void SetBarkoderSettings()
{
// These are optional settings, otherwise default values will be used
BKDView.SetImageResultEnabled(true);
BKDView.SetLocationInImageResultEnabled(true);
BKDView.SetRegionOfInterestVisible(true);
// Enable desired barcode type
BKDView.SetBarcodeTypeEnabled(BarcodeType.QR, true);
BKDView.SetBarcodeTypeEnabled(BarcodeType.Code128, true);
}
Licensing #
(Unlicensed) and part of its data is replaced with dots. Get a free trial key on the trial page: choose Native or WASM and enter your app ID (for the Web SDK, your domain). A trial key lasts 30 days, for up to 25 devices, one per app ID. For a production license, request a quote. By using the SDK you agree to the End User License Agreement.5. Start scanning and read results #
Start scanning #
Add IBarkoderDelegate to your page's class in MainPage.xaml.cs and call BKDView.StartScanning(this), for example from a button. The results come to DidFinishScanning(BarcodeResult[] result, ImageSource originalImageSource); a DidFinishScanning with other parameters is never called (for the barcode thumbnails too, implement DidFinishScanning(BarcodeResult[] result, ImageSource[] thumbnails, ImageSource originalImageSource) instead):
public partial class MainPage : ContentPage, IBarkoderDelegate
{
// Your existing methods and properties...
// Method to start the scanning process
private void StartScanning()
{
// Start scanning using the BarkoderView control and pass 'this' as the delegate
BKDView.StartScanning(this);
}
// Implementing the DidFinishScanning method from IBarkoderDelegate
public void DidFinishScanning(BarcodeResult[] result, ImageSource originalImageSource)
{
// Handle the scanned result here
foreach (var barcode in result)
{
// Do something with each barcode result, for example:
Console.WriteLine($"Scanned barcode: {barcode.TextualData}");
}
}
}