Add AR Overlays in MatrixScan#

Prerequisites#

To proceed, you need to setup a project that uses matrix scan first, check out this guide (you can ignore the bottom section about visualization of tracked barcodes using BarcodeTrackingBasicOverlay).

Getting started#

There are two ways to add advanced AR overlays to a Data Capture View:

Note

  • The first way is the easiest, as it takes care of adding, removing and animating the overlay’s views whenever needed. It’s also flexible enough to cover the majority of use cases.

  • You can always handle touch events on the views you create like you normally would.

Using BarcodeTrackingAdvancedOverlay#

As mentioned above, the advanced overlay combined with its listener offers an easy way of adding augmentations to your DataCaptureView. In this guide we will add a view above each barcode showing its content.

First of all, create a new instance of BarcodeTrackingAdvancedOverlay and add it to the DataCaptureView.

BarcodeTrackingAdvancedOverlay overlay = BarcodeTrackingAdvancedOverlay.Create(barcodeTracking, dataCaptureView);

At this point, you have two options.

Note

The second way will take priority over the first one, which means that if a view for a barcode has been set using BarcodeTrackingAdvancedOverlay.SetViewForTrackedBarcode(), the function IBarcodeTrackingAdvancedOverlayListener.ViewForTrackedBarcode() won’t be invoked for that specific barcode.

Using IBarcodeTrackingAdvancedOverlayListener

public View ViewForTrackedBarcode(BarcodeTrackingAdvancedOverlay overlay, TrackedBarcode trackedBarcode)
{
    // Create and return the view you want to show for this tracked barcode. You can also return null, to have no view for this barcode.
    TextView textView = new TextView(this);
    textView.SetBackgroundColor(Color.White);
    textView.LayoutParameters = new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WrapContent,
            ViewGroup.LayoutParams.WrapContent);
    textView.Text = trackedBarcode.Barcode.Data;
    return textView;
}

public Anchor AnchorForTrackedBarcode(
            BarcodeTrackingAdvancedOverlay overlay,
            TrackedBarcode trackedBarcode)
{
    // As we want the view to be above the barcode, we anchor the view's center to the top-center of the barcode quadrilateral.
    // Use the function 'OffsetForTrackedBarcode' below to adjust the position of the view by providing an offset.
    return Anchor.TopCenter;
}

public PointWithUnit OffsetForTrackedBarcode(
            BarcodeTrackingAdvancedOverlay overlay,
            TrackedBarcode trackedBarcode,
            View view)
{
    // This is the offset that will be applied to the view.
    // You can use MeasureUnit.Fraction to give a measure relative to the view itself, the sdk will take care of transforming this into pixel size.
    // We now center horizontally and move up the view to make sure it's centered and above the barcode quadrilateral by half of the view's height.
    return new PointWithUnit(
            new FloatWithUnit(0f, MeasureUnit.Fraction),
            new FloatWithUnit(-1f, MeasureUnit.Fraction));
}

Using the setters in the overlay

The function IBarcodeTrackingListener.OnSessionUpdated() gives you access to a session, which contains all added, updated and removed tracked barcodes. From here you can create the view you want to display, and then call BarcodeTrackingAdvancedOverlay.SetViewForTrackedBarcode(), BarcodeTrackingAdvancedOverlay.SetAnchorForTrackedBarcode() and BarcodeTrackingAdvancedOverlay.SetOffsetForTrackedBarcode()

public void OnSessionUpdated(BarcodeTracking mode, BarcodeTrackingSession session, IFrameData data)
{
    // Be careful, this function is not invoked on the main thread!
    RunOnUiThread(() => {
        foreach (TrackedBarcode trackedBarcode in session.AddedTrackedBarcodes) {
            TextView textView = new TextView(this);
            textView.SetBackgroundColor(Color.White);
            textView.LayoutParameters =
                new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WrapContent,
                ViewGroup.LayoutParams.WrapContent);

            textView.Text = trackedBarcode.Barcode.Data;
            overlay.SetViewForTrackedBarcode(trackedBarcode, textView);
            overlay.SetAnchorForTrackedBarcode(trackedBarcode, Anchor.TopCenter);
            overlay.SetOffsetForTrackedBarcode(
                trackedBarcode,
                new PointWithUnit(
                    new FloatWithUnit(0f, MeasureUnit.Fraction),
                    new FloatWithUnit(-1f, MeasureUnit.Fraction)
                )
            );
        }
    });
}