Getting started
One package and one namespace - there are no services to register, no script tag and no stylesheet link. Put the scanner on an interactive page - WebAssembly, Server, Auto or Hybrid - and it opens the camera by itself; everything after the camera hands over its pixels is managed code.
Install
In a Blazor Web App, add the package to the project the scanning pages render in: the server project
for InteractiveServer pages, the .Client project for
InteractiveWebAssembly or InteractiveAuto ones, whose assembly has to be
downloaded to the browser.
dotnet add package Bit.BbScanner
Then add the namespace to _Imports.razor:
@using Bit.BbScanner
That is the whole setup. The package's Blazor initializer adds
_content/Bit.BbScanner/bit-bbscanner.css to the page, and the component imports its own
JavaScript module the first time it renders. Linking the stylesheet yourself also works - the
injection then stands down.
A first scanner
The camera starts as soon as the component is ready (AutoStart is on by default) and the
browser asks for permission the first time. The scanner prerenders safely on the server and starts once
the page is interactive. Any interactive render mode will do - InteractiveServer or
InteractiveAuto in place of the one below; in a standalone Blazor WebAssembly app or a
Blazor Hybrid one leave the @rendermode line out, since everything there is interactive
already.
@page "/scan" @rendermode InteractiveWebAssembly <BbScanner Formats="BbBarcodeFormat.QrCode" Region="BbScanRegion.CenteredSquare(0.7)" OnScan="HandleScan" OnError="HandleError" /> @if (_error is not null) { <p role="alert">@_error.Message</p> } @code { private BbScannerException? _error; private void HandleScan(BbBarcodeResult result) { _error = null; Console.WriteLine($"{result.Format}: {result.Text}"); } private void HandleError(BbScannerException error) => _error = error; }
Everything below comes with the component and needs no code: the status bar (top), a dimmed surround and a corner reticle marking the scan region, an outline drawn round each symbol it reads, and a control bar with Pause, Stop/Start and Scan image - plus a camera picker, a torch button and a zoom slider, each only when the device offers one. Drop an image file onto the scanner to scan it without the camera.
Last scan
No camera to hand? Use image scanning, or open the sample barcodes on another screen.
Where it runs
The camera is always opened by the page - the browser, or the WebView of a Blazor Hybrid app - and only
in a secure context: a page served over HTTPS, or from localhost. What the hosting model
decides is where .NET runs, and so where the frames are decoded.
| Hosting model | What to do |
|---|---|
| Blazor WebAssembly | Nothing else: put <BbScanner /> on a page. The fastest path - each frame crosses into managed memory once, through a [JSImport] memory view, and is decoded in the browser. |
| Blazor Web App | Put the package in the project the scanning pages render in: the .Client project for InteractiveWebAssembly and InteractiveAuto, the server project for InteractiveServer. Under Auto the first visit scans on the circuit and later ones in the browser; no page needs @rendermode InteractiveWebAssembly of its own. |
| Blazor Server | The frame loop and the decoder run on the server. The page sends only the luminance of each frame's searched region - about 63 KB for a 640×360 frame through CenteredSquare(0.7), against about 900 KB of RGBA - in slices under SignalR's 32 KB message limit, so the limit can stay as it is. Set a Region, and on a slow link lower TargetFrameRate and MaxProcessingSize. If the circuit is gone for 20 seconds, the page closes the camera. |
| Blazor Hybrid | In a .NET MAUI app, Bit.BbScanner.Maui opens the device's own camera - CameraX, AVFoundation, MediaCapture - and its frames go straight to the decoder in the app; builder.UseBbScanner(), the BlazorWebView wrapped in a BbScannerHost, which draws the preview behind it, and the camera permission on each platform: CAMERA on Android, NSCameraUsageDescription on iOS and Mac Catalyst, the webcam capability on Windows. In WPF and Windows Forms the scanner opens WebView2's camera. |
Lazy loading on WebAssembly
There is nothing to register, so a WebAssembly app can fetch BbScanner only when someone opens a page with a scanner on it. Mark the assembly as lazy loaded in the project that runs in the browser, and load it before that page first renders:
<ItemGroup> <BlazorWebAssemblyLazyLoad Include="Bit.BbScanner.wasm" /> </ItemGroup>
@* App.razor: the router waits for OnNavigateAsync before it renders the page. *@ @using Microsoft.AspNetCore.Components.WebAssembly.Services @inject LazyAssemblyLoader LazyAssemblyLoader <Router AppAssembly="typeof(App).Assembly" OnNavigateAsync="OnNavigateAsync"> ... </Router> @code { private async Task OnNavigateAsync(NavigationContext context) { if (context.Path == "scan") await LazyAssemblyLoader.LoadAssembliesAsync(["Bit.BbScanner.wasm"]); } }
The stylesheet is still injected at startup, since the package's Blazor initializer does not wait for the
assembly, and the component imports its JavaScript module the first time it renders. Only the pages
loaded after the assembly may use BbScanner's types - not a layout, a component on every page or
Program.cs. Do not lazy load in an AOT build: AOT compiles every assembly into
dotnet.native.wasm, so there is nothing left to download later, and lazy loading an
AOT-compiled assembly can fail at startup. Since camera scanning wants AOT, lazy loading suits an app that
reads still images or scans rarely.
How it works
The browser opens the camera and copies pixels; every stage after that is C#, and reuses its buffers from frame to frame.
| Stage | Where | What happens |
|---|---|---|
| Capture | Browser | getUserMedia feeds a <video>; a canvas scales frames to the processing size. |
| Transfer | Boundary | On WebAssembly, one JSImport call copies RGBA into a reused managed buffer. Under Server and Hybrid the page sends the searched region's luminance instead, in slices of 30 KB. |
| Luminance | C# | Fixed point BT.601 weights, with optional subsampling in the same pass. |
| Binarise | C# | Adaptive 8×8 block thresholds for matrix codes, per-row histograms for linear ones. |
| Detect | C# | Finder pattern search, perspective transform and grid sampling. |
| Decode | C# | Reed-Solomon correction, de-interleaving and bit stream parsing. |
Where to go next
Narrow Formats to the symbologies you need - it is the single most effective speed-up -
then aim with a region and tune the rest in the playground. Each page of these docs is a working scanner
that shows one part of the component.