If you want to generate QR codes with JavaScript, there are a whole range of libraries available.
A very interesting choice is the QR Code Generator Library from the Project Nayuki, which is not only available for JavaScript (or TypeScript), but also for Java, Python, C++, C and Rust.
The "QR Code Generator Library" distinguishes between the generation of a QR code pattern and its output - for this reason, you have a lot of leeway, especially with the output, and can "let off steam" to your heart's content.
A QR code pattern can be created either directly for the content of a string or a byte array or for a previously created list of "segments" (the latter, however, is not described here).
"Nayuki" recommends in his examples the following declaration as a shortcut
const QRC = qrcodegen.QrCode
This way, the most important functions and constants of the library can be reached more easily.
The following levels are available for error correction:
QRC.Ecc.LOW
- tolerates up to 7% errors in the data area.QRC.Ecc.MEDIUM
- tolerates up to 15% errors in the data areaQRC.Ecc.QUARTILE
- tolerates up to 25% error in data rangeQRC.Ecc.HIGH
- tolerates up to 30% errors in the data rangeTogether with the abbreviation mentioned at the beginning, generating a QR code pattern for the content of a given string looks like this:
let QRC = qrcodegen.QrCode
let QRCode = QRC.encodeText(Text,ECCLevel)
In this
Text
- refers to the text for whose content (e.g. a URL) the code is to be generatedECCLevel
- refers to the lowest desired level for error correction (see above).If a higher error correction level can be achieved without increasing the size of the QR code, the higher level is preferred.
Together with the abbreviation mentioned at the beginning, generating a QR code pattern for the content of a given ArrayBuffer looks like this:
let QRC = qrcodegen.QrCode
let QRCode = QRC.encodeBinary(${Buffer},${ECCLevel})
In this
Buffer
- refers to the ArrayBuffer for whose contents the code is to be generatedECCLevel
- refers to the lowest desired level for error correction (see above).If a higher error correction level can be achieved without increasing the size of the QR code, the higher level is preferred.
The QR code pattern created as described above can now be output in various ways: it can be
The latter variant in particular is ideal for many forms of further processing.
A QR code pattern called QRCode
can be drawn in an (existing) HTML canvas as follows:
QRCode.drawCanvas(Scale,BorderWidth,CanvasElement)
In this
Scale
- refers to width and height of a module in pixels,BorderWidth
- refers to the width of the "quiet zone" around the actual QR code (measured in multiples of a module),CanvasElement
- refers to the canvas element into which to draw,The size of the canvas is set so that the QR code fits exactly inside it, the individual modules are either black or white and not translucent.
A QR code pattern called QRCode
can be output as a vector graphic in SVG format as follows:
let SVGasString = QRCode.toSvgString(BorderWidth)
In this
BorderWidth
- refers to the width of the "quiet zone" around the actual QR code (measured in multiples of a module)However, a QR code pattern can also be read module by module. For a pattern called QRCode
this is done as follows (shown here in TypeScript notation):
for (let y:int = 0; y < QRCode.size; y++) {
for (let x:int = 0; x < QRCode.size; x++) {
let Module:boolean = QRCode.getModule(x,y)
now do something with Module
}
}
Modules with the value true
are normally shown in black, those with the value false
in white.
This web page uses the following third-party libraries, assets or StackOverflow answers:
The author would like to thank the developers and authors of the above-mentioned contributions for their effort and willingness to make their works available to the general public.