Take the following steps to easily add to your Xamarin application an additional feature: Live capture barcode reading.
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");
});
}
}
}