- commit
- 429dd1126e8c5280943e99793a3783ef8f0d5d29
- parent
- 80895b4a9eff675bc0f8ea09f60106bca9ac44c8
- Author
- Camille Scholtz <camille@airmail.cc>
- Date
- 2017-12-26 14:14
Add WriteColorFile
Diffstat
| M | README.md | 8 | +++++++- |
| M | qrcode.go | 34 | +++++++++++++++++++++++++++++++--- |
2 files changed, 38 insertions, 4 deletions
diff --git a/README.md b/README.md
@@ -27,7 +27,13 @@ A command-line tool `qrcode` will be built into `$GOPATH/bin/`. 27 27 28 28 err := qrcode.WriteFile("https://example.org", qrcode.Medium, 256, "qr.png") 29 2930 -1 Both examples use the `qrcode.Medium` error Recovery Level and create a 256x256 pixel, black on white QR Code.-1 30 - **Create a PNG image with custom colors and write to file:** -1 31 -1 32 err := qrcode.WriteColorFile("https://example.org", qrcode.Medium, 256, color.Black, color.White, "qr.png") -1 33 -1 34 All examples use the qrcode.Medium error Recovery Level and create a fixed -1 35 256x256px size QR Code. The last function creates a white on black instead of black -1 36 on white QR Code. 31 37 32 38 The maximum capacity of a QR Code varies according to the content encoded and 33 39 the error recovery level. The maximum capacity is 2,953 bytes, 4,296
diff --git a/qrcode.go b/qrcode.go
@@ -12,7 +12,7 @@ obscured codes. There are four levels of error recovery: qrcode.{Low, Medium,
12 12 High, Highest}. QR Codes with a higher recovery level are more robust to damage,
13 13 at the cost of being physically larger.
14 14
15 -1 Two functions cover most use cases:
-1 15 Three functions cover most use cases:
16 16
17 17 - Create a PNG image:
18 18
@@ -23,8 +23,13 @@ Two functions cover most use cases:
23 23
24 24 err := qrcode.WriteFile("https://example.org", qrcode.Medium, 256, "qr.png")
25 25
26 -1 Both examples use the qrcode.Medium error Recovery Level and create a fixed
27 -1 256x256px size, black on white QR Code.
-1 26 - Create a PNG image with custom colors and write to file:
-1 27
-1 28 err := qrcode.WriteColorFile("https://example.org", qrcode.Medium, 256, color.Black, color.White, "qr.png")
-1 29
-1 30 All examples use the qrcode.Medium error Recovery Level and create a fixed
-1 31 256x256px size QR Code. The last function creates a white on black instead of black
-1 32 on white QR Code.
28 33
29 34 To generate a variable sized image instead, specify a negative size (in place of
30 35 the 256 above), such as -4 or -5. Larger negative numbers create larger images:
@@ -94,6 +99,29 @@ func WriteFile(content string, level RecoveryLevel, size int, filename string) e
94 99 return q.WriteFile(size, filename)
95 100 }
96 101
-1 102 // WriteColorFile encodes, then writes a QR Code to the given filename in PNG format.
-1 103 // With WriteColorFile you can also specify the colors you want to use.
-1 104 //
-1 105 // size is both the image width and height in pixels. If size is too small then
-1 106 // a larger image is silently written. Negative values for size cause a variable
-1 107 // sized image to be written: See the documentation for Image().
-1 108 func WriteColorFile(content string, level RecoveryLevel, size int, background,
-1 109 foreground color.Color, filename string) error {
-1 110
-1 111 var q *QRCode
-1 112
-1 113 q, err := New(content, level)
-1 114
-1 115 q.BackgroundColor = background
-1 116 q.ForegroundColor = foreground
-1 117
-1 118 if err != nil {
-1 119 return err
-1 120 }
-1 121
-1 122 return q.WriteFile(size, filename)
-1 123 }
-1 124
97 125 // A QRCode represents a valid encoded QRCode.
98 126 type QRCode struct {
99 127 // Original content encoded.