Take the following steps to easily add to your Xamarin application an additional feature: Live capture barcode reading.
Xamarin Framework Deprecation
As of LEADTOOLS v23, The cross-platform Xamarin framework has been deprecated and replaced with LEADTOOLS .NET MAUI framework.
For any LEADTOOLS Xamarin apps you have already built, we recommend you migrate to the LEADTOOLS .NET MAUI framework. To help you with the migration, review the LEADTOOLS .NET MAUI tutorials to get you started.
This change is driven by Microsoft's announcement to end Xamarin support.
Create a new Xamarin forms project and add the camera control by following the Adding a Basic Xamarin Camera Control tutorial.
Right-click the Solution in Visual Studio and select Manage NuGet packages for Solution. Type a search for "Leadtools.Barcode" to install/add it to your solution.
Once you have added the Leadtools.Barcode Nuget Package, add the following global variable to the MainPage:
Leadtools.Barcode.BarcodeEngine barcodeEngine;
In the MainPage constructor, after the license code and the InitializeComponent(), initialize the BarcodeEngine
object, set the AutoRotateImage
option of the Camera
, and subscribe to the FrameReceived
event:
barcodeEngine = new Leadtools.Barcode.BarcodeEngine();
leadCamera.CameraOptions.AutoRotateImage = true;
leadCamera.FrameReceived += LeadCamera_FrameReceived;
Add the FrameReceived
event:
private void LeadCamera_FrameReceived(Leadtools.Camera.Xamarin.FrameHandlerEventArgs e)
{
if (e.Image != null)
{
var data = barcodeEngine.Reader.ReadBarcode(e.Image, Leadtools.LeadRect.Empty, Leadtools.Barcode.BarcodeSymbology.Unknown);
if (data != null && data.Value != null)
{
Device.BeginInvokeOnMainThread(() =>
{
DisplayAlert("Barcode Found!", $"{data.Symbology} : {data.Value}", "OK");
});
}
}
}