QRCode
A pure JavaScript QRCode encode and decode library.
QRCode guide and example
QRCode guide
QRCode encode example
QRCode decode example
Usage
Common Interface
declare class Charset {
public static readonly CP437: Charset;
public static readonly ISO_8859_1: Charset;
public static readonly ISO_8859_2: Charset;
public static readonly ISO_8859_3: Charset;
public static readonly ISO_8859_4: Charset;
public static readonly ISO_8859_5: Charset;
public static readonly ISO_8859_6: Charset;
public static readonly ISO_8859_7: Charset;
public static readonly ISO_8859_8: Charset;
public static readonly ISO_8859_9: Charset;
public static readonly ISO_8859_10: Charset;
public static readonly ISO_8859_11: Charset;
public static readonly ISO_8859_13: Charset;
public static readonly ISO_8859_14: Charset;
public static readonly ISO_8859_15: Charset;
public static readonly ISO_8859_16: Charset;
public static readonly SJIS: Charset;
public static readonly CP1250: Charset;
public static readonly CP1251: Charset;
public static readonly CP1252: Charset;
public static readonly CP1256: Charset;
public static readonly UTF_16BE: Charset;
public static readonly UTF_8: Charset;
public static readonly ASCII: Charset;
public static readonly BIG5: Charset;
public static readonly GB18030: Charset;
public static readonly EUC_KR: Charset;
public constructor(label: string, ...values: number[]);
}
Encoder Interface
declare type Level = 'L' | 'M' | 'Q' | 'H';
declare type RGB = [R: number, G: number, B: number];
declare type FNC1 = [mode: 'GS1'] | [mode: 'AIM', indicator: number];
declare class Alphanumeric {
public constructor(content: string);
}
declare class Byte {
public constructor(content: string, charset?: Charset);
}
declare class Hanzi {
public constructor(content: string);
}
declare class Kanji {
public constructor(content: string);
}
declare class Numeric {
public constructor(content: string);
}
declare interface EncoderOptions {
level?: Level;
hints?: { fnc1?: FNC1 };
version?: 'Auto' | number;
encode?: (content: string, charset: Charset) => Uint8Array;
}
declare interface DataURLOptions {
margin?: number;
foreground?: RGB;
background?: RGB;
}
declare class Encoded {
public size: number;
public mask: number;
public level: Level;
public version: number;
public get(x: number, y: number): number;
public toDataURL(moduleSize: number, options?: DataURLOptions): string;
}
declare class Encoder {
public constructor(options?: EncoderOptions);
public encode(...segments: (Alphanumeric | Byte | Hanzi | Kanji | Numeric)[]): Encoded;
}
Encoder Example
import { Byte, Encoder, Hanzi, Kanji } from '@nuintun/qrcode';
const encoder = new Encoder({
level: 'H'
});
const qrcode = encoder.encode(
new Hanzi('你好世界'),
new Byte('\nhello world\n'),
new Kanji('こんにちは世界')
);
console.log(qrcode.toDataURL());
Decoder Interface
declare class BitMatrix {
public get width(): number;
public get height(): number;
public set(x: number, y: number): void;
public get(x: number, y: number): number;
public flip(): void;
}
declare class Point {
public get x(): number;
public get y(): number;
}
declare class Pattern extends Point {
public get moduleSize(): number;
}
declare class FinderPatternGroup {
public get topLeft(): Pattern;
public get topRight(): Pattern;
public get bottomLeft(): Pattern;
}
declare class Detected {
public get matrix(): BitMatrix;
public get finder(): FinderPatternGroup;
public get alignment(): Pattern | undefined;
public get size(): number;
public get moduleSize(): number;
public mapping(x: number, y: number): Point;
}
declare function grayscale(imageData: ImageData): Uint8Array;
declare function binarize(luminances: Uint8Array, width: number, height: number): BitMatrix;
declare interface DetectorOptions {
strict?: boolean;
}
declare class Detector {
constructor(options?: DetectorOptions);
public detect(binarized: BitMatrix): Generator<Detected, void, boolean>;
}
declare interface DecoderOptions {
decode?: (bytes: Uint8Array, charset: Charset) => string;
}
declare class Decoder {
constructor(options?: DecoderOptions);
public decode(matrix: BitMatrix): Decoded;
}
Decoder Example
import { binarize, Decoder, Detector, grayscale } from '@nuintun/qrcode';
const image = new Image();
image.crossOrigin = 'anonymous';
image.addEventListener('error', () => {
console.error('image load error');
});
image.addEventListener('load', () => {
const { width, height } = image;
const canvas = new OffscreenCanvas(width, height);
const context = canvas.getContext('2d')!;
context.drawImage(image, 0, 0);
const luminances = grayscale(context.getImageData(0, 0, width, height));
const binarized = binarize(luminances, width, height);
const detector = new Detector();
const detected = detector.detect(binarized);
const decoder = new Decoder();
let iterator = detected.next();
while (!iterator.done) {
let succeed = false;
const detect = iterator.value;
try {
const { size, finder, alignment } = detect;
const decoded = decoder.decode(detect.matrix);
const { topLeft, topRight, bottomLeft } = finder;
const topLeftCorner = detect.mapping(0, 0);
const topRightCorner = detect.mapping(size, 0);
const bottomRightCorner = detect.mapping(size, size);
const bottomLeftCorner = detect.mapping(0, size);
const topLeftTiming = detect.mapping(6.5, 6.5);
const topRightTiming = detect.mapping(size - 6.5, 6.5);
const bottomLeftTiming = detect.mapping(6.5, size - 6.5);
console.log({
content: decoded.content,
finder: [topLeft, topRight, bottomLeft],
alignment: alignment ? alignment : null,
timing: [topLeftTiming, topRightTiming, bottomLeftTiming],
corners: [topLeftCorner, topRightCorner, bottomRightCorner, bottomLeftCorner]
});
succeed = true;
} catch {
}
iterator = detected.next(succeed);
}
});
image.src = 'https://nuintun.github.io/qrcode/packages/examples/src/images/qrcode.jpg';