@page "/scan" Live playground · BbScanner

Live playground

Every parameter of <BbScanner> in one place. Changes apply to the running scanner without dropping the camera.

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. PDF417 stays inert until a specification symbol character table is registered; see the README for why.

@foreach (var format in Formats) { }

Capture

Advanced tuning

Everything here is a property of BbScannerOptions, passed through the Options parameter. Formats and Region are still applied on top of it.

Chrome

Programmatic control

The same operations the built-in controls perform, driven from the page.

@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 BbBarcodeFormat[] Formats = [ BbBarcodeFormat.QrCode, BbBarcodeFormat.DataMatrix, BbBarcodeFormat.Code128, BbBarcodeFormat.Code39, BbBarcodeFormat.Ean13, BbBarcodeFormat.Ean8, BbBarcodeFormat.UpcA, BbBarcodeFormat.UpcE, BbBarcodeFormat.Itf, BbBarcodeFormat.Pdf417, ]; private readonly List _results = []; private BbScanner? _scanner; private BbBarcodeFormat _formats = BbBarcodeFormat.All; private BbScanRegion _region = BbScanRegion.CenteredSquare(0.7); private double _regionFraction = 0.7; private BbCameraFacing _facing = BbCameraFacing.Rear; private BbScanDiagnostics _diagnostics; private BbScannerException? _error; private int _frameRate = 15; private int _processingSize = 640; private int _duplicateSeconds = 2; private bool _showControls = true; private bool _showStatus = true; private bool _showReticle = true; private bool _showMask = true; private bool _highlight = true; private void HandleScan(BbBarcodeResult 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(BbScanDiagnostics diagnostics) { _diagnostics = diagnostics; StateHasChanged(); } private void ToggleFormat(BbBarcodeFormat format, bool enabled) { _formats = enabled ? _formats | format : _formats & ~format; } private void SetFormats(BbBarcodeFormat formats) => _formats = formats; private void OnRegionChanged(ChangeEventArgs args) { _regionFraction = Math.Clamp(ToInt(args, 70) / 100.0, 0.2, 1.0); _region = _regionFraction >= 0.999 ? BbScanRegion.Full : BbScanRegion.CenteredSquare(_regionFraction); } private void OnFacingChanged(ChangeEventArgs args) { if (Enum.TryParse(args.Value?.ToString(), out var facing)) { _facing = facing; } } 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(BbBarcodeFormat format) => format switch { BbBarcodeFormat.QrCode => "QR Code", BbBarcodeFormat.DataMatrix => "Data Matrix", BbBarcodeFormat.Code128 => "Code 128", BbBarcodeFormat.Code39 => "Code 39", BbBarcodeFormat.Ean13 => "EAN-13", BbBarcodeFormat.Ean8 => "EAN-8", BbBarcodeFormat.UpcA => "UPC-A", BbBarcodeFormat.UpcE => "UPC-E", BbBarcodeFormat.Itf => "ITF", BbBarcodeFormat.Pdf417 => "PDF417 (needs table)", _ => format.ToString(), }; }