Add AR Overlays in MatrixScan#

Prerequisites#

To proceed, you need to setup a project that uses MatrixScan first, check out this guide (you can ignore the bottom section about the 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.

var overlay = BarcodeTrackingAdvancedOverlay.forView(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.setWidgetForTrackedBarcode(), the function BarcodeTrackingAdvancedOverlayListener.widgetForTrackedBarcode() won’t be invoked for that specific barcode.

Using BarcodeTrackingAdvancedOverlayListener

@override
Widget widgetForTrackedBarcode(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.
  return ARWidget(trackedBarcode.barcode.data);
}

@override
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;
}

@override
PointWithUnit offsetForTrackedBarcode(BarcodeTrackingAdvancedOverlay overlay, TrackedBarcode trackedBarcode) {
  // 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 PointWithUnit(DoubleWithUnit(0, MeasureUnit.fraction), DoubleWithUnit(-1, MeasureUnit.fraction));
}

Using the setters in the overlay

The function BarcodeTrackingListener.didUpdateSession() 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.setWidgetForTrackedBarcode(), BarcodeTrackingAdvancedOverlay.setAnchorForTrackedBarcode() and BarcodeTrackingAdvancedOverlay.setOffsetForTrackedBarcode()

@override
void didUpdateSession(BarcodeTracking barcodeTracking, BarcodeTrackingSession session) {
  for (final trackedBarcode in session.addedTrackedBarcodes) {
    Widget arWidget = ARWidget(trackedBarcode.barcode.data);
    overlay.setWidgetForTrackedBarcode(arWidget, trackedBarcode);
    overlay.setAnchorForTrackedBarcode(Anchor.topCenter, trackedBarcode);
    overlay.setOffsetForTrackedBarcode(
        PointWithUnit(DoubleWithUnit(0, MeasureUnit.fraction), DoubleWithUnit(-1, MeasureUnit.fraction)),
        trackedBarcode);
  }
}

Provide your own custom implementation#

If you do not want to use the overlay, it is also possible to add augmented reality features based on the tracking identifier and the quadrilateral coordinates that every tracked barcode has. Below are some pointers.

Note

The frame coordinates from TrackedBarcode.location need to be mapped to view coordinates, using DataCaptureView.viewQuadrilateralForFrameQuadrilateral().

@override
void didUpdateSession(BarcodeTracking barcodeTracking, BarcodeTrackingSession session) {
  for (final lostTrackIdentifier in session.removedTrackedBarcodes) {
    // You now know the identifier of the tracked barcode that has been lost.
    // Usually here you would remove the views associated.
  }

  for (final trackedBarcode in session.addedTrackedBarcodes) {
    // Fixed identifier for the tracked barcode.
    var trackingIdentifier = trackedBarcode.identifier;

    // Current location of the tracked barcode.
    var location = trackedBarcode.location;
    view.viewQuadrilateralForFrameQuadrilateral(location).then((quadrilateral) => {
          // You now know the location of the tracked barcode.
        });
  }
}