Barcode scanning library based on native platform APIs for barcode detection:
This library was inspired by existing MAUI barcode scanning libraries: BarcodeScanner.Mobile & Zxing.Net.MAUI, but comes with many code improvements and uses native ML APIs on both Android and iOS/macOS.
ViewfinderMode
- detect only barcodes present in camera preview on screen and AimMode
- detect only the barcode that is overlapped with the red dot centred in camera preview,Initialize the plugin in your MauiProgram.cs
:
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
...
.UseBarcodeScanning();
...
return builder.Build();
}
Edit AndroidManifest.xml
file (under the Platforms\Android folder) and add the following permissions inside of the manifest
node:
<uses-permission android:name="android.permission.CAMERA" />
Edit info.plist
file (under the Platforms\iOS or Platforms\MacCatalyst folder) and add the following permissions inside of the dict
node:
<key>NSCameraUsageDescription</key>
<string>Enable camera for barcode scanning.</string>
And ask for permission from user in your code:
await Methods.AskForRequiredPermissionAsync();
xmlns:scanner="clr-namespace:BarcodeScanning;assembly=BarcodeScanning.Native.Maui"
CameraEnabled
property to true
in XAML, code behind or ViewModel to start the camera. As a best practice set it in OnAppearing()
method override in your ContentPage.OnDetectionFinished
event in Code-behind:
<scanner:CameraView ...
OnDetectionFinished="CameraView_OnDetectionFinished"
.../>
private void CameraView_OnDetectionFinished(object sender, OnDetectionFinishedEventArg e)
{
if (e.BarcodeResults.Length > 0)
...
}
or bind OnDetectionFinishedCommand
property to a Command in your ViewModel:
<scanner:CameraView ...
OnDetectionFinishedCommand="{Binding DetectionFinishedCommand}"
.../>
public ICommand DetectionFinishedCommand { get; set; }
...
DetectionFinishedCommand = new Command<BarcodeResult[]>(BarcodeResult[] result) =>
{
if (result.Length > 0)
...
}
CameraEnabled
property to false
in OnDisappearing()
method override in your ContentPage.DisconnectHandler()
is no loger required, but optional. More info here: What's new in .NET MAUI for .NET 9CaptureNextFrame
property to true
to capture next frame from the camera feed as a PlatformImage
. Listen to OnImageCaptured
event or bind to OnImageCapturedCommand
property to get the caputured image. Image is captured from the original camera feed and can be different from the on-screen preview. After the image is captured CaptureNextFrame
property is automaticly set to false
to prevent memory leaks. Example can be found in ScanTab.xaml.cs
.1D: Codabar, Code 39, Code 93, Code 128, EAN-8, EAN-13, ITF, UPC-A, UPC-E; 2D: Aztec, Data Matrix, PDF417, QR Code
1D: Codabar, Code 39, Code 93, Code 128, EAN-8, EAN-13, GS1 DataBar, ITF, UPC-A, UPC-E; 2D: Aztec, Data Matrix, MicroPDF417, MicroQR, PDF417, QR Code
A list of bindable properties with descriptions can be found in CameraView.cs source file.
Windows is currently unsupported, but support can be added in the future through Zxing.Net project.