For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /api/angular-lynx/hardware/barcode-scanner.md.

LynxBarcodeScanner

Reads a QR code or a barcode with the camera, using the phone's own scanner. import { LynxBarcodeScanner, BarcodeScannerError, BARCODE_FORMATS, type BarcodeFormat, type BarcodeScan, type BarcodeScanOptions, type BarcodeScannerErrorCode } from '@blotch/angular-lynx';

Turn it on

Add permissions.camera to the build target's options in angular.json, with the reason iOS shows when it asks for the camera:

angular.json
"options": {
  "permissions": { "camera": "Scan your ticket" }
}

What it adds:

  • iOS: NSCameraUsageDescription, the reason iOS's camera prompt shows, and the scanner's module.
  • Android: the scanner's module, and Google's code scanner from Google Play services. The scanner needs no permission, but the setting still declares CAMERA, which LynxImagePicker's camera asks for. The camera is declared not required, so Google Play still offers the app to devices without one.

It also turns on LynxImagePicker's camera.

Without it, the app has no scanner code at all, on iOS or Android, and scan() rejects with unavailable. That's on purpose: App Store Connect flags an app whose code uses the camera without saying why. On Android, the scanner also brings in Google Play services' libraries.

See Turn on device features for every setting.

Usage

const scanner = inject(LynxBarcodeScanner);

try {
  const code = await scanner.scan({ formats: ['qr'] });
  // { format: 'qr', value: 'WIFI:S:Home;T:WPA;P:…;;' }
  this.ticket.set(code.value);
} catch (error) {
  if (error instanceof BarcodeScannerError && error.code === 'canceled') return;
  throw error;
}

API

LynxBarcodeScanner

@Injectable({ providedIn: 'root' })
class LynxBarcodeScanner {}

Opens the platform's own scanner over the app, and closes it at the first code it reads, so the app doesn't draw a camera screen:

  • iOS: VisionKit's DataScannerViewController, in a sheet with a Cancel button. It needs iOS 16 and an A12 chip or later (iPhone XS and newer). iOS asks for the camera the first time, with permissions.camera's reason.
  • Android: Google's code scanner, which runs in Google Play services. Play services shows the camera and hands back only the code, so the app needs no camera permission. If the person has refused Play services the camera, scan() rejects with denied.
  • Android's first scan: Play services downloads the scanner the first time any app asks for it. A scan before that finishes rejects with unavailable.

Only the apps --platform builds, with ng build or ng serve, have it. Elsewhere scan() rejects with unavailable: in Lynx Explorer, the web preview and the iOS Simulator, which has no camera.

Under the testing library, a scan waits until the spec answers it with queueBarcodeScan(), cancelBarcodeScan() or failBarcodeScan().

LynxBarcodeScanner.scan()

scan(options?: BarcodeScanOptions): Promise<BarcodeScan>;

Opens the scanner and resolves with the first code it reads. Rejects with a BarcodeScannerError. One scan can be open at a time: starting another while one is open rejects with failed.

BarcodeScanOptions

type BarcodeScanOptions = {
  readonly formats?: readonly BarcodeFormat[];
};
OptionDescription
formatsThe kinds of code to read; the scanner ignores any other. Every format (BARCODE_FORMATS) when it's left out or empty

Name the formats you expect: fewer read faster and misread less.

BarcodeScan

type BarcodeScan = {
  readonly format: BarcodeFormat;
  readonly value: string;
};

value is what the code holds, as text: a URL, a ticket number, a Wi-Fi network's WIFI:S:…;;. Your app reads what it means.

BarcodeFormat

type BarcodeFormat =
  | 'qr'
  | 'aztec'
  | 'dataMatrix'
  | 'pdf417'
  | 'ean13'
  | 'ean8'
  | 'upcA'
  | 'upcE'
  | 'code39'
  | 'code93'
  | 'code128'
  | 'itf'
  | 'codabar';

The codes both platforms' scanners read. itf is ITF-14 and interleaved 2 of 5. iOS reads a UPC-A as an EAN-13 that starts with 0: with upcA in formats, such a code comes back as upcA with its 12 digits.

BarcodeScannerError

class BarcodeScannerError extends Error {
  readonly code: BarcodeScannerErrorCode;
}

type BarcodeScannerErrorCode = 'canceled' | 'unavailable' | 'denied' | 'failed';
codeWhen
canceledThe person closed the scanner
unavailableThe host has no scanner: no permissions.camera, an iPhone before iOS 16 or the A12, a phone without Google Play services, or not a --platform app
deniedThe person said no to the camera, or a restriction such as Screen Time blocks it. Send them to Settings to change it
failedThe scanner couldn't start or stopped, or a scan was already open

denied is about the app's camera on iOS. On Android it's about Google Play services' camera, since Play services shows it.