BarcodeDetector API example
BarcodeDetector API example. Detect QR codes and barcodes from a camera frame with the OS shape-detection stack.
The problem
A page that reads a QR usually ships a decoder. WASM, a worker, a model. Face landmark in this lab is that path. You pay the download so the scan can run.
The method
BarcodeDetector is Shape Detection. The OS already has a scanner.
getSupportedFormats() is this machine, not Chrome in general. Windows can expose the constructor
and still return nothing. detect() takes an ImageBitmapSource and returns
boundingBox, cornerPoints, format, rawValue.
What to do
- Run
getSupportedFormats(). An empty list means the constructor is a stub. - Construct. Point a code at the camera, or pick a still. Run
detect().
getSupportedFormats()
Static. The formats this platform can actually read. Presence of BarcodeDetector is not enough.
await BarcodeDetector.getSupportedFormats()
new BarcodeDetector()
Omit formats to search everything the OS listed. Pass one format from the select to restrict.
Changing the select drops the instance.
const detector = new BarcodeDetector()
const qr = new BarcodeDetector({ formats: ["qr_code"] })
detect()
One shot on the stage. A live <video> or a still. The overlay is cornerPoints,
not a product UI.
const barcodes = await detector.detect(source)
barcodes[0].format
barcodes[0].rawValue
barcodes[0].boundingBox
barcodes[0].cornerPoints