QRCode.QR (QRCode v2.2.1) View Source

QR code data structure

Link to this section Summary

Functions

Creates QR code. You can change the error correction level according to your needs. There are four level of error correction: :low | :medium | :quartile | :high where :low is default value.

The same as create/2, but raises a QRCode.Error exception if it fails.

Link to this section Types

Specs

groups() :: {[[], ...], [[]]}

Specs

level() :: :low | :medium | :quartile | :high

Specs

mask_num() :: 0..7

Specs

mode() :: :numeric | :alphanumeric | :byte | :kanji | :eci

Specs

t() :: %QRCode.QR{
  ecc: ExMaybe.t(QRCode.ErrorCorrection.t()),
  ecc_level: level(),
  encoded: ExMaybe.t(binary()),
  mask_num: mask_num(),
  matrix: MatrixReloaded.Matrix.t(),
  message: ExMaybe.t(String.t()),
  mode: mode(),
  orig: ExMaybe.t(String.t()),
  version: ExMaybe.t(version())
}

Specs

version() :: 1..40

Link to this section Functions

Link to this function

create(orig, level \\ :low)

View Source

Specs

create(String.t(), level()) :: Result.t(String.t(), t())

Creates QR code. You can change the error correction level according to your needs. There are four level of error correction: :low | :medium | :quartile | :high where :low is default value.

This function returns Result, it means either tuple of {:ok, QR.t()} or {:error, "msg"}.

Example:

iex> QRCode.QR.create("Hello World")
{:ok,
%QRCode.QR{
  ecc: %QRCode.ErrorCorrection{
    blocks_in_group1: 1,
    blocks_in_group2: 0,
    codewords: {[[139, 194, 132, 243, 72, 115, 10]], []},
    codewords_per_block_in_group1: 19,
    codewords_per_block_in_group2: 0,
    ec_codewrods_per_block: 7,
    groups: {[
        [64, 180, 134, 86, 198, 198, 242, 5, 118, 247, 38, 198, 64, 236, 17,
        236, 17, 236, 17]
      ], []}
  },
  ecc_level: :low,
  encoded: <<64, 180, 134, 86, 198, 198, 242, 5, 118, 247, 38, 198, 64, 236,
    17, 236, 17, 236, 17>>,
  mask_num: 0,
  matrix: [
    [1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1],
    [1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0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1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0],
    [1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
    [1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0],
    [1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1]
  ],
  message: <<64, 180, 134, 86, 198, 198, 242, 5, 118, 247, 38, 198, 64, 236,
    17, 236, 17, 236, 17, 139, 194, 132, 243, 72, 115, 10>>,
  mode: :byte,
  orig: "Hello World",
  version: 1
}}

For saving QR code to svg file, use QRCode.Svg.save_as/3 function:

iex> qr = QRCode.QR.create("Hello World", :high)
iex> qr |> Result.and_then(&QRCode.Svg.save_as(&1,"hello.svg"))
{:ok, "hello.svg"}

The svg file will be saved into your project directory.

Link to this function

create!(text, level \\ :low)

View Source

Specs

create!(String.t(), level()) :: t()

The same as create/2, but raises a QRCode.Error exception if it fails.