-
Notifications
You must be signed in to change notification settings - Fork 15
/
render.ex
111 lines (94 loc) · 3.89 KB
/
render.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
defmodule QRCode.Render do
@moduledoc """
Render common module.
"""
alias QRCode.Render.{PngSettings, SvgSettings}
@doc """
Render QR matrix to `svg` or `png` binary representation with default settings.
"""
@spec render(Result.t(String.t(), binary()), :png | :svg) :: Result.t(String.t(), binary())
def render(qr, :svg) do
render(qr, :svg, %SvgSettings{})
end
def render(qr, :png) do
render(qr, :png, %PngSettings{})
end
@doc """
Render QR matrix to `svg` or `png` binary representation with your settings.
You can change the appearance of `svg` using the options:
```elixir
| Setting | Type | Default value | Description |
|--------------------|------------------------|---------------|-------------------------------------|
| scale | positive integer | 10 | changes size of rendered QR code |
| background_opacity | nil or 0.0 <= x <= 1.0 | nil | sets background opacity of svg |
| background_color | string or {r, g, b} | "#ffffff" | sets background color of svg |
| qrcode_color | string or {r, g, b} | "#000000" | sets color of QR |
| image | {string, size} or nil | nil | puts the image to the center of svg |
| structure | :minify or :readable | :minify | minifies or makes readable svg file |
svg_settings = %QRCode.Render.SvgSettings{qrcode_color: {17, 170, 136}, structure: :readable}
```
and same a way for `png`:
```elixir
| Setting | Type | Default value | Description |
|--------------------|------------------------|---------------|------------------------------|
| scale | positive integer | 10 | changes size of rendered QR |
| background_color | string or {r, g, b} | "#ffffff" | sets background color of png |
| qrcode_color | string or {r, g, b} | "#000000" | sets color of QR |
```
"""
@spec render(Result.t(String.t(), binary()), atom(), SvgSettings.t() | PngSettings.t()) ::
Result.t(String.t(), binary())
def render(qr, :svg, settings) do
QRCode.Render.Svg.create(qr, settings)
end
def render(qr, :png, settings) do
QRCode.Render.Png.create(qr, settings)
end
@doc """
Encodes rendered QR matrix as `svg` or `png` binary into a base 64.
This encoded string can be then used in Html as
For `svg` use
```
<img src="data:image/svg+xml; base64, encoded_svg_qr_code" alt="QR code" />
```
and in case of `png`
```
<img src="data:image/png; base64, encoded_png_qr_code" alt="QR code" />
```
"""
@spec to_base64(Result.t(String.t(), binary())) :: Result.t(String.t(), binary())
def to_base64({:ok, rendered_qr_matrix}) do
rendered_qr_matrix
|> Base.encode64()
|> Result.ok()
end
def to_base64(error), do: error
@doc """
Saves rendered QR code to `svg` or `png` file. See a few examples below:
```elixir
iex> "Hello World"
|> QRCode.create(:high)
|> QRCode.render(:svg)
|> QRCode.save("/path/to/hello.svg")
{:ok, "/path/to/hello.svg"}
```
```elixir
iex> png_settings = %QRCode.Render.PngSettings{qrcode_color: {17, 170, 136}}
iex> "Hello World"
|> QRCode.create(:high)
|> QRCode.render(:png, png_settings)
|> QRCode.save("/tmp/to/hello.png")
{:ok, "/path/to/hello.png"}
```
![QR code color](docs/qrcode_color.png)
"""
@spec save(Result.t(any(), binary()), Path.t()) ::
Result.t(String.t() | File.posix() | :badarg | :terminated, Path.t())
def save({:ok, rendered_qr_matrix}, path_with_file_name) do
case File.write(path_with_file_name, rendered_qr_matrix) do
:ok -> {:ok, path_with_file_name}
err -> err
end
end
def save(error, _path_with_file_name), do: error
end