Generate QR codes in Deno, as base64-encoded images, completely dependency-free and requires no binary.
This is a port of zenozeng/node-yaqrcode, which itself is a port of the QR code generator in the d-project.
import { qrcode } from "https://raw.githubusercontent.com/denorg/qrcode/da60b2680b4b56da03c635260e19f24f2fd0a44d/mod.ts";
const base64Image = await qrcode("bitcoin:ADDRESS?amount=0.5&label=ORDER"); // data:image/gif;base64,...The deno.land/x/qrcode tag is still at v2.0.0 and does not include the current return type fix (#7). The source-revision URL above is pinned so the example type-checks without claiming a release that has not happened.
You can also add a custom size by specifying size in the second parameter:
const fixedSizeImage = await qrcode("bitcoin:ADDRESS?amount=0.5&label=ORDER", { size: 500 });qrcode is asynchronous. It returns a Promise<string> that resolves to a
complete GIF data URL (data:image/gif;base64,...), not a QR code object. You
can use that string directly as an image source:
const imageSource: string = await qrcode("Hello world");
const html = `<img src="${imageSource}" alt="QR code">`;To save the generated QR code as a GIF in Deno, decode the data URL first:
const dataUrl = await qrcode("Hello world");
const base64 = dataUrl.replace(/^data:image\/gif;base64,/, "");
const bytes = Uint8Array.from(
atob(base64),
(character) => character.charCodeAt(0),
);
await Deno.writeFile("qrcode.gif", bytes);The generator only produces GIF data URLs; there is no option to request PNG output. If you need a PNG, decode the GIF and convert its first frame with an image library (this optional dependency is not needed to generate QR codes):
import { GIF } from "jsr:@matmen/imagescript@1.3.1";
import { qrcode } from "https://raw.githubusercontent.com/denorg/qrcode/da60b2680b4b56da03c635260e19f24f2fd0a44d/mod.ts";
const dataUrl = await qrcode("Hello world", { size: 250 });
const base64 = dataUrl.slice("data:image/gif;base64,".length);
const gifBytes = Uint8Array.from(
atob(base64),
(character) => character.charCodeAt(0),
);
const gif = await GIF.decode(gifBytes, true);
await Deno.writeFile("qrcode.png", await gif[0].encode());Save this as convert.ts and run it with
deno run --allow-write --allow-net=jsr.io convert.ts. ImageScript loads its
WebAssembly encoder from JSR at runtime, so it needs access to jsr.io even
when its import is cached.
The examples/server.ts file exposes QR codes over HTTP using Deno.serve:
deno run --allow-net examples/server.tsThen open a URL like this to receive a GIF response:
http://localhost:4000/?data=Hello%20World!&size=250&typeNumber=5&errorCorrection=L
Short query parameters are also supported for compact URLs: d, s, v, and e.
CLI with DPX
After installing DPX, you can directly use the CLI using the dpx command:
dpx qrcode <text>Alternatively, you can use it directly from the CLI by using deno run:
deno run https://deno.land/x/qrcode/cli.ts <text>You can also install it globally using the following:
deno install https://deno.land/x/qrcode/cli.tsThen, the package is available to run:
qrcode <text>Run tests:
deno test- To support typeNumber 40, use the
RS_BLOCK_TABLEfrom davidshimjs/qrcodejs - To support UTF-8, use the code from davidshimjs/qrcodejs
- Returns a base64-encoded image URL
- zenozeng/node-yaqrcode is the Node.js project that is the origin of this implementation
- MIT Β© Denorg
- Original repo: MIT Β© Zeno Zeng
- "QR Code" is registered trademark of Denso Wave
A project by Denorg, the world's first Deno-focused community
organization and consulting company. Work with us β