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:
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, whichLynxImagePicker'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
API
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, withpermissions.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 withdenied. - 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()
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
Name the formats you expect: fewer read faster and misread less.
BarcodeScan
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
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
denied is about the app's camera on iOS. On Android it's about Google Play services' camera, since Play services shows it.
