Integrate the Scandit Barcode Scanner into your app

This guide shows how to use the unified API for scanning barcodes in your application.

Get the Scandit Barcode Scanner

The unified barcode scanner API is available as a NuGet package and can easily be installed via NuGet.org. For details on how to add a NuGet package to your project, take a look at the guides for Xamarin Studio, or Visual Studio. Search for the Scandit.BarcodePicker.Unified package and add it to your project. You will need to add a reference to the portable class library where your barcode scanning logic is going to be implemented, and to the iOS/Android and Windows applications. The latter is required to also include the platform-specific portion of the code into your app.

Add the barcode scanner namespace to easily access its classes

If you don't want to type the whole namespace each time when using the scanner you should add a using directive at the top of the class you will use the scanner in:

Set the app key

Sign in to your account at http://account.scandit.com to look up your app key in the Download section.

App Key in your account


The application key must be set before you use the barcode picker for the first time. Typically you will put this code into the Page constructor that uses the barcode picker:

// must be set before you use the picker for the first time.
ScanditService.ScanditLicense.AppKey = "--- ENTER YOUR SCANDIT APP KEY HERE ---";

Add code to start the scanning process

In the unified API, the barcode picker is a singleton whose instance will be different depending on the platform the code runs on. To scan a barcode, these 3 steps are neccessary. 1. configure the barcode picker, 2. register a DidScan delegate, 3. start the scanning. These steps are shown in the code sample below:

// Configure the barcode picker through a scan settings instance by defining which
// symbologies should be enabled.
var settings = ScanditService.BarcodePicker.GetDefaultScanSettings();
// prefer backward facing camera over front-facing cameras.
settings.CameraPositionPreference = CameraPosition.Back;
// Enable symbologies that you want to scan.
settings.EnableSymbology(Symbology.EAN13, true);
settings.EnableSymbology(Symbology.UPCA, true);
settings.EnableSymbology(Symbology.QR, true);
ScanditService.BarcodePicker.DidScan += OnDidScan;
await ScanditService.BarcodePicker.ApplySettingsAsync(settings);
// Start the scanning process.
await ScanditService.BarcodePicker.StartScanningAsync();

Add code to handle the scanning event

To be notified when a barcode is scanned, add a delegate to the DidScan event of the barcode picker. A possible implementation of such a delegate looks like this:

void OnDidScan(ScanSession session)
{
// guaranteed to always have at least one element, so we don't have to
// check the size of the NewlyRecognizedCodes array.
var firstCode = session.NewlyRecognizedCodes.First();
var message = string.Format("Code Scanned:\n {0}\n({1})", firstCode.Data,
firstCode.SymbologyString.ToUpper());
// Because this event handler is called from an scanner-internal thread,
// you must make sure to dispatch to the main thread for any UI work.
Device.BeginInvokeOnMainThread(() => {
this.ResultLabel.Text = message
ScanditService.BarcodePicker.DidScan -= OnDidScan;
});
session.StopScanning();
}

Careful: The delegate is invoked on a picker-internal queue. To perform any UI work, you must dispatch to the main UI thread.

Next steps