@page "/scan" Live playground · BbScanner

Live playground

The parameters of <BbScanner> in one place. Changes apply to the running scanner without dropping the camera, except where a setting says otherwise. Camera selection, AcceptResult, the texts and the overlays each have a page of their own: camera, lifecycle and customisation.

Live @if (_scanner?.Camera is { } camera) { @camera.Width×@camera.Height }
@* Appended to the built-in control bar. *@ @* Arbitrary content on top of the viewfinder. *@ @if (_lastFormat is not null) {
@_lastFormat
}

Formats

Narrowing the set is the cheapest speed-up available: an unused symbology costs nothing because its decoder is never created, and it is the main guard against a misread between similar linear symbologies.

@foreach (var format in Formats) { }

Capture

Advanced tuning

Everything here is a property of BbScannerOptions, passed through the Options parameter. Formats, Region and an explicitly set DuplicateSuppressionWindow are still applied on top of it, so the common cases never need it.

Try harder and inverted symbols cost time on every frame, so they are off for live scanning and on for still images. Reading right to left is cheap and needed for a symbol held upside down; scanning columns, for one held sideways, costs about as much as the row pass. Both are on by default. An add-on is always read into Metadata.Extension when it is there; requiring it only holds back a symbol whose add-on has not been read yet.

Chrome

What the component draws over the video. Each is a bool parameter, and each can be turned off to build your own interface around the scanner.

The Clear log button in the control bar is this page's own, added through ControlsContent; the badge naming the last symbology over the video is ChildContent.

Feedback

Feedback takes BbScannerScanFeedback.Vibrate, Beep or both. Browsers only sound a tone after the user has interacted with the page, and Safari does not vibrate, so the outline and the screen reader announcement always come too.

Programmatic control

The same operations the built-in controls perform, driven from the page through a @@ref to the scanner, plus ResetDuplicateFilter, which forgets the values already reported so the one in view is reported again.

@if (_error is not null) {
@_error.Kind — @_error.Message
}
@code { private BbScannerOptions _options = new(); private string _itfLengths = "default"; private bool _fill; private string? _lastFormat; /// /// Options are cloned before every edit, so the running decoder is reconfigured by a new /// instance rather than mutated underneath itself. /// private void Tune(Action edit) { var next = _options.Clone(); edit(next); next.Validate(); _options = next; } private void OnItfLengthsChanged(ChangeEventArgs args) { _itfLengths = args.Value?.ToString() ?? "default"; IReadOnlyCollection lengths = _itfLengths switch { "14" => [14], "any" => [], _ => [6, 8, 10, 12, 14, 16, 18, 20], }; Tune(o => o.ItfLengths = lengths); } private static readonly BbScannerBarcodeFormat[] Formats = [ BbScannerBarcodeFormat.QrCode, BbScannerBarcodeFormat.DataMatrix, BbScannerBarcodeFormat.Code128, BbScannerBarcodeFormat.Code39, BbScannerBarcodeFormat.Ean13, BbScannerBarcodeFormat.Ean8, BbScannerBarcodeFormat.UpcA, BbScannerBarcodeFormat.UpcE, BbScannerBarcodeFormat.Itf, BbScannerBarcodeFormat.Pdf417, BbScannerBarcodeFormat.Aztec, BbScannerBarcodeFormat.Code93, BbScannerBarcodeFormat.Codabar, BbScannerBarcodeFormat.DataBar, BbScannerBarcodeFormat.DataBarLimited, BbScannerBarcodeFormat.DataBarExpanded, BbScannerBarcodeFormat.MaxiCode, BbScannerBarcodeFormat.MicroQrCode, BbScannerBarcodeFormat.MicroPdf417, ]; private readonly List _results = []; private BbScanner? _scanner; private BbScannerBarcodeFormat _formats = BbScannerBarcodeFormat.All; private BbScannerScanRegion _region = BbScannerScanRegion.CenteredSquare(0.7); private double _regionFraction = 0.7; private BbScannerCameraFacing _facing = BbScannerCameraFacing.Rear; private BbScannerScanDiagnostics _diagnostics; private BbScannerException? _error; private int _frameRate = 15; private int _requestedWidth = 1280; private int _requestedHeight = 720; private int _processingSize = 640; private int _duplicateSeconds = 2; private int _symbolsPerFrame = 1; private BbScannerAfterScan _afterScan = BbScannerAfterScan.Continue; private bool _showControls = true; private bool _showStatus = true; private bool _showReticle = true; private bool _showMask = true; private bool _highlight = true; private bool _fileScanning = true; private bool _vibrate; private bool _beep; private BbScannerScanFeedback Feedback => (_vibrate ? BbScannerScanFeedback.Vibrate : BbScannerScanFeedback.None) | (_beep ? BbScannerScanFeedback.Beep : BbScannerScanFeedback.None); private void OnResolutionChanged(ChangeEventArgs args) { (_requestedWidth, _requestedHeight) = ToInt(args, 1280) switch { 640 => (640, 480), 1920 => (1920, 1080), _ => (1280, 720), }; } private void HandleScan(BbScannerBarcodeResult result) { _lastFormat = result.Format.ToString(); _results.Insert(0, result); if (_results.Count > 40) { _results.RemoveAt(_results.Count - 1); } StateHasChanged(); } private void HandleError(BbScannerException error) { _error = error; StateHasChanged(); } private void HandleStatus(BbScannerStatus status) { if (status != BbScannerStatus.Faulted) { _error = null; } StateHasChanged(); } private void HandleDiagnostics(BbScannerScanDiagnostics diagnostics) { _diagnostics = diagnostics; StateHasChanged(); } private void ToggleFormat(BbScannerBarcodeFormat format, bool enabled) { _formats = enabled ? _formats | format : _formats & ~format; } private void SetFormats(BbScannerBarcodeFormat formats) => _formats = formats; private void OnRegionChanged(ChangeEventArgs args) { _regionFraction = Math.Clamp(ToInt(args, 70) / 100.0, 0.2, 1.0); _region = _regionFraction >= 0.999 ? BbScannerScanRegion.Full : BbScannerScanRegion.CenteredSquare(_regionFraction); } private void OnFacingChanged(ChangeEventArgs args) { if (Enum.TryParse(args.Value?.ToString(), out var facing)) { _facing = facing; } } private void OnAfterScanChanged(ChangeEventArgs args) { if (Enum.TryParse(args.Value?.ToString(), out var afterScan)) { _afterScan = afterScan; } } private Task OpenPicker() => _scanner is null ? Task.CompletedTask : _scanner.OpenFilePickerAsync(); private static int ToInt(ChangeEventArgs args, int fallback) => int.TryParse(args.Value?.ToString(), out var value) ? value : fallback; private static string Label(BbScannerBarcodeFormat format) => format switch { BbScannerBarcodeFormat.QrCode => "QR Code", BbScannerBarcodeFormat.DataMatrix => "Data Matrix", BbScannerBarcodeFormat.Code128 => "Code 128", BbScannerBarcodeFormat.Code39 => "Code 39", BbScannerBarcodeFormat.Ean13 => "EAN-13", BbScannerBarcodeFormat.Ean8 => "EAN-8", BbScannerBarcodeFormat.UpcA => "UPC-A", BbScannerBarcodeFormat.UpcE => "UPC-E", BbScannerBarcodeFormat.Itf => "ITF", BbScannerBarcodeFormat.Pdf417 => "PDF417", BbScannerBarcodeFormat.Aztec => "Aztec", BbScannerBarcodeFormat.Code93 => "Code 93", BbScannerBarcodeFormat.DataBar => "GS1 DataBar", BbScannerBarcodeFormat.DataBarLimited => "DataBar Limited", BbScannerBarcodeFormat.DataBarExpanded => "DataBar Expanded", BbScannerBarcodeFormat.MaxiCode => "MaxiCode", BbScannerBarcodeFormat.MicroQrCode => "Micro QR", BbScannerBarcodeFormat.MicroPdf417 => "MicroPDF417", _ => format.ToString(), }; }