Scanner
Camera-based code scanning. Decoding uses the native BarcodeDetector browser API (zero third-party decoding libraries); when the environment is unsupported, permission is denied, or the camera fails, the component automatically falls back to a manual input row — deterministic and testable.
Basic usage
active is a presence-boolean attribute: set it to request the camera and start preview scanning; remove it to stop and release the stream. The live-camera demo requires browser authorization (choose "Allow"), and is best experienced in a BarcodeDetector-capable browser (Chrome / Edge / Android WebView, etc.).
Driven by the active attribute (button-triggered, camera permission required)
const el = document.querySelector('oas-scanner')
el.start() // = setAttribute('active', ''): request the camera and start preview
el.stop() // = removeAttribute('active'): stop and release the streamstart()/stop() are imperative conveniences for the active attribute — state is always owned by the attribute; binding attributes directly from host frameworks (Vue/React) works identically.
Manual input mode
With manual present, a manual input row is shown (label-associated input; submit via Enter or the button). It does not conflict with camera preview: you can use manual-only (manual alone), or camera + manual together (as above) — users always have an input path when scanning fails.
manual input (no camera required, works in any environment)
Enter a code (e.g. OAS-2026-001) above, then press Enter or click "Confirm" — the component fires oas-scan and clears the input.
Manual submits share the same idempotency contract as camera decodes: repeated submits of the same value do not re-fire (see "Dedup and throttling").
Fallback chain and oas-error
Three failure classes fall back to manual mode automatically and fire oas-error once (per activation session per code; removing and re-setting active starts a new session):
detail.code | Scenario | Component behavior |
|---|---|---|
not-supported | No native BarcodeDetector (older Firefox / iOS Safari / test environments like happy-dom), or an insecure context (non-HTTPS/localhost, no getUserMedia) | Falls back to manual without requesting the camera |
permission | getUserMedia denied by user/system (NotAllowedError / SecurityError) | Falls back to manual; status shows permission denied |
camera | No camera device, device busy, play() rejected, or other camera errors | Falls back to manual; status shows camera failure |
oas-error / oas-scan event takeover
Fired oas-scan / oas-error events will appear here.
Dedup and throttling
oas-scan(detail{ value, format? }) is idempotent: the sameformat+valuefingerprint fires at most once per session; keeping the camera aimed at the same code does not re-trigger.start()(or removing then re-settingactive) opens a new session and resets the fingerprint, forcing a re-fire of the same code.- After a hit, frame sampling pauses for 1200 ms then resumes automatically (throttling) — brief occlusions or angle adjustments need no restart; with no hits, frames are sampled every 150 ms (video frame → offscreen canvas →
BarcodeDetector.detect()). - Manual submits fire
oas-scanwithoutformat; camera hits carry the format reported byBarcodeDetector(e.g.qr_code,ean_13).
Browser support (honest boundary)
BarcodeDetector is a newer native API: Chromium-based browsers (Chrome / Edge / Android WebView) and some platforms support it; Firefox and iOS Safari have long lacked it or ship it experimentally. The component feature-detects per environment; unsupported environments are not a broken experience — manual mode works out of the box. A pure-TS software decoder is out of scope for this release; hosts needing full-browser scanning should integrate a third-party decoding library and bridge it through the same oas-scan event protocol.
Accessibility
- The video area carries
role="img"with a built-inaria-label(Chinese「相机扫描画面」/ English "Camera scan preview"; overridable via thearia-labelattribute). - The status area carries
role="status": state changes (scanning / unsupported / permission denied / camera error) are announced by screen readers. - The manual row associates its
<label>to the input viafor;<form>semantics make Enter submit directly; the confirm button is keyboard-reachable, and the focus ring uses library tokens (falling back to a system-color outline under forced-colors).
API
oas-scanner
Attributes
| Attribute | Description | Type | Default |
|---|---|---|---|
active | Starts camera scanning (presence requests getUserMedia and begins preview); falls back to manual input when BarcodeDetector is unavailable, permission is denied, or the camera errors | boolean | — |
aria-label | Accessible name of the component, defaults to i18n | — | — |
manual | Always show the manual input row (also entered automatically when the camera or permission is unavailable) | boolean | — |
Methods
| Method | Signature | Description |
|---|---|---|
start | start(): void | Start camera scanning (equivalent to setting the active attribute) |
stop | stop(): void | Stop the camera and release the media stream (equivalent to removing the active attribute) |
Events
| Event | Description |
|---|---|
oas-error | Fired when camera or recognition capability is unavailable, detail { code }: not-supported (no BarcodeDetector) / permission (permission denied) / camera (camera error); the component falls back to manual input mode after firing |
oas-scan | Fired when a code is recognized, detail { value, format? }; consecutive identical fingerprints are not re-fired, and detection auto-resumes after a throttling delay |
CSS Variables
| CSS Variable | Default |
|---|---|
--oas-scanner-height | 220px |
Parts
root / viewport / video / status / manual / input / submit are customizable via ::part().