# BbScanner (Component) `Bit.BbScanner.BbScanner` - inherits `ComponentBase` - implements `IAsyncDisposable`, `IComponent`, `IHandleAfterRender`, `IHandleEvent` A camera barcode scanner. The component owns the camera, the frame loop and one BbScannerBarcodeDecoder. Frames never enter the Blazor render tree: the video element paints itself, the decoder reads pixels out of a buffer JavaScript writes into directly, and the component only re-renders when something a user can see actually changes. A scanner running at twenty frames a second typically renders a handful of times a minute. It runs under every interactive render mode. On Blazor WebAssembly the frame loop and the decoder run in the browser, and JavaScript writes each frame straight into managed memory. Under Blazor Server and Blazor Hybrid the camera is still the page's, but .NET runs elsewhere: the page cuts each frame down to the luminance of the searched region and streams it across, and the decoder runs where .NET does - on the server, or natively in the app. Prerendered, it renders its markup and stays inert until it becomes interactive. ## Parameter - **AcceptResult** : `Func` - Decides which results are reported. A result it turns down is not reported, not outlined, not announced and does not count as a scan for BbScanner.AfterScan - the scanner simply keeps looking - so an application expecting, say, a ticket number can ignore every other code in view. Called on every frame that reads a symbol, before duplicate suppression, so it should be cheap. An exception it throws rejects the result and is routed to the renderer. - **AdditionalAttributes** : `IReadOnlyDictionary` - Any other attributes are applied to the root element. - **AfterScan** : `BbScannerAfterScan` = `BbScannerAfterScan.Continue` - What the scanner does once it has reported a scan: keep going, pause, or stop. - **AriaLabel** : `string` = `"Barcode scanner"` - Accessible name for the scanner region. - **AutoStart** : `bool` = `true` - Start the camera as soon as the component is ready. - **ChildContent** : `RenderFragment` - Arbitrary content rendered on top of the scanner. - **ControlsContent** : `RenderFragment` - Extra controls appended to the built-in control bar. - **CssClass** : `string` - Extra CSS classes for the root element. - **DeviceId** : `string` - A specific camera to open, from BbScanner.GetCamerasAsync. Changing it while the scanner is running switches cameras. - **DuplicateSuppressionWindow** : `TimeSpan?` - How long the same value is suppressed after being reported. When not set, the value from BbScanner.Options applies, and failing that two seconds. Timeout.InfiniteTimeSpan reports each value once for as long as the camera stays open - a stocktake, a check-in desk - remembering up to BbScannerDuplicateFilter.SessionCapacity values. BbScanner.StartAsync on a scanner with no camera open, after BbScanner.StopAsync or a fault, and BbScanner.ResetDuplicateFilter forget them; switching cameras, a camera reopened once the page is shown again and, unlike a finite window, BbScanner.Resume do not. - **EnableFileScanning** : `bool` = `true` - Offer the built-in still image scanning control. - **Facing** : `BbScannerCameraFacing` = `BbScannerCameraFacing.Rear` - Which camera to prefer when BbScanner.DeviceId is not set. - **Feedback** : `BbScannerScanFeedback` = `BbScannerScanFeedback.None` - A vibration, a beep, or both, each time a scan is reported. Off by default; see BbScannerScanFeedback for what browsers allow. - **Fill** : `bool` = `false` - Stretch to the size of the parent instead of keeping the camera's aspect ratio. The video is cropped to fill; overlays and highlights are mapped onto the visible part. - **Formats** : `BbScannerBarcodeFormat` = `BbScannerBarcodeFormat.Common` - The symbologies to look for: BbScannerBarcodeFormat.Common unless set. The others, PDF417 and Aztec among them, are asked for by name, since each costs every frame time of its own. - **HighlightResults** : `bool` = `true` - Outline a decoded symbol where it was found in the frame. - **MaxImageFileSize** : `long` = `33554432` - The largest image file, in bytes, the built-in file picker accepts; larger files are refused before they are decoded. Zero accepts any size. The default, 32 MB, holds any photograph a phone takes. The size of a file says little about the size of the image it decodes to, so an image whose header declares more than about 268 megapixels is refused before it is decoded as well, whatever this is set to. - **MaxProcessingSize** : `int` = `640` - Longest edge, in pixels, of the image the decoder actually sees. Frames are scaled down to this by the browser before they cross into managed code, which is the single most effective performance control the component exposes. - **MaxSymbolsPerFrame** : `int` = `1` - The most symbols to read from one frame. The default, one, reads the first symbol found and is the cheapest; more reads a sheet of labels or a shelf at once, each symbol reported through BbScanner.OnScan and outlined on its own. Applies to still images too. - **MessageTemplate** : `RenderFragment` - Replaces the rendering of status and error messages. - **OnCameraChanged** : `EventCallback` = `default` - Raised when a camera is opened, with what the browser negotiated. - **OnDiagnostics** : `EventCallback` = `default` - Raised at most twice a second with the timings of the most recent frame. Subscribing has no cost when nothing is listening. - **OnError** : `EventCallback` = `default` - Raised when the scanner fails to start or the camera is lost. - **OnScan** : `EventCallback` = `default` - Raised for every accepted scan, after duplicate suppression. - **OnStatusChanged** : `EventCallback` = `default` - Raised whenever BbScanner.Status changes. - **Options** : `BbScannerOptions` - Advanced tuning. When supplied, BbScanner.Formats, BbScanner.Region and an explicitly set BbScanner.DuplicateSuppressionWindow are still applied on top of it, so the common cases do not need this at all. - **Overlay** : `RenderFragment` - Replaces the built-in reticle entirely. - **Region** : `BbScannerScanRegion` = `BbScannerScanRegion.Full` - The part of the frame to search, in normalized coordinates. The default searches the whole frame; a centred square is the usual choice for an aiming user interface. Still images scanned through the file picker are always searched in full. - **ReleaseCameraWhenHidden** : `bool` = `false` - Close the camera while the page is hidden - another tab, a minimised window, a locked phone - and open it again when the page is shown. It saves the battery and puts the camera indicator out, at the cost of a moment's restart, and of the permission prompt again on a browser that did not remember the user's answer. Whatever this is set to, a camera the browser itself ends because the page went into the background, which phones do, is reopened when the page comes back rather than reported as lost. - **RequestedHeight** : `int` = `720` - Camera height to ask the browser for. - **RequestedWidth** : `int` = `1280` - Camera width to ask the browser for. The browser is free to negotiate something else. - **ShowControls** : `bool` = `true` - Show the built-in control bar. - **ShowMask** : `bool` = `true` - Dim the area outside BbScanner.Region. - **ShowReticle** : `bool` = `true` - Draw the corner reticle. Ignored when BbScanner.Overlay is supplied. - **ShowStatusBar** : `bool` = `true` - Show the built-in status bar. - **TargetFrameRate** : `int` = `15` - How many frames a second to decode. Higher costs battery for little benefit above about twenty. - **Texts** : `BbScannerTexts` - The texts the scanner shows and announces - its controls, status, notices, error messages and screen reader announcements - for a language other than English. Anything the set leaves out stays English. ## Property - **Camera** : `BbScannerCameraInfo` - What the browser negotiated for the open camera, or null. - **Cameras** : `IReadOnlyList` = `new List()` - Cameras discovered by the last call to BbScanner.GetCamerasAsync. - **Diagnostics** : `BbScannerScanDiagnostics` = `default` - Timings from the most recently decoded frame. - **FramesDecoded** : `long` = `0` - Number of camera frames decoded since the scanner was created. - **FramesWithSymbol** : `long` = `0` - Number of camera frames that contained a symbol, before duplicate suppression. - **IsTorchOn** : `bool` = `false` - Whether the torch is currently on. - **ProcessingSize** : `ValueTuple` = `default` - Size of the frames handed to the decoder, or zero before the camera has opened. - **Status** : `BbScannerStatus` = `BbScannerStatus.Idle` - Current lifecycle state. - **Zoom** : `double` = `0` - Current zoom value, or zero when zoom is not supported. ## Method - **DecodeAllInImage**(ReadOnlySpan pixels, int width, int height, BbScannerPixelFormat format, int maxSymbols = 16) : `IReadOnlyList` - Decodes every symbol in an image already loaded into a pixel buffer, up to maxSymbols, with the still image settings and the symbologies and options of this scanner. Raises no events. - **DecodeImage**(ReadOnlySpan pixels, int width, int height, BbScannerPixelFormat format) : `BbScannerBarcodeResult` - Decodes an image already loaded into a pixel buffer, for applications that source images themselves rather than through the built-in picker. The whole image is searched with the still image settings, using the symbologies and options of this scanner. - **DisposeAsync**() : `ValueTask` - **GetCameraPermissionAsync**() : `Task` - Asks the browser what it would do with a request for the camera, without making one: so a page can explain why it needs the camera before the prompt appears, or say how to allow it once it has been refused. - **GetCamerasAsync**() : `Task>` - Enumerates the available cameras. Browsers withhold camera labels until permission has been granted once, so calling this before BbScanner.StartAsync tends to return generic names. - **OpenFilePickerAsync**() : `Task` - Opens the file picker so the user can scan a still image. A browser opens a file picker for a script only while a click of the user's is fresh. Called from a click handler, that is always so on WebAssembly and in a WebView; under Blazor Server the click reaches the page again a network round trip later, which a strict browser may no longer count as the user's. The built-in Scan image button opens the picker in the page itself, so it is not affected. - **Pause**() : `void` - Stops decoding but keeps the camera open, so resuming is instant. - **ResetDuplicateFilter**() : `void` - Forgets the values already reported, so that each is reported again the next time it is read - after a stocktake is saved, say, under a Timeout.InfiniteTimeSpanBbScanner.DuplicateSuppressionWindow. - **Resume**() : `void` - Resumes decoding after BbScanner.Pause. The symbol that was in view when scanning paused is reported again, unless BbScanner.DuplicateSuppressionWindow is Timeout.InfiniteTimeSpan, whose whole point is that a value is reported once for as long as the camera stays open. - **ScanCurrentFrameAsync**(int maxSymbols = 16) : `Task>` - Scans the camera's current frame at the camera's own resolution, with the still image settings. The live scan reads frames scaled down to BbScanner.MaxProcessingSize to keep up with the camera; a code too dense or too far away for that is often still readable at full size, which is what this is for - a "capture" button beside the viewfinder, say. Raises no events. - **ScanImageAsync**(Stream image, int maxSymbols = 16, CancellationToken cancellationToken = null) : `Task>` - Scans an encoded image - the bytes of a PNG, JPEG, WebP or any format the browser decodes - such as a file from Blazor's InputFile. It is decoded by the page exactly as a file chosen from the built-in picker is, with the same size limits and EXIF orientation, and needs no camera, so no permission is asked for. Raises no events; the results are returned. Under Blazor Server the image is decoded in the browser too, so a stream the server holds is sent to the browser and its luminance comes back: an image the user uploads crosses the network twice. The built-in picker, which decodes the file where the user chose it, sends it once. - **ScanImageAsync**(ReadOnlyMemory image, string contentType = null, int maxSymbols = 16, CancellationToken cancellationToken = null) : `Task>` - Scans an encoded image held in memory; see BbScanner.ScanImageAsync. - **SetTorchAsync**(bool on) : `Task` - Turns the torch on or off where the camera supports it. - **SetZoomAsync**(double zoom) : `Task` - Sets the zoom where the camera supports it. - **StartAsync**(string deviceId = null) : `Task` - Opens the camera and starts decoding. Calling this while a previous start is still waiting for the permission prompt supersedes that start: only the most recent request ever ends up owning the camera. - **StopAsync**() : `Task` - Stops decoding and releases the camera. - **SwitchCameraAsync**(string deviceId) : `Task` - Switches to another camera without tearing the component down.