Make Qr Codes suited for the terminal. This package provides functions for creating both large and small QR codes, with customizable options such as error correction level, border size, module colors etc.
npm i @sec-ant/ansi-qr-code
First, import the makeAnsiQrCode
and/or makeSmallAnsiQrCode
function(s) from the package.
import { makeAnsiQrCode, makeSmallAnsiQrCode } from "@sec-ant/ansi-qr-code";
You can then generate a QR code by calling the function, passing your desired text along with optional configuration options.
Large QR Codes are generated by makeAnsiQrCode
function. This function uses ideographic space (" ") to construct the Qr Code.
const ansiQrCode = makeAnsiQrCode("Hello, world!", {
ecc: ECC.MEDIUM,
border: 1,
blackModuleColor: BG_BLUE,
whiteModuleColor: BG_BRIGHT_GREEN,
});
console.log(ansiQrCode.value);
- ecc:
Ecc
- Determines the error correction level. Can be one ofEcc
enum values(Ecc.LOW
,Ecc.MEDIUM
,Ecc.QUARTILE
, orEcc.HIGH
). Default isEcc.LOW
. - border:
number
- Specifies the border size. Default is4
. - blackModuleColor:
BgColor | Reset
- Defines the ANSI color code for the black modules of the QR code. Default isBG_BLACK
. - whiteModuleColor:
BgColor | Reset
- Defines the ANSI color code for the white modules of the QR code. Default isBG_BRIGHT_WHITE
.
Small QR Codes are generated by makeSmallAnsiQrCode
function. This function uses the combinations of block elements and space as quadrublocks to construct the Qr Code.
quadrublocks | 00 |
01 |
10 |
11 |
---|---|---|---|---|
00 |
|
▄ |
▄ |
▄▄ |
01 |
▀ |
█ |
▄▀ |
▄█ |
10 |
▀ |
▀▄ |
█ |
█▄ |
11 |
▀▀ |
▀█ |
█▀ |
██ |
const smallAnsiQrCode = makeSmallAnsiQrCode("Hello, world!", {
ecc: ECC.MEDIUM,
border: 4,
blackModuleColor: FG_RED,
whiteModuleColor: RESET,
});
console.log(smallAnsiQrCode.value);
- ecc:
Ecc
- Determines the error correction level. Can be one ofEcc
enum values(Ecc.LOW
,Ecc.MEDIUM
,Ecc.QUARTILE
, orEcc.HIGH
). Default isEcc.LOW
. - border:
number
- Specifies the border size. Default is4
. - blackModuleColor:
FgColor | Reset
(Default style) orBgColor | Reset
(Inverted style) - Defines the ANSI color code for the black modules of the QR code. - whiteModuleColor:
BgColor | Reset
(Default style) orFgColor | Reset
(Inverted style) - Defines the ANSI color code for the white modules of the QR code.
The package also exports ECC
(an Enum that determines the error correction level) and some additional ANSI terminal colors and escape codes like RESET
, BG_BLACK
, BG_BRIGHT_WHITE
, and FG_BLACK
. You can use these constants to set the module colors of your QR codes.
MIT