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.

const overlay = BarcodeTrackingAdvancedOverlay.withBarcodeTrackingForView(barcodeTracking, view);

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 BarcodeTrackingAdvancedOverlayListener.viewForTrackedBarcode() won’t be invoked for that specific barcode.

Using BarcodeTrackingAdvancedOverlayListener

// The component must be registered: `AppRegistry.registerComponent('ARView', () => ARView)` e.g. in index.js
class ARView extends BarcodeTrackingAdvancedOverlayView {
  render() {
    return <Text style={{backgroundColor: 'white'}}>{this.props.barcodeData}</Text>
  }
}

// ...

overlay.listener = {
  viewForTrackedBarcode: (overlay, 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 new ARView({barcodeData: trackedBarcode.barcode.data});
  },

  anchorForTrackedBarcode: (overlay, 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;
  },

  offsetForTrackedBarcode: (overlay, trackedBarcode) => {
    // This is the offset that will be applied to the view.
    // You can use .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 the view's height.
    return new PointWithUnit(
      new NumberWithUnit(0, MeasureUnit.Fraction),
      new NumberWithUnit(-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.setViewForTrackedBarcode(), BarcodeTrackingAdvancedOverlay.setAnchorForTrackedBarcode() and BarcodeTrackingAdvancedOverlay.setOffsetForTrackedBarcode()

didUpdateSession: (barcodeTracking, session) => {
  session.addedTrackedBarcodes.map(trackedBarcode => {
    let trackedBarcodeView = new ARView({barcodeData: trackedBarcode.barcode.data});

    this.overlay.setViewForTrackedBarcode(trackedBarcodeView, trackedBarcode);
    this.overlay.setAnchorForTrackedBarcode(Anchor.CenterTop, trackedBarcode);
    this.overlay.setOffsetForTrackedBarcode(
      new PointWithUnit(
        new NumberWithUnit(0, MeasureUnit.Fraction), new NumberWithUnit(-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 quadrilateral coordinates that every tracked barcode has. Below are some pointers.

  • Set a BarcodeTrackingListener on the barcode tracking

  • In the BarcodeTrackingListener.didUpdateSession() function fetch the added, updated and removed tracked barcodes.

  • Create and show the views for the added barcodes.

  • Remove the views for the lost barcodes.

  • Compare the updated barcodes of the current frame with the ones from the previous one, and change your visualizations accordingly. You should use TrackedBarcode.location which gives you an accurate location for the barcode.

  • There are some state transitions where animating from the previous location of a tracked barcode to its current position can end up with weird artifacts because the order of the location’s corners is not stable. If your visualization depends on the order of the corners (by for example drawing a rectangle connecting all corners), make sure that you query TrackedBarcode.shouldAnimateFromPreviousToNextState before animating. If the order of the corners were to change it is possible that an animation would flip the visualization, which is not what happened to the actual barcode. However, a visualization that just draws something in the center of the location does not depend on the the order of corners and can ignore this.

Note

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

didUpdateSession: (barcodeTracking, session) => {
  session.removedTrackedBarcodes.map(lostTrackIdentifier => {
    // You now know the identifier of the tracked barcode that has been lost.
    // Usually here you would remove the views associated.
  });

  session.addedTrackedBarcodes.map(trackedBarcode => {
    // Fixed identifier for the tracked barcode.
    const trackingIdentifier = trackedBarcode.identifier;

    // Current location of the tracked barcode.
    const location = trackedBarcode.location;
    view.viewQuadrilateralForFrameQuadrilateral(location)
      .then(quadrilateral => {
        // This boolean indicates if it's safe to animate the views to the current location.
        const shouldAnimate = trackedBarcode.shouldAnimateFromPreviousToNextState;

        // You now know the updated location of the tracked barcode.
        // Usually here you would animate the views to the retrieved location.
      });
  });
}