Barcode Scanner
Reads barcodes and QR codes from the camera or a picture, each code outlined where it is; with the browser's BarcodeDetector, or barcode-detector's where it has none.
Installation
It has its own entry point, @semantic-components/ui/barcode-scanner, built on barcode-detector — an optional peer of ui: install it where you use the component.
import { ScBarcodeScanner, ScBarcodeScannerVideo, ScBarcodeScannerOverlay, ScBarcodeScannerToggle, ScBarcodeScannerFile } from '@semantic-components/ui/barcode-scanner';Usage
Usage
The camera is off.
Point the camera at a code.
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScFrame } from '@semantic-components/ui';import { ScBarcodeScanner, ScBarcodeScannerOverlay, ScBarcodeScannerToggle, ScBarcodeScannerVideo,} from '@semantic-components/ui/barcode-scanner';import { SiCameraIcon, SiCameraOffIcon } from '@semantic-icons/lucide-icons';Anatomy
The scanner holds your markup: a frame (its size, its corners) with the video and the outlines in it, the button that turns the camera on, a file input for a picture. What each state means is yours to say.
import { ScBarcodeScanner, ScBarcodeScannerFile, ScBarcodeScannerOverlay, ScBarcodeScannerToggle, ScBarcodeScannerVideo,} from '@semantic-components/ui/barcode-scanner';<div scBarcodeScanner #scanner="scBarcodeScanner" (detected)="read($event)"> <div scFrame class="relative aspect-4/3 overflow-hidden"> <video scBarcodeScannerVideo></video> <svg scBarcodeScannerOverlay></svg> </div> <button scBarcodeScannerToggle>…Scan or Stop, from scanner.active()…</button> <input type="file" scBarcodeScannerFile /></div>Reading
(detected) gives each new code — its text, its format, its corners. The same code still in view, or back within 1.5 seconds, is not new. To scan once, turn the camera off there, or bind [(active)]. scan(picture) reads a file or an image the same way.
state() says where it is: idle, starting, scanning — or why not: denied (the camera refused), unavailable (none), unsupported (nothing to read with: no BarcodeDetector, and barcode-detector not installed).
Browsers
It reads with the browser's BarcodeDetector, which Chromium has on Android and macOS. Where there is none — Firefox, Safari, Chromium on Windows and Linux — it loads barcode-detector's on the first scan, and only then: nothing is loaded where the browser has its own, and no global is set.
Its decoder, zxing-wasm's (about 1 MB), is served by your app, never by a CDN: copy it into your assets, where it's looked for (/zxing/zxing_reader.wasm).
// angular.json: the decoder, in the assets{ "glob": "zxing_reader.wasm", "input": "node_modules/zxing-wasm/dist/reader", "output": "zxing"} Elsewhere, say where it is with withBarcodeScanner().
// app.config.tsimport { withBarcodeScanner } from '@semantic-components/ui/barcode-scanner';
provideSemanticComponents( { preset: { style } }, withBarcodeScanner({ wasmUrl: '/assets/zxing_reader.wasm' }),);Accessibility
- Not everyone can aim a camera: offer a picture (a labelled
scBarcodeScannerFile), and a field to type the code. - Say each code read in a live region; the video and the outlines are hidden from screen readers.
- The toggle's name follows the state — Scan, Stop — and says what a press does.
- Say what a state means where the video is: the camera refused, none to use, a browser that can't read codes.
Examples
Scan once
import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, form } from '@angular/forms/signals';import { ScField, ScFrame, ScInput, ScLabel } from '@semantic-components/ui';import { type ScBarcode, ScBarcodeScanner, ScBarcodeScannerOverlay, ScBarcodeScannerToggle, ScBarcodeScannerVideo,} from '@semantic-components/ui/barcode-scanner';Formats
The camera is off.
Nothing read yet.
import { Component, ViewEncapsulation, computed, signal } from '@angular/core';import { FormField, form } from '@angular/forms/signals';import { ScField, ScFrame, ScRadio, ScRadioGroup,} from '@semantic-components/ui';import { type ScBarcodeFormat,From a picture
Nothing read yet.
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScButton, ScFrame, ScLabel } from '@semantic-components/ui';import { type ScBarcode, ScBarcodeScanner, ScBarcodeScannerFile,} from '@semantic-components/ui/barcode-scanner';
/** No camera needed: a picture — your file, or one of these — is read the same way. The way for anyone who cannot aim a camera. */@Component({RTL
الكاميرا متوقفة.
وجّه الكاميرا نحو رمز.
import { Dir } from '@angular/cdk/bidi';import { Component, LOCALE_ID, ViewEncapsulation, signal } from '@angular/core';import { ScFrame } from '@semantic-components/ui';import { ScBarcodeScanner, ScBarcodeScannerOverlay, ScBarcodeScannerToggle, ScBarcodeScannerVideo,} from '@semantic-components/ui/barcode-scanner';import { SiCameraIcon, SiCameraOffIcon } from '@semantic-icons/lucide-icons';API reference
Each part takes class, merged over its own (cn: a later class wins).
| Part | Selector | Description |
|---|---|---|
ScBarcodeScanner | [scBarcodeScanner] | Reads codes from the camera (active) or a picture (scan()); data-state. exportAs scBarcodeScanner: state(), codes() (in the frame now), scan(picture). |
ScBarcodeScannerVideo | video[scBarcodeScannerVideo] | The camera, the one at the back where there is one: it fills its frame (object-cover). Hidden from screen readers. |
ScBarcodeScannerOverlay | svg[scBarcodeScannerOverlay] | Each code in the frame, outlined where it is, over the video. Its colour is the text colour; hidden from screen readers. |
ScBarcodeScannerToggle | button[scBarcodeScannerToggle] | The camera on and off: the style’s button (variant, size). Disabled where the browser can’t read codes. Write its icon and name from active(). |
ScBarcodeScannerFile | input[type=file][scBarcodeScannerFile] | Reads the picture chosen: the style’s input, images only. Label it. |
| Input | Type | Default | Description |
|---|---|---|---|
active | boolean | false | Two-way: the camera on, scanning. Off again when refused or missing. |
formats | ScBarcodeFormat[] | [] | The ones to read ('qr_code', 'ean_13'…); none, all the browser reads. |
(detected) | ScBarcode | — | Each new code: rawValue, format, cornerPoints. |