Detect and Extract Barcodes using Live Capture - Xamarin C#

This tutorial shows how to live capture barcodes with the Xamarin Camera Control in a C# Xamarin application using the LEADTOOLS SDK.

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
Development License Download LEADTOOLS

Required Knowledge

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 - Xamarin C# tutorial.

Create the Project and Add the LEADTOOLS References

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:

For a complete list of which DLL libraries are required for your application, refer to Files to be Included with your Application.

Set the License File

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:

Add Code to Check and Ask for Camera Permission

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:

C#
using System; 
using System.Text; 
 
using Xamarin.Forms; 
using Xamarin.Forms.Xaml; 
 
using Leadtools; 
using Leadtools.Barcode; 

Add the following global variable:

C#
private BarcodeEngine barcodeEngineInstance; 

Initialize the BarcodeEngine instance inside the LiveCapturePage class as seen below.

C#
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.

C#
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"); 
            }); 
        } 
    } 
} 

Run the Project

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.

Wrap-up

This tutorial showed how to use the FrameReceived event to run barcode live detection using the device's camera.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.


Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.