2.1.0 • Published 3 months ago

dfx-qrcode v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

dfx-qrcode

dfx-qrcode is a tiny and simple-to-use Angular QR-Code generator library.

npm version NPM npm bundle size

Features

  • ESM Module based
  • Tiny (~ 5.9kB minified + gzipped)
  • Accessible
  • Server Side Rendering (Angular Universal)
  • Standalone Component API
  • Supports Canvas, PNG and SVG

Version compatibility

Angulardfx-qrcode
17.x.x2.x.x
16.x.x1.x.x

Installation

  • npm
    npm install dfx-qrcode
  • pnpm
    pnpm install dfx-qrcode

Usage

Standalone Components

import { QrCodeComponent } from 'dfx-qrcode';

@Component({
  standalone: true,
  selector: 'app-root',
  template: ` <qrcode data="hello" /> `,
  imports: [QRCodeComponent],
})
export class AppComponent {}

Module

import { QRCodeComponent } from 'dfx-qrcode';

@NgModule({
  declaration: [AppComponent],
  imports: [QRCodeComponent],
})
export class AppModule {}

@Component({
  selector: 'app-root',
  template: ` <qrcode data="hello" /> `,
})
export class AppComponent {}

Configuration

Standalone Components

import {
  provideQRCode,
  QRCodeComponent,
  withAllowEmptyString,
  withColorDark,
  withColorLight,
  withElementType,
  withErrorCorrectionLevel,
  withImage,
  withImageHeight,
  withImageSrc,
  withImageWidth,
  withMargin,
  withSize,
  withVersion,
} from 'dfx-qrcode';

@Component({
  // ...
  standalone: true,
  imports: [QRCodeComponent],
  providers: [
    provideQRCode(
      withAllowEmptyString(false),
      withColorDark('#000000'),
      withColorLight('#FFFFFF'),
      withCssClass('qrcode'),
      withElementType('img'),
      withErrorCorrectionLevel('L'),
      withMargin(4),
      withImage(withImageSrc('assets/logo.png'), withImageWidth(40), withImageHeight(40)),
      withVersion(-1),
      withSize(5),
    ),
  ],
  template: ` <qrcode data="hello" /> `,
  // ...
})
export class AppComponent {}

Module

import {
  provideQRCode,
  QRCodeComponent,
  withAllowEmptyString,
  withColorDark,
  withColorLight,
  withElementType,
  withErrorCorrectionLevel,
  withImage,
  withImageHeight,
  withImageSrc,
  withImageWidth,
  withMargin,
  withSize,
  withVersion,
} from 'dfx-qrcode';

@NgModule({
  declaration: [AppComponent],
  imports: [QRCodeComponent],
  providers: [
    provideQRCode(
      withAllowEmptyString(false),
      withColorDark('#000000'),
      withColorLight('#FFFFFF'),
      withCssClass('qrcode'),
      withElementType('img'),
      withErrorCorrectionLevel('L'),
      withMargin(4),
      withImage(withImageSrc('assets/logo.png'), withImageWidth(40), withImageHeight(40)),
      withVersion(-1),
      withSize(5),
    ),
  ],
})
export class AppModule {}

On component usage

@Component({
  // ...
  selector: 'app-root',
  template: `
    <qrcode
      [data]="'hello'"
      [allowEmptyString]="true"
      [elementType]="'img'"
      [errorCorrectionLevel]="'L'"
      [ariaLabel]="'Description of QR-Code'"
      [title]="'Description of QR-Code'"
      [alt]="'Description of QR-Code'"
      [cssClass]="'qrcode'"
      [colorDark]="'#000000'"
      [colorLight]="'#FFFFFF'"
      [margin]="4"
      [size]="5"
      [version]="-1"
      [imageSrc]="'assets/logo.png'"
      [imageWidth]="40"
      [imageHeight]="40"
    />
  `,
  // ...
})
export class AppComponent {}

Parameters

NameTypeDefault valueDescription
allowEmptyStringbooleanfalseDefine if empty or undefined strings should be converted to a space (" ") or throw an error.
elementType'img' \| 'canvas' \| 'svg'imgDefine if the QR-Code should be rendered as HTMLImageElement or HTMLCanvasElement
errorCorrectionLevel'L' \| 'M' \| 'Q' \| 'H'LDefine the error correction level
cssClassstringqrcodeDefine a css class for the wrapper element of the QR-Code
colorDarkHex color#000000Define the color of the dark elements
colorLightHex color#FFFFFFDefined the color of the light elements
marginnumber4Define the margin around the QR-Code in px
sizenumber5Define the size of the modules of the QR-Code in px
versionnumber-1Define the QR-Code version. -1 means auto-select
ariaLabelstring \| undefinedundefinedString which should be set as aria-label tag
titlestring \| undefinedundefinedString which should be set as title tag
altstring \| undefinedundefinedString which should be set as alt tag
imageSrcstring \| undefinedundefinedPath to image displayed in the QR-Code center
imageHeightnumber \| undefinedundefinedHeight of center image in px
imageWidthnumber \| undefinedundefinedWidth of center image in px

Error correction level

Error correction capability allows to successfully scan a QR Code even if the symbol is dirty or damaged. Four levels are available to choose according to the operating environment.

Higher levels offer a better error resistance but reduce the symbol's capacity. If the chances that the QR Code symbol may be corrupted are low (for example if it is showed through a monitor) is possible to safely use a low error level such as Low or Medium.

Possible levels are shown below:

LevelError resistance
L (Low)~7%
M (Medium)~15%
Q (Quartile)~25%
H (High)~30%

The percentage indicates the maximum amount of damaged surface after which the symbol becomes unreadable.

If not specified, the default value is L.

QR Code capacity

Capacity depends on symbol version and error correction level. Also encoding modes may influence the amount of storable data.

The QR Code versions range from version 1 to version 40. Each version has a different number of modules (black and white dots), which define the symbol's size. For version 1 they are 21x21, for version 2 25x25 e so on. Higher is the version, more are the storable data, and of course bigger will be the QR Code symbol.

The table below shows the maximum number of storable characters in each encoding mode and for each error correction level.

ModeLMQH
Numeric7089559639933057
Alphanumeric4296339124201852
Byte2953233116631273

If no version is specified, the more suitable value will be used. Unless a specific version is required, this option is not needed.

By Dafnik

2.1.0

3 months ago

2.0.1

4 months ago

1.0.1

6 months ago

2.0.0-beta.0

6 months ago

2.0.0

6 months ago

0.0.1

11 months ago