QRCode.Svg (QRCode v2.2.1) View Source

SVG structure and helper functions.

Link to this section Summary

Functions

Create Svg structure from QR matrix as binary. This binary contains svg attributes and svg elements.

Saves QR code to svg file. This function returns Result, it means either tuple of {:ok, "path/to/file.svg"} or {:error, reason}.

Create Svg structure from QR matrix as binary and encode it into a base 64. This encoded string can be then used in Html as

Link to this section Types

Specs

t() :: %QRCode.Svg{
  body: String.t(),
  height: ExMaybe.t(integer()),
  rank_matrix: ExMaybe.t(pos_integer()),
  width: ExMaybe.t(integer()),
  xlink: String.t(),
  xmlns: String.t()
}

Link to this section Functions

Link to this function

create(qr, settings \\ %SvgSettings{})

View Source

Specs

Create Svg structure from QR matrix as binary. This binary contains svg attributes and svg elements.

Link to this function

save_as(qr, svg_name, settings \\ %SvgSettings{})

View Source

Specs

save_as(QRCode.QR.t(), Path.t(), QRCode.SvgSettings.t()) ::
  Result.t(String.t() | File.posix() | :badarg | :terminated, Path.t())

Saves QR code to svg file. This function returns Result, it means either tuple of {:ok, "path/to/file.svg"} or {:error, reason}.

Also there are a few settings for svg:

| Setting            | Type                   | Default value | Description               |
|--------------------|------------------------|---------------|---------------------------|
| scale              | positive integer       | 10            | scale for svg QR code     |
| background_opacity | nil or 0.0 <= x <= 1.0 | nil           | background opacity of svg |
| background_color   | string or {r, g, b}    | "#ffffff"     | background color of svg   |
| qrcode_color       | string or {r, g, b}    | "#000000"     | color of QR code          |
| format             | :none or :indent       | :none         | indentation of elements   |

By this option, you can set the size QR code, background color (and also opacity) of QR code or QR code colors.The format option is for removing indentation (of elements) in a svg file. Let's see an example below:

iex> settings = %QRCode.SvgSettings{qrcode_color: {17, 170, 136}}
iex> qr = QRCode.QR.create("your_string")
iex> qr |> Result.and_then(&QRCode.Svg.save_as(&1,"/tmp/your_name.svg", settings))
{:ok, "/tmp/your_name.svg"}

The svg file will be saved into your tmp directory.

QR code color

Link to this function

to_base64(qr, settings \\ %SvgSettings{})

View Source

Specs

to_base64(QRCode.QR.t(), QRCode.SvgSettings.t()) :: binary()

Create Svg structure from QR matrix as binary and encode it into a base 64. This encoded string can be then used in Html as

<img src="data:image/svg+xml;base64, encoded_svg_qr_code" />