Errors and permissions
Camera failures are ordinary and expected: permission denied, no camera, camera already in
use - and an uploaded file may not be an image at all. The component classifies each one as a
BbScannerErrorKind so an application can respond without parsing message text, and
the permission can be checked before the browser ever asks.
Provoke a failure
An unknown DeviceId is requested as an exact constraint, so browsers
reject it with OverconstrainedError, which the component reports as
ConstraintsUnsatisfied.
RequestedWidth and RequestedHeight are sent as
ideal constraints, so an unreachable resolution is negotiated down rather
than refused; resolution alone never produces an error.
To see a permission denial, block camera access for this site in the browser and press Start normally.
GetCameraPermissionAsync asks the browser what a start would do, without
opening the camera or asking the user, 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. It returns a
BbCameraPermission:
- Prompt
- Starting will ask the user.
- Granted
- Starting will open the camera without asking.
- Denied
- Starting will fail with
PermissionDenied; only the user, in the site settings, can change that. - Unknown
- The browser will not say (an older Firefox, or a WebView without the Permissions API), or the page is not interactive yet; starting is the only way to find out.
Current state
Error kinds
A camera failure reaches OnError as a BbScannerException whose
Kind is one of these, Status becomes Faulted, the camera is
released, and the scanner shows the message. StartAsync tries again. An image handed to
ScanImageAsync throws the exception instead, since that call returns its results.
| Kind | Meaning | Suggested handling |
|---|---|---|
| PermissionDenied | The user or a policy refused the camera. | Explain why the camera is needed and offer a retry, since the browser will not re-prompt on its own. |
| NoCamera | No device matched the request. | Fall back to image scanning or manual entry. |
| CameraUnavailable | A device exists but is held by something else, or it stopped delivering frames - unplugged, or claimed by another application. | Ask the user to close other applications using the camera, then retry. |
| ConstraintsUnsatisfied | No camera can meet the requested constraints - typically a DeviceId that is gone. | Drop the DeviceId and start again with Facing, since the requested camera is gone. |
| NotSupported | The page has no camera API (some WebViews), is not a secure context, or could not load the component's JavaScript module. | Serve over HTTPS, or hide the scanner entirely on a browser or WebView that offers no camera. |
| InvalidImage | An image given to ScanImageAsync is not an image, is larger than MaxImageFileSize, decodes to too many pixels, or could not be decoded. | Tell the user which - too large, not an image, unreadable - and let them pick another. |
| Unknown | Anything the browser did not classify, or a decoder that failed on frame after frame. | Log the inner exception and offer a retry. |
<BbScanner OnError="HandleError" /> @code { private string? _problem; private void HandleError(BbScannerException error) => _problem = error.Kind switch { BbScannerErrorKind.PermissionDenied => "Allow the camera in your browser's site settings, then try again.", BbScannerErrorKind.NoCamera => "No camera found - scan a photo instead.", BbScannerErrorKind.CameraUnavailable => "Close other apps using the camera, then try again.", _ => error.Message, }; }
The message the scanner itself shows can be translated, per kind, through
BbScannerTexts.ErrorMessage - see customisation; OnError
still receives the exception with its own message.
Secure context and frames
Browsers only expose getUserMedia over HTTPS or on
localhost. Served any other way the component reports
NotSupported immediately, with a message that says exactly that,
instead of hanging on a permission prompt that will never appear. Still image scanning works
either way.
The render mode makes no difference: under Blazor Server the page opens the camera just as it does on
WebAssembly. In a .NET MAUI app with Bit.BbScanner.Maui the device's own camera is opened
instead, and the platform asks for it: a permission the app does not declare, or the user refuses, ends
in PermissionDenied, and a BlazorWebView not wrapped in a BbScannerHost in
NotSupported. Elsewhere a BlazorWebView is a secure context already, and one
that exposes no camera API at all is reported as NotSupported too.
A page inside a cross-origin <iframe> needs the camera delegated to it -
<iframe allow="camera"> - and a Permissions-Policy header that forbids the
camera makes every start fail with PermissionDenied.