This tutorial shows how to live capture barcodes in a C# Xamarin application using the LEADTOOLS Xamarin Camera Control.
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.
Overview | |
---|---|
Summary | This tutorial covers how to use the FrameReceived event to live capture barcodes in a C# Xamarin application. |
Completion Time | 15 minutes |
Visual Studio Project | Download tutorial project (509 KB) |
Platform | C# Xamarin Cross-Platform Application |
IDE | Visual Studio 2019, 2022 |
Development License | Download LEADTOOLS |
Get familiar with the basic steps of creating a project and working with the Xamarin Camera Control by reviewing the Add References and Set a License and Integrate Live Capture with Xamarin Camera Control tutorials, before working on the Detect and Extract Barcodes using Live Capture tutorial.
Start with a copy of the project created in the Integrate Live Capture with Xamarin Camera Control tutorial. If you do not have that project, follow the steps in that tutorial to create it.
The references needed depend upon the purpose of the project. For this project, the following NuGet packages are needed:
Leadtools.Barcode
Leadtools.Camera.Xamarin
For a complete list of which DLL libraries are required for your application, refer to Files to be Included with your Application.
The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.
There are two types of runtime licenses:
With the project created, the references added, and the license set, coding can begin.
In the Solution Explorer, open LiveCapturePage.xaml.cs
and ensure that the following are added to the using
area at the top of the code:
using System;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Leadtools;
using Leadtools.Barcode;
Add the following global variable:
private BarcodeEngine barcodeEngineInstance;
Initialize the BarcodeEngine
instance inside the LiveCapturePage
class as seen below.
public LiveCapturePage()
{
InitializeComponent();
leadCamera.CameraOptions.AutoRotateImage = true;
barcodeEngineInstance = new BarcodeEngine();
}
Add the code below to the LeadCamera_FrameReceived
event handler to grab each frame as a RasterImage
object, run barcode detection, and if a barcode(s) is detected extract the barcode data and output the barcode information to the user.
private void LeadCamera_FrameReceived(Leadtools.Camera.Xamarin.FrameHandlerEventArgs e)
{
// Frame Received Handler Code
frameCounter = frameCounter + 1;
Device.BeginInvokeOnMainThread(() =>
{
capturedFrames.Text = $"Frames Processed: {frameCounter}";
});
using (RasterImage image = e.Image)
{
BarcodeData[] dataArray = barcodeEngineInstance.Reader.ReadBarcodes(image, LeadRect.Empty, 0, null);
if (dataArray.Length > 0)
{
liveCapture.Text = "Live Capture";
leadCamera.FrameReceived -= LeadCamera_FrameReceived;
StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0} barcode(s) found", dataArray.Length);
sb.AppendLine();
for (int i = 0; i < dataArray.Length; i++)
{
BarcodeData data = dataArray[i];
sb.AppendFormat("Symbology: {0}, Location: {1}, Data: {2}", data.Symbology.ToString(), data.Bounds.ToString(), data.Value);
sb.AppendLine();
}
Device.BeginInvokeOnMainThread(() =>
{
DisplayAlert("Barcode(s) Found!", sb.ToString(), "OK");
});
}
}
}
Select the desired project (iOS or Android) and run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the application runs and it will ask to allow Camera
permissions which is required.
A preview from the device's camera will be displayed in the CameraView.
For testing, click the Live Capture button at the bottom of the device's screen. The frame counter will increase at the top of the UI to show the frames captured by the CameraView.
For each frame a RasterImage
object is created and the BarcodeEngine
will look to detect a barcode in the frame.
If there is a barcode in the frame, it will be detected, extracted, and the information will display as an alert to the user.
This tutorial showed how to use the FrameReceived
event to run barcode live detection using the device's camera.