Decoding/Encoding QR Code With Pure JavaScript

Category: Javascript | May 25, 2022
Author:nuintun
Views Total:3,830 views
Official Page:Go to website
Last Update:May 25, 2022
License:MIT

Preview:

Decoding/Encoding QR Code With Pure JavaScript

Description:

QR code encoder and decoder tools which help you quickly generate QR codes from given contents and decode QR codes from images.

How to use it:

Install the package.

# Yarn
$ yarn add @nuintun/qrcode
# NPM
$ npm install @nuintun/qrcode --save

Import modules as per your needs.

// encoder
import { Encoder, QRByte, QRKanji, ErrorCorrectionLevel } from '@nuintun/qrcode';
// decoder
import { Decoder } from '@nuintun/qrcode';

Encoder

Generate a QR code from the content you provide.

const qrcode = new Encoder();
qrcode.write('你好世界\n');
qrcode.write(new QRByte('hello world\n'));
qrcode.write(new QRKanji('こんにちは世界'));
qrcode.make();
console.log(qrcode.toDataURL());

Customize the encoder with the following methods.

// set version
qrcode.setVersion(number);
// set the error correction level
// L, M, Q, H
setErrorCorrectionLevel(level);
// set qrcode encoding hint
setEncodingHint(hint);

More possible API methods for the encoder.

// get matrix
qrcode.getMatrix();
// qet matrix size
qrcode.getMatrixSize();
// get version
qrcode.getVersion();
// get error correction level
qrcode.ggetErrorCorrectionLevel();
// get qrcode encoding hint
qrcode.getEncodingHint();
// add qrcode data
qrcode.write(QRByte | QRKanji | QRNumeric | QRAlphanumeric);
// get byte with row and col
qrcode.isDark(row: number, col: number);
// generate
qrcode.make();
// output base64 image
qrcode.toDataURL(moduleSize: number, margin: number)

Decoder:

Decode the QR code data from an image you provide.

const qrcode = new Decoder();
qrcode
  .scan('qrcode.jpg')
  .then(result => {
    console.log(result.data);
  })
  .catch(error => {
    console.error(result.data);
  });

Possible API methods.

const qrcode = new Decoder();
// set options
qrcode.setOptions({ 
  inversionAttempts: 'dontInvert' | 'onlyInvert' | 'attemptBoth' | 'invertFirst' 
});
// scan an image
qrcode.scan(src, promise);
// decode a qrcode from image data
decode(data: Uint8ClampedArray, width: number, height: number);

Changelog:

v3.3.5 (05/25/2023)

  • Update dependencies

v3.2.0 (05/07/2022)

  • Update dependencies

v3.1.3 (04/22/2022)

  • Update dependencies

v3.1.1 (01/24/2022)

  • Update

v3.0.1 (08/13/2020)

  • Fixed scan error to standard error

v3.0.0 (08/10/2020)

  • [Decoder]: Merge ECI chunk into QRByte chunk.

v3.0.0 (08/07/2020)

  • Change error to standard error

v2.1.1 (06/16/2020)

  • Add fallback locator point recenter

v2.1.0 (05/11/2020)

  • Update reader

v2.0.1 (04/13/2020)

  • Rename private property
  • Rename methods

v1.0.3 (12/18/2019)

  • Adjust code style

v1.0.1 (11/19/2019)

  • Fix “onlyInvert” inversionAttempts option

You Might Be Interested In:


One thought on “Decoding/Encoding QR Code With Pure JavaScript

Leave a Reply