Skip to content

Camera, torch and zoom

Camera selection, torch and zoom are all capabilities of the running media track. The component reports what the browser says is available and never offers a control the hardware will refuse: its own control bar shows a camera picker only when there is more than one camera, and a torch button and a zoom slider only when the open camera supports them.

Live Idle
Ready

Available cameras

None listed yet. Browsers withhold the list, and camera labels, until permission has been granted at least once.

Enumerate calls GetCamerasAsync, and Use SwitchCameraAsync, which reopens the scanner on that camera without tearing the component down. Binding the DeviceId parameter does the same. A camera the browser gives no name - every one, before permission - is listed as BbScannerTexts.UnnamedCamera, "Camera 1" in English.

Negotiated track

The BbCameraInfo that OnCameraChanged delivers each time a camera opens, also readable as the scanner's Camera property: what the browser actually negotiated, which may differ from RequestedWidth and RequestedHeight.

No camera open.

Controls

The torch button calls SetTorchAsync and the slider SetZoomAsync; IsTorchOn and Zoom read the current state back. Torch and zoom are almost always rear-camera-only, and support varies by browser, so both calls return whether the browser accepted them rather than throwing, and a zoom outside the range is clamped to it.

Scan full frame calls ScanCurrentFrameAsync, which reads the current frame at the camera's own resolution with the still image settings - for a code too dense or too far away for the live frames, which are scaled down to MaxProcessingSize. It returns every symbol it finds and raises no event, which makes it a good "capture" button.

In code

A camera picker of your own: list the cameras once the scanner has opened one - so their labels are readable - and bind the choice to DeviceId.

<BbScanner @ref="_scanner" DeviceId="@_deviceId" OnCameraChanged="OnCameraChanged" />

<select @bind="_deviceId">
    @foreach (var camera in _cameras)
    {
        <option value="@camera.DeviceId">@camera.Label</option>
    }
</select>

@code {
    private BbScanner _scanner = default!;
    private IReadOnlyList<BbCameraDevice> _cameras = [];
    private string? _deviceId;
    private bool _canTorch;   // offer a torch button of your own only when there is a torch

    private async Task OnCameraChanged(BbCameraInfo camera)
    {
        _canTorch = camera.SupportsTorch;
        _cameras = await _scanner.GetCamerasAsync();
    }
}
An unhandled error has occurred. Reload 🗙