0.12.1 • Published 2 months ago

fast_qr v0.12.1

Weekly downloads
-
License
SEE LICENSE IN LI...
Repository
github
Last release
2 months ago

fast_qr is approximately 6-7 times faster than qrcode, see benchmarks

You can create a QR as

  • Raw matrix, well suited for custom usage
  • Vectorized image, well suited for web usage
  • Image, well suited for mobile / print usage

Usage

JavaScript / Typescript

Installation

npm install --save fast_qr
# Or
yarn add fast_qr

Create an svg

import init, { qr_svg } from "fast_qr";
import type { QrSvgOptions } from "fast_qr";

const options: QrSvgOptions = {
  module_color: "#FFF",
  background_color: "#000",
};

/// Once `init` is called, `qr_svg` can be called any number of times
// Using then / catch:
init()
  .then(() => {
    for (let i = 0; i < 10; i++) {
      const svg = qr_svg("https://fast-qr.com", options);
      console.log(svg);
    }
  })
  .catch((e) => {
    console.error("Could not fetch wasm: ", e);
  });

// Or using modern async await:
await init();
for (let i = 0; i < 10; i++) {
  const svg = qr_svg("https://fast-qr.com", options);
  console.log(svg);
}

Rust

Examples

You can run the examples with:

cargo run --example simple
cargo run --example svg -F svg
cargo run --example image -F image

They are all explained in detail below.

Converts QRCode to Unicode

use fast_qr::convert::ConvertError;
use fast_qr::qr::QRBuilder;

fn main() -> Result<(), ConvertError> {
    // QRBuilder::new can fail if content is too big for version,
    // please check before unwrapping.
    let qrcode = QRBuilder::new("https://example.com/")
        .build()
        .unwrap();

    let str = qrcode.to_str(); // .print() exists
    println!("{}", str);

    Ok(())
}

Converts QRCode to SVG docs.rs

Note: It requires the svg feature

use fast_qr::convert::ConvertError;
use fast_qr::convert::{svg::SvgBuilder, Builder, Shape};
use fast_qr::qr::QRBuilder;

fn main() -> Result<(), ConvertError> {
    // QRBuilder::new can fail if content is too big for version,
    // please check before unwrapping.
    let qrcode = QRBuilder::new("https://example.com/")
        .build()
        .unwrap();

    let _svg = SvgBuilder::default()
        .shape(Shape::RoundedSquare)
        .to_file(&qrcode, "out.svg");

    Ok(())
}

Converts QRCode to an image docs.rs

Note: It requires the image feature

use fast_qr::convert::ConvertError;
use fast_qr::convert::{image::ImageBuilder, Builder, Shape};
use fast_qr::qr::QRBuilder;

fn main() -> Result<(), ConvertError> {
    // QRBuilder::new can fail if content is too big for version,
    // please check before unwrapping.
    let qrcode = QRBuilder::new("https://example.com/")
        .build()
        .unwrap();

    let _img = ImageBuilder::default()
        .shape(Shape::RoundedSquare)
        .background_color([255, 255, 255, 0]) // Handles transparency
        .fit_width(600)
        .to_file(&qrcode, "out.png");

    Ok(())
}

Build WASM

WASM module also exists in NPM registry

Package is named fast_qr and can be installed like so :

npm install --save fast_qr

WASM module might be bundled

Find a bundled version in the latest release.

WASM module can be built from source

./wasm-pack.sh # Runs build in release mode and wasm-opt twice again
wasm-pack pack pkg # Creates an archive of said package
# wasm-pack publish pkg # Creates an archive & publish it to npm

Benchmarks

According to the following benchmarks, fast_qr is approximately 6-7x faster than qrcode.

BenchmarkLowerEstimateUpper
V03H/qrcode524.30 us535.02 us547.13 us
V03H/fast_qr82.079 us82.189 us82.318 usfast_qr is 6.51x faster
V10H/qrcode2.1105 ms2.1145 ms2.1186 ms
V10H/fast_qr268.70 us269.28 us269.85 usfast_qr is 7.85x faster
V40H/qrcode18.000 ms18.037 ms18.074 ms
V40H/fast_qr2.4313 ms2.4362 ms2.4411 msfast_qr is 7.40x faster

More benchmarks can be found in /benches folder.

0.12.1

2 months ago

0.9.0

12 months ago

0.8.3

1 year ago

0.8.1

1 year ago

0.7.1

1 year ago

0.7.0

1 year ago

0.5.0

2 years ago

0.5.1

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago