Barcode AR Overlays

To deliver more personalized visual feedback when scanning barcodes, override the native visual feedback function.

This can be achieved using the EnableBarcodeOverlays method by passing an implementation of the ICommonBarcodeOverlayListener object as a reference.

ICommonBarcodeOverlayListener

Interface for a callback to be invoked on CommonOverlayView lifecycle events.

Methods:

Enabling Barcode Overlays

List<CommonOverlayView> OnCreate(CommonVisibleBarcode commonVisibleBarcode)

Required - called every time a CommonVisibleBarcode that is not contained on current overlays is discovered. Must return a list of CommonOverlayView that will be placed near the barcode.

void OnUpdate(List<CommonOverlayView> overlayViews, CommonVisibleBarcode commonVisibleBarcode)

Optional - called every time a CommonVisibleBarcode needs to be repositioned.

void OnRemove(List<CommonOverlayView> overlayViews, CommonVisibleBarcode commonVisibleBarcode)

Optional - called every time a CommonVisibleBarcode is no longer detected after the interval defined on GetClearTimeoutMills and shall be disposed.

Setting Overlay Timeouts

long GetClearTimeoutMills()

Optional - sets the max duration in milliseconds an overlay will persist on the screen after it is not detected by the scanner. Should be a value between 500 and 5000. If used, should be set before calling EnableBarcodeOverlays() method.

Default value - 1000

Avoid making long operations inside OnCreate, OnUpdate and OnRemove. Due to the nature of their successive calls, the overlay performance can be dramatically affected by the completion period.
Adding simple clickable framed barcode overlay
public class BarcodeOverlayListenerImpl: ICommonBarcodeOverlayListener
{
    public List<CommonOverlayView> OnCreate(CommonVisibleBarcode commonVisibleBarcode)
    {
        var context = Application.Current?.Handler?.MauiContext;
        var imageButton = new ImageButton()
        {
            Aspect = Aspect.AspectFit,
            Source = "dotnet_bot.png",
            WidthRequest = 50,
            HeightRequest = 50,
            BackgroundColor = Colors.Transparent
        };
        imageButton.Clicked += (s, e) =>
        {
            System.Diagnostics.Debug.WriteLine(commonVisibleBarcode.Value + " barcode clicked.");

        };
        var buttonViewHandler = imageButton.ToHandler(context);
        var buttonOverlayView = new CommonOverlayView(
            viewHandler: buttonViewHandler,
            nativeView: buttonViewHandler.PlatformView,
            overlayConfig: new OverlayConfig()
            {
                Anchor = OverlayAnchorConfig.Center,
                SizeDimension = new OverlayDimensionConfig()
                {
                    ScaleX = new OverlayScaleConfig() {
                        ScaleValue = 1,
                        ScaleType = OverlayScaleTypeConfig.Overlay
                    },
                    ScaleY = new OverlayScaleConfig() {
                        ScaleValue = 1,
                        ScaleType = OverlayScaleTypeConfig.Overlay
                    }
                }
            }
        );
        return [buttonOverlayView];
    }
}
Enabling barcode overlays
  • Android

  • iOS

scanView.ScanViewPlugin.ActiveScanViewPlugin.First().EnableBarcodeOverlays(new BarcodeOverlayListenerImpl());
scanView.EnableBarcodeOverlays(new BarcodeOverlayListenerImpl());

Disabling BarcodeOverlays

Once the overlays are no longer required you can call DisableBarcodeOverlays().

  • Android

  • iOS

scanView.ScanViewPlugin.ActiveScanViewPlugin.First().DisableBarcodeOverlays();
scanView.DisableBarcodeOverlays();

CommonVisibleBarcode

A class that contains general barcode information.

Methods:

String Value

Returns the decoded barcode value.

Barcode Barcode

Returns whole information of barcode scan result (as described in Anyline.SDK.NET.Common.Result.Barcode).

System.Drawing.Point[] Coordinates

Returns the coordinates and dimensions of detected barcode image in pixels.

Lazy<MemoryStream> BarcodeImageMemoryStream

Returns a MemoryStream object containing the cropped barcode image bitmap.

Action OnInvalidate

The Action to be executed when the CommonVisibleBarcode is invalidated.

double GetArea()

Returns the Area (pixel x pixel) of the coordinates' outer rect.

void Invalidate()

Invalidates the current views associated with this barcode and triggers a new call to ICommonBarcodeOverlayListener.OnCreate passing this CommonVisibleBarcode as reference.

Switching overlays depending on the barcode size
public class BarcodeOverlayListenerImpl: ICommonBarcodeOverlayListener
{
    private const int MaxSmallArea = 12000;
    private enum OverlayType
    {
        Small,
        Big
    }
    private OverlayType _currentOverlayType = OverlayType.Small;

    private static OverlayType GetOverlayTypeFromVisibleBarcode(CommonVisibleBarcode commonVisibleBarcode)
    {
        return commonVisibleBarcode.GetArea() > MaxSmallArea ? OverlayType.Big : OverlayType.Small;
    }

    public List<CommonOverlayView> OnCreate(CommonVisibleBarcode commonVisibleBarcode)
    {
        var context = Application.Current?.Handler?.MauiContext;

        //stores a value of OverlayTypeSmall or OverlayTypeBig
        _currentOverlayType = GetOverlayTypeFromVisibleBarcode(commonVisibleBarcode);

        VisualElement element;
        // create an instance of ImageButton from small barcode detections
        // or an instance of Label for bigger barcode detections
        if (_currentOverlayType == OverlayType.Small)
        {
            element = new ImageButton()
            {
                Aspect = Aspect.AspectFit,
                Source = "dotnet_bot.png",
                WidthRequest = 50,
                HeightRequest = 50,
                BackgroundColor = Colors.Transparent
            };
        }
        else
        {
            element = new Label()
            {
                Text = commonVisibleBarcode.Value,
                HorizontalTextAlignment = TextAlignment.Center,
                VerticalTextAlignment = TextAlignment.Center,
                FontAttributes = FontAttributes.Bold,
                WidthRequest = 50,
                HeightRequest = 50,
                TextColor = Colors.White,
                BackgroundColor = Colors.Black,
            };
        }
        var viewHandler = element.ToHandler(context);
        var buttonOverlayView = new CommonOverlayView(
            viewHandler: viewHandler,
            nativeView: viewHandler.PlatformView,
            overlayConfig: new OverlayConfig()
            {
                Anchor = OverlayAnchorConfig.Center,
                SizeDimension = new OverlayDimensionConfig()
                {
                    ScaleX = new OverlayScaleConfig() {
                        ScaleValue = 1,
                        ScaleType = OverlayScaleTypeConfig.Overlay
                    },
                    ScaleY = new OverlayScaleConfig() {
                        ScaleValue = 1,
                        ScaleType = OverlayScaleTypeConfig.Overlay
                    }
                }
            }
        );
        return [buttonOverlayView];
    }

    public void OnUpdate(List<CommonOverlayView> overlayViews, CommonVisibleBarcode commonVisibleBarcode) {
        //OnUpdate() is called every time a previously detected barcode needs to be repositioned.
        var oldOverlayType = _currentOverlayType;
        var newOverlayType = GetOverlayTypeFromVisibleBarcode(commonVisibleBarcode);
        if (oldOverlayType != newOverlayType) {
            // calling Invalidate() makes the overlay to be disposed
            // and a new OnCreate() will be called for a new view instance
            commonVisibleBarcode.Invalidate();
        }
    }
}

CommonOverlayView

Class definition for a callback to provide CommonVisibleBarcode placement information.

Methods:

CommonOverlayView(IPlatformViewHandler viewHandler, IView nativeView, OverlayConfig overlayConfig)

Required - constructor that receives an IView to be placed according to OverlayConfig.

OverlayConfig

Configuration for barcode overlays placement.

Methods:

OverlayAnchorConfig GetAnchor()

Optional - returns the desired OverlayAnchorConfig for a CommonOverlayView

Value

TopLeft

TopCenter

TopRight

CenterLeft

Center

CenterRight

BottomLeft

BottomCenter

BottomRight

Default value - Center (anchored to the center of the barcode)

OverlayDimensionConfig GetSizeDimension()

Optional - returns the desired size using OverlayDimensionConfig for a CommonOverlayView

Default value - detected barcode width and height

OverlayDimensionConfig GetOffsetDimension()

Optional - returns the desired offset using OverlayDimensionConfig for a CommonOverlayView

Default value - no offset

OverlayDimensionConfig

Class containing OverlayScaleConfig for X and Y.

OverlayScaleConfig ScaleX

OverlayScaleConfig ScaleY

Parameters - OverlayScaleConfig for X and Y axes.

OverlayScaleConfig

Class containing scale information for a single axe.

float ScaleValue

OverlayScaleTypeConfig ScaleType

Parameters - a float value to be used with the OverlayScaleTypeConfig for positioning overlays.

OverlayScaleTypeConfig

Enum class containing possible values for scaling overlays.

Value

Description

None

No scaling

Overlay

Scale based on barcode size

FixedPx

Fixed size in pixels

KeepRatio

Keep original view size ratio