View Source QRCode.QR (QRCode v3.0.0)

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

@type groups() :: {[[], ...], [[]]}
@type level() :: :low | :medium | :quartile | :high
@type mask_num() :: 0..7
@type mode() :: :numeric | :alphanumeric | :byte | :kanji | :eci
@type 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())
}
@type version() :: 1..40

Link to this section Functions

Link to this function

create(orig, level \\ :low, mode \\ :byte)

View Source
@spec create(String.t(), level(), mode()) :: 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

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,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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, you have to render it first and then save it:

iex> qr = QRCode.QR.create("Hello World", :high)
iex> qr |> QRCode.render() |> QRCode.save("hello.svg")
{:ok, "hello.svg"}

The svg file will be saved into your project directory.

Link to this function

create!(text, level \\ :low, mode \\ :byte)

View Source
@spec create!(String.t(), level(), mode()) :: t()

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