Get Started With Barcode Capture

In this guide you will learn step by step how to add barcode capture to your application. Roughly, the steps are:

  • Include the ScanditBarcodeCapture library and its dependencies to your project, if any.

  • Create a new data capture context instance, initialized with your license key.

  • Create a barcode capture settings and enable the barcode symbologies you want to read in your application.

  • Create a new barcode capture mode instance and initialize it with the settings created above.

  • Register a barcode capture listener to receive scan events. Process the successful scans according to your application’s needs, e.g. by looking up information in a database. After a successful scan, decide whether more codes will be scanned, or the scanning process should be stopped.

  • Obtain a camera instance and set it as the frame source on the data capture context.

  • Display the camera preview by creating a data capture view.

  • If displaying a preview, optionally create a new overlay and add it to data capture view for a better visual feedback.

Prerequisites

Before starting with adding a capture mode, make sure that you have a valid Scandit Data Capture SDK license key and that you added the necessary dependencies. If you have not done that yet, check out this guide.

Note

You can retrieve your Scandit Data Capture SDK license key, by signing in to your account at ssl.scandit.com/dashboard/sign-in.

Internal dependencies

Some of the Scandit Data Capture SDK modules depend on others to work:

Module

Dependencies

Scandit.DataCapture.Core

No dependencies

Scandit.DataCapture.Core.Maui

  • Scandit.DataCapture.Core

Scandit.DataCapture.BarcodeCapture

  • Scandit.DataCapture.Core

Scandit.DataCapture.Parser

  • Scandit.DataCapture.Core

Scandit.DataCapture.TextCapture

  • Scandit.DataCapture.Core

Scandit.DataCapture.IdCapture

  • Scandit.DataCapture.Core

  • Scandit.DataCapture.TextCapture (VIZ document)

Create the Data Capture Context

The first step to add capture capabilities to your application is to create a new data capture context. The context expects a valid Scandit Data Capture SDK license key during construction.

DataCaptureContext context = DataCaptureContext.ForLicenseKey("-- ENTER YOUR SCANDIT LICENSE KEY HERE --");

Configure the Barcode Scanning Behavior

Barcode scanning is orchestrated by the BarcodeCapture data capture mode. This class is the main entry point for scanning barcodes. It is configured through BarcodeCaptureSettings and allows to register one or more listeners that will get informed whenever new codes have been recognized.

For this tutorial, we will setup barcode scanning for a small list of different barcode types, called symbologies. The list of symbologies to enable is highly application specific. We recommend that you only enable the list of symbologies your application requires.

BarcodeCaptureSettings settings = BarcodeCaptureSettings.Create();
HashSet<Symbology> symbologies = new HashSet<Symbology>()
{
    Symbology.Code128,
    Symbology.Code39,
    Symbology.Qr,
    Symbology.Ean8,
    Symbology.Upce,
    Symbology.Ean13Upca
};
settings.EnableSymbologies(symbologies);

If you are not disabling barcode capture immediately after having scanned the first code, consider setting the BarcodeCaptureSettings.CodeDuplicateFilter to around 500 or even -1 if you do not want codes to be scanned more than once.

Next, create a BarcodeCapture instance with the settings initialized in the previous step:

barcodeCapture = BarcodeCapture.Create(context, settings);

Register the Barcode Capture Listener

To get informed whenever a new code has been recognized, add a IBarcodeCaptureListener through BarcodeCapture.AddListener() and implement the listener methods to suit your application’s needs.

First implement the IBarcodeCaptureListener interface. For example:

public void OnBarcodeScanned(BarcodeCapture barcodeCapture, BarcodeCaptureSession session, IFrameData frameData)
{
    IList<Barcode> barcodes = session?.NewlyRecognizedBarcodes;
    // Do something with the barcodes

    // Dispose the frame when you have finished processing it. If the frame is not properly disposed,
    // different issues could arise, e.g. a frozen, non-responsive, or "severely stuttering" video feed.
    frameData.Dispose();
}

Then add the listener:

barcodeCapture.AddListener(this);

Alternatively to register IBarcodeCaptureListener interface it is possible to subscribe to corresponding events. For example:

barcodeCapture.BarcodeScanned += (object sender, BarcodeCaptureEventArgs args) =>
{
    IList<Barcode> barcodes = args.Session?.NewlyRecognizedBarcodes;
    // Do something with the barcodes
}

Use the Built-in Camera

The data capture context supports using different frame sources to perform recognition on. Most applications will use the built-in camera of the device, e.g. the world-facing camera of a device. The remainder of this tutorial will assume that you use the built-in camera.

Important

In iOS, the user must explicitly grant permission for each app to access cameras. Your app needs to provide static messages to display to the user when the system asks for camera permission. To do that include the NSCameraUsageDescription key in your app’s Info.plist file.

When using the built-in camera there are recommended settings for each capture mode. These should be used to achieve the best performance and user experience for the respective mode. The following couple of lines show how to get the recommended settings and create the camera from it:

camera = Camera.GetDefaultCamera();
camera?.ApplySettingsAsync(BarcodeCapture.RecommendedCameraSettings);

Because the frame source is configurable, the data capture context must be told which frame source to use. This is done with a call to DataCaptureContext.SetFrameSourceAsync():

context.SetFrameSourceAsync(camera);

The camera is off by default and must be turned on. This is done by calling IFrameSource.SwitchToDesiredStateAsync() with a value of FrameSourceState.On:

camera?.SwitchToDesiredStateAsync(FrameSourceState.On);

There is a separate guide for more advanced camera functionality.

Use a Capture View to Visualize the Scan Process

When using the built-in camera as frame source, you will typically want to display the camera preview on the screen together with UI elements that guide the user through the capturing process. To do that, add a DataCaptureView to your view hierarchy:

DataCaptureView dataCaptureView = DataCaptureView.Create(dataCaptureContext, View.Bounds);
View.AddSubview(dataCaptureView);

Alternatively you can use a DataCaptureView from XAML in your MAUI application. For example:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:scandit="clr-namespace:Scandit.DataCapture.Core.UI.Maui;assembly=ScanditCaptureCoreMaui"
  <ContentPage.Content>
      <AbsoluteLayout>
          <scandit:DataCaptureView
              x:Name="dataCaptureView"
              AbsoluteLayout.LayoutBounds="0,0,1,1"
              AbsoluteLayout.LayoutFlags="All"
              DataCaptureContext="{Binding DataCaptureContext}" >
          </scandit:DataCaptureView>
      </AbsoluteLayout>
  </ContentPage.Content>
</ContentPage>

You can configure your view in the code behind class. For example:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        // Initialization of DataCaptureView happens on handler changed event.
        dataCaptureView.HandlerChanged += DataCaptureViewHandlerChanged;
    }

    private void DataCaptureViewHandlerChanged(object? sender, EventArgs e)
    {
        // Your dataCaptureView configuration goes here, e.g. add overlay
    }
}

For MAUI development add Scandit.DataCapture.Core.Maui NuGet package into your project.

To visualize the results of barcode scanning, the following overlay can be added:

BarcodeCaptureOverlay overlay = BarcodeCaptureOverlay.Create(barcodeCapture, dataCaptureView);

Disabling Barcode Capture

To disable barcode capture, for instance as a consequence of a barcode being recognized, set BarcodeCapture.Enabled to false.

The effect is immediate: no more frames will be processed after the change. However, if a frame is currently being processed, this frame will be completely processed and deliver any results/callbacks to the registered listeners. Note that disabling the capture mode does not stop the camera, the camera continues to stream frames until it is turned off.

What’s next?

To dive further into the Scandit Data Capture SDK we recommend the following articles: