Skip to content

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)

Start camera scanningStopClick "Start camera scanning" to request camera permission and begin preview
js
const el = document.querySelector('oas-scanner')
el.start()   // = setAttribute('active', ''): request the camera and start preview
el.stop()    // = removeAttribute('active'): stop and release the stream

start()/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.codeScenarioComponent behavior
not-supportedNo 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
permissiongetUserMedia denied by user/system (NotAllowedError / SecurityError)Falls back to manual; status shows permission denied
cameraNo camera device, device busy, play() rejected, or other camera errorsFalls 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 same format+value fingerprint fires at most once per session; keeping the camera aimed at the same code does not re-trigger. start() (or removing then re-setting active) 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-scan without format; camera hits carry the format reported by BarcodeDetector (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-in aria-label (Chinese「相机扫描画面」/ English "Camera scan preview"; overridable via the aria-label attribute).
  • 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 via for; <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 ​

AttributeDescriptionTypeDefault
activeStarts camera scanning (presence requests getUserMedia and begins preview); falls back to manual input when BarcodeDetector is unavailable, permission is denied, or the camera errorsboolean—
aria-labelAccessible name of the component, defaults to i18n——
manualAlways show the manual input row (also entered automatically when the camera or permission is unavailable)boolean—

Methods ​

MethodSignatureDescription
startstart(): voidStart camera scanning (equivalent to setting the active attribute)
stopstop(): voidStop the camera and release the media stream (equivalent to removing the active attribute)

Events ​

EventDescription
oas-errorFired 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-scanFired 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 VariableDefault
--oas-scanner-height220px

Parts ​

root / viewport / video / status / manual / input / submit are customizable via ::part().

Released under the MIT OR Apache-2.0 License.