Integrate the Scandit Barcode Scanner into your app

Integrate the Scandit Barcode Scanner into your own app by following these steps:

Get the Scandit Barcode Scanner

The Scandit Barcode Scanner is available as the Scandit.BarcodePicker.Xamarin 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.

Alternatively, you can get the Scandit Barcode Scanner in the Xamarin Component Store. You can use the components walkthrough by Xamarin to help you with adding the component to your project.

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

Set the app key in your AppDelegate's FinishedLaunching method the following way:

@implementation AppDelegate
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
// Provide the app key for your scandit license.
License.SetAppKey(@"--- ENTER YOUR SCANDIT APP KEY HERE ---");
// ...
}

Add code to handle the scanning event

Implement the ScanDelegate protocol to handle the successful barcode decoding. The API reference of the ScanDelegate protocol provides more details about what is returned.

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

public class PickerScanDelegate : ScanDelegate {
UIViewController presentingViewController;
public PickerScanDelegate(UIViewController controller) {
presentingViewController = controller;
}
public override void DidScan (BarcodePicker picker, IScanSession session) {
NSArray recognized = session.NewlyRecognizedCodes;
Barcode code = recognized.GetItem<Barcode> (0);
// Add your own code to handle the barcode result e.g.
Console.WriteLine("scanned {0} barcode: {1}", code.Symbology, code.Data);
}
}

And then set the scan delegate on the picker.

Careful: Since a change around iOS 9 the delegates are only softly referenced and can be dereferenced and deallocated if the memory is low. To avoid this you should keep a reference to them until you no longer use the picker (for example by creating a class variable for the delegates in the presenting view controller).

scanDelegate = new PickerScanDelegate(this);
picker.ScanDelegate = scanDelegate;

If you are displaying the BarcodePicker modally and want to use the scanner's toolbar to cancel and return you will also need to implement the CancelDelegate.

public class OverlayCancelDelegate : CancelDelegate {
UIViewController presentingViewController;
public OverlayCancelDelegate(UIViewController controller) {
presentingViewController = controller;
}
public override void DidCancel(ScanOverlay overlay, NSDictionary status) {
Console.WriteLine ("Cancel was pressed.");
presentingViewController.DismissViewController (true, null);
}
}

And then set the cancel delegate on the overlay view.

cancelDelegate = new OverlayCancelDelegate(this);
picker.OverlayView.CancelDelegate = cancelDelegate;

Add code to start the scanning process

The scanning process is started by instantiating BarcodePicker, specifying the delegate that will receive the scan callback event and then starting the scanner.

// Configure the barcode picker through a scan settings instance by defining which
// symbologies should be enabled.
ScanSettings settings = ScanSettings.DefaultSettings ();
// prefer backward facing camera over front-facing cameras.
settings.CameraFacingDirection = CameraFacingDirection.Back;
// Enable symbologies that you want to scan.
settings.SetSymbologyEnabled (Symbology.EAN13, true);
settings.SetSymbologyEnabled (Symbology.UPC12, true);
settings.SetSymbologyEnabled (Symbology.QR, true);
// Setup the barcode scanner.
BarcodePicker picker = new BarcodePicker (settings);
// Set the delegate to receive scan events.
scanDelegate = new PickerScanDelegate(this);
picker.ScanDelegate = scanDelegate;
// Start the scanning process.
picker.StartScanning ();
// Show the scanner. The easiest way to do so is by presenting it modally.
PresentViewController (picker, true, null);

Next steps