<p align="center">
  <a href="https://github.com/qrcode-ai-com/qrcode-barcode-scanner-rust">
    <picture>
      <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/qrcode-ai-com/.github/main/profile/assets/banner-browser-dark.gif?v=74f967bc6d23">
      <img src="https://raw.githubusercontent.com/qrcode-ai-com/.github/main/profile/assets/banner-browser-light.gif?v=1ab06a2a50e5" alt="QR Code &amp; Barcode Scanner for the browser: the JavaScript and TypeScript logo, the quick start in the page, and a QR code and a barcode being read" width="100%">
    </picture>
  </a>
</p>

# Examples

<!-- qrcode-ai-readme-actions:v2 -->

### Ask AI

[![ChatGPT](https://raw.githubusercontent.com/qrcode-ai-com/qrcode-barcode-scanner-rust/main/docs/assets/actions/chatgpt-v2.svg)](https://chatgpt.com/?q=Help%20me%20understand%20Examples%20%28crates%2Fqrcode-ai-scanner-wasm%2Fexamples%2FREADME.md%29%20for%20QR%20Code%20%26%20Barcode%20Scanner%2C%20the%20open-source%20scanner.%20Source%20documentation%3B%20publication%20is%20not%20implied.%20Provided%20README%20context%20%28verify%20against%20the%20sources%29%3A%20These%20examples%20show%20how%20the%20installed%20browser%20package%20is%20used%20without%20a%20framework%3A%20import%20an%20image%2C%20run%20decoding%20in%20a%20module%20worker%2C%20or%20connect%20the%20camera%20controller%20to%20its%20overlay.%20They%20are%20runnable%20package%20examples%2C%20not%20an%20additional%20scanner%20SDK.%20Serve%20the%20package%20as%20described%20below%20and%20keep%20secure-context%20and%20camera-permission%20requirements%20separate%20from%20image-decoding%20failures.%20Read%20the%20public%20documentation%20below.%20Explain%20what%20this%20destination%20does%2C%20its%20documented%20installation%20or%20access%20steps%2C%20a%20minimal%20example%20if%20supported%2C%20prerequisites%2C%20limitations%20and%20license.%20Distinguish%20published%20packages%20from%20working%20source%20and%20preparation.%20Cite%20your%20sources%3B%20if%20a%20page%20is%20unavailable%20or%20a%20fact%20cannot%20be%20checked%2C%20say%20so.%20Do%20not%20invent%20installation%20commands%2C%20benchmark%20results%20or%20comparisons.%20Answer%20in%20my%20language.%20Public%20sources%3A%20https%3A%2F%2Fgithub.com%2Fqrcode-ai-com%2Fqrcode-barcode-scanner-rust%2Fblob%2Fmain%2Fcrates%2Fqrcode-ai-scanner-wasm%2Fexamples%2FREADME.md%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fqr-code-scanner%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fbarcode-scanner%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fqr-code-scanner%2Fsdk%2Fjavascript%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fbarcode-scanner%2Fsdk%2Fjavascript%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fqr-code-generator%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fbarcode-generator)
[![Claude](https://raw.githubusercontent.com/qrcode-ai-com/qrcode-barcode-scanner-rust/main/docs/assets/actions/claude-v2.svg)](https://claude.ai/new?q=Help%20me%20understand%20Examples%20%28crates%2Fqrcode-ai-scanner-wasm%2Fexamples%2FREADME.md%29%20for%20QR%20Code%20%26%20Barcode%20Scanner%2C%20the%20open-source%20scanner.%20Source%20documentation%3B%20publication%20is%20not%20implied.%20Provided%20README%20context%20%28verify%20against%20the%20sources%29%3A%20These%20examples%20show%20how%20the%20installed%20browser%20package%20is%20used%20without%20a%20framework%3A%20import%20an%20image%2C%20run%20decoding%20in%20a%20module%20worker%2C%20or%20connect%20the%20camera%20controller%20to%20its%20overlay.%20They%20are%20runnable%20package%20examples%2C%20not%20an%20additional%20scanner%20SDK.%20Serve%20the%20package%20as%20described%20below%20and%20keep%20secure-context%20and%20camera-permission%20requirements%20separate%20from%20image-decoding%20failures.%20Read%20the%20public%20documentation%20below.%20Explain%20what%20this%20destination%20does%2C%20its%20documented%20installation%20or%20access%20steps%2C%20a%20minimal%20example%20if%20supported%2C%20prerequisites%2C%20limitations%20and%20license.%20Distinguish%20published%20packages%20from%20working%20source%20and%20preparation.%20Cite%20your%20sources%3B%20if%20a%20page%20is%20unavailable%20or%20a%20fact%20cannot%20be%20checked%2C%20say%20so.%20Do%20not%20invent%20installation%20commands%2C%20benchmark%20results%20or%20comparisons.%20Answer%20in%20my%20language.%20Public%20sources%3A%20https%3A%2F%2Fgithub.com%2Fqrcode-ai-com%2Fqrcode-barcode-scanner-rust%2Fblob%2Fmain%2Fcrates%2Fqrcode-ai-scanner-wasm%2Fexamples%2FREADME.md%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fqr-code-scanner%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fbarcode-scanner%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fqr-code-scanner%2Fsdk%2Fjavascript%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fbarcode-scanner%2Fsdk%2Fjavascript%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fqr-code-generator%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fbarcode-generator)
[![Gemini](https://raw.githubusercontent.com/qrcode-ai-com/qrcode-barcode-scanner-rust/main/docs/assets/actions/gemini-v2.svg)](https://gemini.google.com/)
[![Grok](https://raw.githubusercontent.com/qrcode-ai-com/qrcode-barcode-scanner-rust/main/docs/assets/actions/grok-v2.svg)](https://grok.com/?q=Help%20me%20understand%20Examples%20%28crates%2Fqrcode-ai-scanner-wasm%2Fexamples%2FREADME.md%29%20for%20QR%20Code%20%26%20Barcode%20Scanner%2C%20the%20open-source%20scanner.%20Source%20documentation%3B%20publication%20is%20not%20implied.%20Provided%20README%20context%20%28verify%20against%20the%20sources%29%3A%20These%20examples%20show%20how%20the%20installed%20browser%20package%20is%20used%20without%20a%20framework%3A%20import%20an%20image%2C%20run%20decoding%20in%20a%20module%20worker%2C%20or%20connect%20the%20camera%20controller%20to%20its%20overlay.%20They%20are%20runnable%20package%20examples%2C%20not%20an%20additional%20scanner%20SDK.%20Serve%20the%20package%20as%20described%20below%20and%20keep%20secure-context%20and%20camera-permission%20requirements%20separate%20from%20image-decoding%20failures.%20Read%20the%20public%20documentation%20below.%20Explain%20what%20this%20destination%20does%2C%20its%20documented%20installation%20or%20access%20steps%2C%20a%20minimal%20example%20if%20supported%2C%20prerequisites%2C%20limitations%20and%20license.%20Distinguish%20published%20packages%20from%20working%20source%20and%20preparation.%20Cite%20your%20sources%3B%20if%20a%20page%20is%20unavailable%20or%20a%20fact%20cannot%20be%20checked%2C%20say%20so.%20Do%20not%20invent%20installation%20commands%2C%20benchmark%20results%20or%20comparisons.%20Answer%20in%20my%20language.%20Public%20sources%3A%20https%3A%2F%2Fgithub.com%2Fqrcode-ai-com%2Fqrcode-barcode-scanner-rust%2Fblob%2Fmain%2Fcrates%2Fqrcode-ai-scanner-wasm%2Fexamples%2FREADME.md%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fqr-code-scanner%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fbarcode-scanner%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fqr-code-scanner%2Fsdk%2Fjavascript%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fbarcode-scanner%2Fsdk%2Fjavascript%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fqr-code-generator%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fbarcode-generator)
[![Perplexity](https://raw.githubusercontent.com/qrcode-ai-com/qrcode-barcode-scanner-rust/main/docs/assets/actions/perplexity-v2.svg)](https://www.perplexity.ai/?q=Help%20me%20understand%20Examples%20%28crates%2Fqrcode-ai-scanner-wasm%2Fexamples%2FREADME.md%29%20for%20QR%20Code%20%26%20Barcode%20Scanner%2C%20the%20open-source%20scanner.%20Source%20documentation%3B%20publication%20is%20not%20implied.%20Provided%20README%20context%20%28verify%20against%20the%20sources%29%3A%20These%20examples%20show%20how%20the%20installed%20browser%20package%20is%20used%20without%20a%20framework%3A%20import%20an%20image%2C%20run%20decoding%20in%20a%20module%20worker%2C%20or%20connect%20the%20camera%20controller%20to%20its%20overlay.%20They%20are%20runnable%20package%20examples%2C%20not%20an%20additional%20scanner%20SDK.%20Serve%20the%20package%20as%20described%20below%20and%20keep%20secure-context%20and%20camera-permission%20requirements%20separate%20from%20image-decoding%20failures.%20Read%20the%20public%20documentation%20below.%20Explain%20what%20this%20destination%20does%2C%20its%20documented%20installation%20or%20access%20steps%2C%20a%20minimal%20example%20if%20supported%2C%20prerequisites%2C%20limitations%20and%20license.%20Distinguish%20published%20packages%20from%20working%20source%20and%20preparation.%20Cite%20your%20sources%3B%20if%20a%20page%20is%20unavailable%20or%20a%20fact%20cannot%20be%20checked%2C%20say%20so.%20Do%20not%20invent%20installation%20commands%2C%20benchmark%20results%20or%20comparisons.%20Answer%20in%20my%20language.%20Public%20sources%3A%20https%3A%2F%2Fgithub.com%2Fqrcode-ai-com%2Fqrcode-barcode-scanner-rust%2Fblob%2Fmain%2Fcrates%2Fqrcode-ai-scanner-wasm%2Fexamples%2FREADME.md%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fqr-code-scanner%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fbarcode-scanner%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fqr-code-scanner%2Fsdk%2Fjavascript%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fbarcode-scanner%2Fsdk%2Fjavascript%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fqr-code-generator%20https%3A%2F%2Fqrcode-ai.com%2Fen-US%2Fbarcode-generator)
[![Copilot](https://raw.githubusercontent.com/qrcode-ai-com/qrcode-barcode-scanner-rust/main/docs/assets/actions/copilot-v2.svg)](https://copilot.com/)
[![DeepSeek](https://raw.githubusercontent.com/qrcode-ai-com/qrcode-barcode-scanner-rust/main/docs/assets/actions/deepseek-v2.svg)](https://chat.deepseek.com/)
[![Meta AI](https://raw.githubusercontent.com/qrcode-ai-com/qrcode-barcode-scanner-rust/main/docs/assets/actions/meta-ai-v2.svg)](https://www.meta.ai/)
[![Qwen](https://raw.githubusercontent.com/qrcode-ai-com/qrcode-barcode-scanner-rust/main/docs/assets/actions/qwen-v2.svg)](https://qwen.ai/)

Choose an AI assistant to explain this destination using its public sources. Gemini opens gemini.google.com. Gemini, Copilot, DeepSeek, Meta AI and Qwen require pasting the prompt below; the other links include it in their URL. Sign-in or a manual paste may still be required. Check the answer against the documentation.

<details>
<summary>Copy the plain-text prompt</summary>

Use the code block's copy control, or select its complete text, then paste it into your assistant.

```text
Help me understand Examples (crates/qrcode-ai-scanner-wasm/examples/README.md) for QR Code & Barcode Scanner, the open-source scanner. Source documentation; publication is not implied. Provided README context (verify against the sources): These examples show how the installed browser package is used without a framework: import an image, run decoding in a module worker, or connect the camera controller to its overlay. They are runnable package examples, not an additional scanner SDK. Serve the package as described below and keep secure-context and camera-permission requirements separate from image-decoding failures. Read the public documentation below. Explain what this destination does, its documented installation or access steps, a minimal example if supported, prerequisites, limitations and license. Distinguish published packages from working source and preparation. Cite your sources; if a page is unavailable or a fact cannot be checked, say so. Do not invent installation commands, benchmark results or comparisons. Answer in my language. Public sources: https://github.com/qrcode-ai-com/qrcode-barcode-scanner-rust/blob/main/crates/qrcode-ai-scanner-wasm/examples/README.md https://qrcode-ai.com/en-US/qr-code-scanner https://qrcode-ai.com/en-US/barcode-scanner https://qrcode-ai.com/en-US/qr-code-scanner/sdk/javascript https://qrcode-ai.com/en-US/barcode-scanner/sdk/javascript https://qrcode-ai.com/en-US/qr-code-generator https://qrcode-ai.com/en-US/barcode-generator
```

</details>

<!-- /qrcode-ai-readme-actions:v2 -->

These examples show how the installed browser package is used without a framework: import an image, run decoding in a module worker, or connect the camera controller to its overlay. They are runnable package examples, not an additional scanner SDK. Serve the package as described below and keep secure-context and camera-permission requirements separate from image-decoding failures.

The [QR code scanner](https://qrcode-ai.com/en-US/qr-code-scanner) and [barcode scanner](https://qrcode-ai.com/en-US/barcode-scanner) are QR Code AI’s browser scanning entry points. For the corresponding development route, the [QR code scanner JavaScript guide](https://qrcode-ai.com/en-US/qr-code-scanner/sdk/javascript) and [barcode scanner JavaScript guide](https://qrcode-ai.com/en-US/barcode-scanner/sdk/javascript) connect this documentation to the two product journeys. If a product page is unavailable, the instructions and references in this README remain the starting point for this source directory.

To prepare a local image for the headless example, use the [QR code generator](https://qrcode-ai.com/en-US/qr-code-generator) or the [barcode generator](https://qrcode-ai.com/en-US/barcode-generator). Keep the original payload with the exported image so that you can compare the scan result with the value you intended to encode.

These pages run from the installed package folder itself: no bundler, framework or download.
The browser loads the package's own files by relative path (`../auto.js`), so a static server
rooted at the package folder runs them, provided it sends `.js` files with a JavaScript type
(see [Serve the package folder](#serve-the-package-folder)).

| File | What it shows |
| --- | --- |
| `headless.html`, `headless.js` | [Scan an image](#scan-an-image-headlesshtml) you choose, in a module worker. |
| `camera.html`, `camera.js` | [Scan with the camera](#scan-with-the-camera-camerahtml): the camera controller, the tracker and the overlay. |
| `scanner.worker.js` | [The module worker](#reuse-the-worker-scannerworkerjs) both pages share. |
| `ssr.mjs` | [Server rendering](#server-rendering-ssrmjs): the camera, overlay and destination modules imported in Node. |

The same modules also go through a bundler: see [Use a bundler](#use-a-bundler).

## Scan an image: `headless.html`

Choose an image with the file picker, or drop it on the page, and a profile (`full`, `fast` or
`frame`). The page decodes the image in a module worker, on your device: the image is never
uploaded. For each code it lists, it shows:

- the exact symbology (`qr_code`, `code128`, ...) and the payload kind (`url`, `text`, ...);
- how many copies of the code the scan located, and the charset of its text. A scan publishes
  at most 16 copies of one code. A count is exact only when the report says that the scan ran
  to its end, reconciled every read and left no copy out. Otherwise every count reads "at least
  N (incomplete)" when the report says some copy counts are incomplete (copies left out, or two
  kept as one, in a code it does not name), or "at least N (not proven complete)";
- the full decoded text, exactly as returned, as plain text. A decoded link is never opened and
  never made clickable. Characters that do not show are listed by code point under the text:
  controls, format characters, separators other than the ordinary space, default-ignorable
  characters (variation selectors, Hangul fillers, tag characters) and the blank Braille
  pattern.

It also names what it runs: the package name and version from `package.json`, the SHA-256 of
the WASM module from `build-info.json`, and the version the loaded module reports.

Each scan runs without a time budget, and the page reads from each report what it proves
(`trace.budget_exhausted` and `trace.saturation`; a flag the report leaves out counts as
unknown, never as false). The outcomes it tells apart:

| Outcome | Meaning |
| --- | --- |
| Codes found | The scan finished and lists every code it read. |
| Codes found, more may be present | The scan reached a capacity, such as more than 16 codes in one image or more reads than it reconciles, or its report does not say: what it left out is unknown, not absent. |
| No code found | The scan finished and found no code this profile can read. A valid result, not an error. |
| No code listed | The list is empty, but the scan reached a capacity, or its report does not say: this does not prove that the image holds no code. |
| Input error | The scanner refused the file: bytes it cannot decode as an image (`QRS-001`), or an image past the default size limits (`QRS-002`, `QRS-003`). |
| Configuration error | The request is wrong, not the image: a malformed request, an option outside its domain (`QRS-006`) or an unknown profile. |
| Loading error | The scanner did not load, so nothing was scanned. The loader's error code and HTTP status are shown; **Load the scanner again** retries in a new worker. |
| Scanner fault | Any other error during a scan, such as a WebAssembly runtime error, a stack overflow or a report the binding could not hand over. Its name and message are shown; reload the page before scanning again. |

Changing the profile scans the chosen image again. The controls are labelled and work with the
keyboard, the result summary is a live region (the list of codes is not announced as a whole),
and the colours follow the system's light or dark setting.

## Scan with the camera: `camera.html`

`camera.js` wires the package's camera parts the way an application would: `createCameraScanner`
from `../camera.js` captures frames, `createDetectionTracker` follows each code from frame to
frame, and `createCameraOverlay` from `../camera-overlay.js` draws an outline on each code with
a list of codes and a readout below the video. The frames are decoded by `scanner.worker.js`,
the image example's worker, with the `frame` profile and no time budget.

- **Nothing starts by itself.** The worker loads the scanner when the page opens, and a loading
  error offers **Load the scanner again**, in a new worker. The camera opens only when you press
  **Start the camera**; the same button stops it, and stopping ends every camera track.
- **Every tracker snapshot reaches the overlay, in order**: the tracker's `onChange` calls
  `overlay.update(snapshot)`.
- **A frame is copied only when the overlay asks.** Before each frame's pixels move to the
  worker, the page asks `overlay.wantsFrameImage()`, which is true only when a thumbnail in the
  list is due, at most once per second by default; only then does it copy the frame for
  `overlay.setFrameImage()`. With no code in view it copies nothing.
- **A hung frame replaces the worker.** When a frame passes the controller's decode timeout,
  the page terminates the worker before the controller moves on, and the controller's `recover`
  starts a new one.
- **Camera choice.** With two cameras or more, a list offers them; choosing one ends the session
  on the current camera and opens the chosen one.
- **Hidden page.** The controller stops the camera when the page is hidden or left, and the page
  says so; it never restarts the camera by itself. A page that is closed, not kept in the
  back-forward cache, also ends the tracker, the overlay, the verifier and the worker.
- **Honest failures.** A denied permission, a missing or busy camera, a page that is not a secure
  context, a camera that stops, a scanner that fails or hangs: each has its own message, with
  the controller's error code and the browser's error name, and **Start the camera** tries again.
- **Display.** Effects dim and blur the picture around the codes and draw a halo on their
  outlines: around every code, around the selected one, or off. Dim, blur and halo have their
  own sliders, and the list of codes can show automatically, with two codes or more, always, or
  never. The effects change only what you see: the scanner reads the frames as captured.
- **Decoded values are text.** The overlay's Open button is turned off (`actions: { open: false }`),
  so a decoded link is never opened, and the page builds no markup from a decoded value.
- **Link destinations are off by default**, whenever the page opens. Turned on, the page creates
  a verifier with a transport it builds itself, `createFetchTransport({ scope: 'network' })`
  from `../destination.js`: no hosted service. The readout then offers **Check destination**
  for an http(s) link, and a check starts only when you press it. A check is a real request
  from your browser to the link, and to every redirect the browser follows: up to 20, to any
  host, one on a private network included. Before the request leaves, the transport vets only
  the link itself; each redirect is left to the browser. A check can count a visit or use up a
  one-time link. Its result is what the browser lets the page see of those requests, shown as
  text; it is not a safety verdict.
- **Keyboard.** Every control is a native button, list, checkbox or slider. In the list of codes,
  the arrow keys, Home and End move between codes, Enter or Space selects one, and a digit
  selects codes 1 to 9 while the focus is in the list or the readout; after you select a code,
  Escape returns to automatic selection.

The page names the package, its version, the WASM SHA-256 and the engine version, as the image
example does.

## Serve the package folder

Module scripts and module workers do not run from a `file://` page. Serve the package folder
(the one that holds `package.json`, not `examples/`) with a static server on localhost, and
open a page under it. For example, from a project that installed the package:

```sh
python3 -m http.server 8000 --bind 127.0.0.1 --directory node_modules/@qrcode-ai-com/qrcode-barcode-scanner-wasm
```

then open <http://127.0.0.1:8000/examples/headless.html> or
<http://127.0.0.1:8000/examples/camera.html>.

The server must send `.js` files with a JavaScript type such as `text/javascript`: the browser
runs no module script sent as `text/plain`, and the page then stays on its starting message.
The type of the `.wasm` file does not matter: the loader compiles the bytes it fetches.

A browser offers the camera only to a secure context. `http://127.0.0.1` and
`http://localhost` count as secure on the machine that runs the browser; from another device,
such as a phone, serve the folder over `https`. Without a secure context the camera page says
so when you press **Start the camera**.

Each page requests only files of the package folder. The image page: `examples/headless.html`,
`examples/headless.js`, `examples/scanner.worker.js`, `auto.js`, `scan.js`,
`qrcode-barcode-scanner.js`, `qrcode-barcode-scanner_bg.wasm`, `package.json` and
`build-info.json`. The camera page: `examples/camera.html`, `examples/camera.js`, `camera.js`,
`camera-overlay.js`, `destination.js`, `examples/scanner.worker.js`, `auto.js`, `scan.js`,
`qrcode-barcode-scanner.js`, `qrcode-barcode-scanner_bg.wasm`, `package.json` and
`build-info.json`; and, only for a destination check you start, the link's URL and every redirect
the browser follows from it.

Each page also carries a Content-Security-Policy of its own, in a `meta` element. The image
page's:

```text
default-src 'none'; script-src 'self'; worker-src 'self'; connect-src 'self'; img-src 'self' blob: data:; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'
```

Scripts, workers and requests come from the page's own origin only; images also from `blob:`
(the preview) and `data:` (the empty icon); styles are inline. The camera page's:

```text
default-src 'none'; script-src 'self'; worker-src 'self'; connect-src 'self' https: http:; img-src data:; base-uri 'none'; form-action 'none'
```

Its `connect-src` also allows other http(s) hosts, which only a destination check you start
contacts. It needs no `style-src`: its styles are constructed stylesheets (see the next
section). A `meta` policy governs the page, not the module worker: a dedicated worker takes its
policy from the headers of its own script response, which a plain static server does not send.
The next section gives a policy for a server to send.

## Content Security Policy

Both pages scan under this policy, sent by the server as a `Content-Security-Policy` header
with every file of the package folder, with no violation reported:

```text
default-src 'self'; script-src 'self' 'wasm-unsafe-eval'; img-src blob: data:; style-src 'sha256-H+TFXvU970e5Qjw2zoXlY4qrtDJpAuCJvfJsc+MyRMI='
```

Each directive, and why the pages need it:

- `default-src 'self'`: every kind of request not named below comes from the page's origin
  only. The pages fetch `package.json` and `build-info.json`, and the worker fetches the
  `.wasm` module; `connect-src` falls back to this directive. With `default-src 'none'` the
  browser blocks those three requests.
- `script-src 'self' 'wasm-unsafe-eval'`: the page scripts, the module worker and the modules
  they import come from the page's origin; module workers fall back to this directive, so no
  `worker-src` is needed. `'wasm-unsafe-eval'` lets the worker compile the WebAssembly module,
  which the browser otherwise refuses (a `script-src` violation for `wasm-eval`).
- `img-src blob: data:`: the image page previews the chosen file from a `blob:` URL, and both
  pages declare an empty `data:` icon, so that the browser requests no `/favicon.ico`.
- `style-src 'sha256-…'`: the hash of the image page's one inline `<style>` element, which
  allows exactly that element. The camera page needs nothing here: it applies its own styles
  and the overlay's `CAMERA_OVERLAY_CSS` as constructed stylesheets
  (`document.adoptedStyleSheets`), creates the overlay with `styles: 'none'`, and the overlay
  sets each element's values through the CSSOM, never a `style` attribute. `style-src` governs
  none of these. The hash changes whenever the text of that `<style>` element changes.

No other directive is needed. The camera picture is a `MediaStream` set as the video's
`srcObject`, which is no request, so `media-src` plays no part; `connect-src` and `worker-src`
would only repeat their fallbacks. Removing any one source above makes a page report a
violation.

Under this policy, `connect-src` allows the page's origin only, so the browser blocks a
destination check of another host before sending it, and the readout shows the check as
unavailable. To let users check destinations from the camera page, add those origins to
`connect-src`, for example `connect-src 'self' https:`, knowing that every check is then a real
request from the user's browser, to the link and to every redirect the browser follows from it.

### How the policy was found

The package's browser run finds the policy above from the installed package, in Playwright's
Chromium and Firefox builds. It starts from this broader policy:

```text
default-src 'self'; script-src 'self' 'wasm-unsafe-eval'; worker-src 'self'; connect-src 'self'; img-src 'self' blob: data:; style-src 'self'; media-src 'self' blob:
```

Its `style-src 'self'` allows stylesheet files only and blocks the image page's inline
`<style>`, so the run adds that element's hash to `style-src` and checks that both pages scan
with no violation reported. It then removes each source in turn, in the order written, and
keeps a removal when both pages still scan with no violation reported; a directive left with
no source is dropped and falls back to `default-src`. It keeps `default-src 'self'`, since
removing it would let every directive the policy does not name load from anywhere, and checks
that `default-src 'none'` reports violations. The policy it ends with must be the one above,
and removing each of its sources must report the violation given for it. The same run presses
**Check destination** on the camera page under that policy: the browser reports a
`connect-src` violation for the link and the check is shown as unavailable. No other browser
is claimed.

## Reuse the worker: `scanner.worker.js`

The worker owns one scanner and knows nothing about the page that starts it, so another page
(the camera page, for example) can share it:

```js
const worker = new Worker(new URL('./scanner.worker.js', import.meta.url), { type: 'module' });
```

Every request carries an `id`, and gets exactly one reply with the same `id`:

| Request | Reply when it succeeds |
| --- | --- |
| `{ id, type: 'load' }` | `{ id, type, ok: true, version, variant, runtime, capabilities }` |
| `{ id, type: 'scan_image', bytes, ...options }` | `{ id, type, ok: true, report, ms }` |
| `{ id, type: 'scan_frame', rgba, width, height, ...options }` | `{ id, type, ok: true, report, ms }` |

In the load reply, `version` is the loaded module's version and `variant` the module the loader
chose (`'portable'`). `runtime` is what this browser supports, `{ wasm, simd, baseline }`, as
`scannerCapabilities()` from `../auto.js` returns it. `capabilities` lists the readers compiled
into the loaded module, `{ versions, engines, symbologies }`, as its `capabilities()` returns it.

`bytes` is an encoded image and `rgba` holds `width * height * 4` bytes, row by row; either can
be an `ArrayBuffer` or a typed array.
Transfer a large buffer rather than copying it: `worker.postMessage(request, [bytes])` for an
`ArrayBuffer`, `[bytes.buffer]` for a typed array (a typed array is not transferable itself).
The transfer detaches the whole buffer, with every view on it.

`options` are the package's named scan options (`ScanOptions`, the options of `scan` and
`scanFrame`), each written as a field of the request. Every one can be left out, and a request
without any scans as the engine does by default:

| Option | Type | What it sets |
| --- | --- | --- |
| `profile` | string | `'full'`, `'fast'` or `'frame'`. Left out, `scan_image` uses `full` and `scan_frame` uses `frame`. |
| `formats` | array of strings | The symbologies to read, by the names a report uses, such as `'qr_code'`. Left out or empty, every symbology is read. |
| `budgetMs` | number | The time budget in milliseconds, in place of the profile's. `0` runs the scan to its end. |
| `retryBudgetMs` | number | The budget of one second scan, which runs only when the time budget cut the first. `0` runs that scan to its end. |
| `maxDimension`, `maxPixels` | number | The largest accepted width or height, and the largest pixel count. |
| `scoreSkipAxes`, `scoreSkipChecks` | array of strings | The stress axes left out of the score, and the report sections withheld. |
| `scorePreset` | string | `'design'` or `'capture'`. |
| `alphaBackground` | string | The background under transparent pixels. |
| `alphaPalette` | array of strings | The colors the placement envelope probes. |

An editor that reads QR codes only, with a short first scan and one scan to the end when the
budget cut it:

```js
worker.postMessage({ id, type: 'scan_image', bytes, formats: ['qr_code'], budgetMs: 800,
  retryBudgetMs: 0 }, [bytes]);
```

Reading one symbology runs no reader of another, so the scan is shorter and cannot report a
code of another kind. `report` is the report of the last scan the request ran, and `ms` the
time of all of them. After a rerun with a budget of `0` the report is final; with any other
rerun budget, `report.trace.budget_exhausted` says whether the second scan was cut too.

A request that fails gets `{ id, type, ok: false, phase, error }`. `phase` is `'load'` when the
scanner could not load (`error.code` names the loader's reason, and `error.status` the HTTP
status when there is one), `'scan'` when the engine refused the call or its input, or stopped
(`error.qrs` holds the `QRS-xxx` code its message carries, or `null`; `error.name` and
`error.message` are the error's own), and `'request'` when the message itself is malformed: an
unknown `type`, missing bytes, a field that is not an option (a misspelling such as `budget_ms`
is refused, never ignored; `error.message` lists the options), or an option of the wrong type.
An option of the right type whose value the engine refuses, such as an unknown `formats` name,
is a `'scan'` failure.

Requests run one at a time, in the order they arrive. A worker loads the scanner once and
shares that attempt with every request, a failed one included: to retry, terminate the worker
and start a new one.

## Use a bundler

The pages above import the package's files by relative path because they run from the package
folder. In an application built by a bundler that resolves package `exports` and builds
`new Worker(new URL(…, import.meta.url))` as a worker, as Vite does, import the same modules
by package name. Your page's module:

```js
import init, { scan_image } from '@qrcode-ai-com/qrcode-barcode-scanner-wasm';
import { createCameraScanner, createDetectionTracker }
  from '@qrcode-ai-com/qrcode-barcode-scanner-wasm/camera';
import { CAMERA_OVERLAY_CSS, createCameraOverlay }
  from '@qrcode-ai-com/qrcode-barcode-scanner-wasm/camera-overlay';
import { createDestinationVerifier, createFetchTransport }
  from '@qrcode-ai-com/qrcode-barcode-scanner-wasm/destination';

const worker = new Worker(new URL('./scanner.worker.js', import.meta.url), { type: 'module' });
```

The package root is the scanner module itself (`init()` loads it); in a worker, prefer the
loader of `/auto`. The worker is your copy of `examples/scanner.worker.js`, saved beside that
module as `scanner.worker.js`, with its import of `'../auto.js'` replaced by:

```js
import { createScannerLoader, scannerCapabilities }
  from '@qrcode-ai-com/qrcode-barcode-scanner-wasm/auto';
```

Vite builds the worker as a separate module and emits the `.wasm` file that `/auto` asks for
as an asset. Under Vite's dev server, also exclude the package from dependency pre-bundling,
`optimizeDeps: { exclude: ['@qrcode-ai-com/qrcode-barcode-scanner-wasm'] }`: the package's modules
find the `.wasm` file next to themselves, through `import.meta.url`, and a pre-bundled copy of
them is not next to it. The package's browser run builds exactly these two blocks with
`vite build`, from the installed package, and checks that each package name resolves to the
package's own file and that every name they import is exported there; it runs no dev server
and no other bundler.

## Server rendering: `ssr.mjs`

The camera, overlay and destination modules import without a browser, so a page rendered on a
server can import them. `ssr.mjs` shows it: it first replaces the 34 DOM, camera and network
globals it lists (`window`, `document`, `navigator`, `fetch`, `XMLHttpRequest`, `WebSocket`,
`Worker` and the others in its `TRAPPED` list) with traps that record each read, then imports
`./camera`, `./camera-overlay` and `./destination` by package name and calls helpers a server
can use. Other globals stay as Node defines them. Run it with Node from the project that
installed the package:

```sh
node node_modules/@qrcode-ai-com/qrcode-barcode-scanner-wasm/examples/ssr.mjs
```

It prints exactly:

```text
Imported ./camera, ./camera-overlay and ./destination.
destinationCandidate('https://example.com/menu#top'): eligible true, request URL https://example.com/menu
destinationCandidate('call me'): eligible false, reason not_http
Verifier without a transport: canResolve false, resolve() unavailable (no_transport)
Default destination label: Destination not checked
symbologyFamily('qr_code'): matrix
Detection tracker: 0 tracks, status peek
Trapped globals read: none
```

A check requests a link without its fragment, plain text is never a link, and a verifier with
no transport sends nothing. The last line covers the 34 trapped globals only, not every name a
browser defines. The camera controller, the overlay and a fetch transport need a browser: create
them in client code. The package's browser run executes this file from the installed package
folder, compares these lines exactly, and checks that the run opens no socket and looks up no
name.

Maintained by **Thibaut Mélen & Nicolas Cella** for **QR Code AI**, a product of **SuperNovae Studio**. The license of each component is stated in its own license files.

<!-- qrscan-source-license:v1 -->

## Source license

Project-authored scanner code and documentation in this checkout are available under the [MIT License](https://github.com/qrcode-ai-com/qrcode-barcode-scanner-rust/blob/main/LICENSE). You may use, modify and redistribute this source in open-source or proprietary applications, including commercial products, while preserving the copyright and permission notice. Thibaut Mélen & Nicolas Cella develop QR Code AI at SuperNovae Studio.

Retained third-party code, dependencies, artwork and test data keep their own licenses and notices. The [licensing guide](https://github.com/qrcode-ai-com/qrcode-barcode-scanner-rust/blob/main/LICENSING.md) explains these component boundaries; the project license does not assign a license to an unrelated platform or service.

Published archives retain the license shipped with their version. A pinned engine, adapter or bundle may therefore have different terms from this working source. Read its packaged license and the [license history](https://github.com/qrcode-ai-com/qrcode-barcode-scanner-rust/blob/main/docs/guides/license-history.mdx) before reuse. The source change does not replace an archive or publish a new version.

<!-- /qrscan-source-license:v1 -->
