- commit
- 64729ee8b51c0238515204037c58e847d8fc9c6a
- parent
- 5bfceac8c4950ff8f735525ddcde5a6b08d77ab6
- Author
- shenwei356 <shenwei356@gmail.com>
- Date
- 2016-12-31 15:44
add cli, add method QRCode.Write(), update readme
Diffstat
| M | .gitignore | 2 | ++ |
| M | README.md | 107 | ++++++++++++++++++++++++++++++++++++++++--------------------- |
| M | qrcode.go | 17 | +++++++++++++++++ |
| A | qrcode/main.go | 76 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 files changed, 165 insertions, 37 deletions
diff --git a/.gitignore b/.gitignore
@@ -1,2 +1,4 @@ 1 1 *.sw* 2 2 *.png -1 3 *.directory -1 4 qrcode/qrcode
diff --git a/README.md b/README.md
@@ -2,40 +2,73 @@ 2 2 3 3 <img src='https://skip.org/img/nyancat-youtube-qr.png' align='right'> 4 45 -1 Package qrcode implements a QR Code encoder. [](https://travis-ci.org/skip2/go-qrcode) <br>6 -17 -1 <br>8 -1 A QR Code is a matrix (two-dimensional) barcode. Arbitrary content may be encoded, with URLs being a popular choice :)<br>9 -1 <br>10 -1 Each QR Code contains error recovery information to aid reading damaged or obscured codes. There are four levels of error recovery: Low, medium, high and highest. QR Codes with a higher recovery level are more robust to damage, at the cost of being physically larger.<br>11 -1 <br>12 -1 <h1>Usage</h1>13 -1 <pre>import qrcode "github.com/skip2/go-qrcode"14 -1 </pre>15 -116 -1 <ul><li><b>Create a PNG image:</b><pre>var png []byte17 -1 png, err := qrcode.Encode("https://example.org", qrcode.Medium, 256)18 -1 </pre></li></ul>19 -120 -1 <ul><li><b>Create a PNG image and write to a file:</b>21 -1 <pre>err := qrcode.WriteFile("https://example.org", qrcode.Medium, 256, "qr.png")22 -1 </pre></li></ul>23 -124 -1 Both examples use the <code>qrcode.Medium</code> error Recovery Level and create a 256x256 pixel, black on white QR Code.<br>25 -1 <br>26 -1 The maximum capacity of a QR Code varies according to the content encoded and<br>27 -1 the error recovery level. The maximum capacity is 2,953 bytes, 4,296<br>28 -1 alphanumeric characters, 7,089 numeric digits, or a combination of these.<br>29 -1 <br>30 -1 <h1>Documentation</h1>31 -132 -1 <a href='https://godoc.org/github.com/skip2/go-qrcode'><img src='https://godoc.org/github.com/skip2/go-qrcode?status.png' /></a>33 -134 -1 <h1>Demoapp</h1>35 -1 <a href='http://go-qrcode.appspot.com'>http://go-qrcode.appspot.com</a>36 -137 -1 <h1>Links</h1>38 -139 -1 <ul><li><a href='http://en.wikipedia.org/wiki/QR_code'>http://en.wikipedia.org/wiki/QR_code</a>40 -1 </li><li><a href='http://www.iso.org/iso/catalogue_detail.htm?csnumber=43655'>ISO/IEC 18004:2006</a> - Main QR Code specification (approx CHF 198,00)<br>41 -1 </li><li><a href='https://github.com/qpliu/qrencode-go/'>https://github.com/qpliu/qrencode-go/</a> - alternative Go QR encoding library based on <a href='https://github.com/zxing/zxing'>ZXing</a>-1 5 Package qrcode implements a QR Code encoder. [](https://travis-ci.org/skip2/go-qrcode) -1 6 -1 7 A QR Code is a matrix (two-dimensional) barcode. Arbitrary content may be encoded, with URLs being a popular choice :) -1 8 -1 9 Each QR Code contains error recovery information to aid reading damaged or obscured codes. There are four levels of error recovery: Low, medium, high and highest. QR Codes with a higher recovery level are more robust to damage, at the cost of being physically larger. -1 10 -1 11 ## Install -1 12 -1 13 go get -u github.com/skip2/go-qrcode -1 14 -1 15 A command-line tool `qrcode` will be built into `$GOPATH/bin/`. -1 16 -1 17 ## Usage -1 18 -1 19 import qrcode "github.com/skip2/go-qrcode" -1 20 -1 21 - **Create a PNG image:** -1 22 -1 23 var png []byte -1 24 png, err := qrcode.Encode("https://example.org", qrcode.Medium, 256) -1 25 -1 26 - **Create a PNG image and write to a file:** -1 27 -1 28 err := qrcode.WriteFile("https://example.org", qrcode.Medium, 256, "qr.png") -1 29 -1 30 Both examples use the `qrcode.Medium` error Recovery Level and create a 256x256 pixel, black on white QR Code. -1 31 -1 32 The maximum capacity of a QR Code varies according to the content encoded and -1 33 the error recovery level. The maximum capacity is 2,953 bytes, 4,296 -1 34 alphanumeric characters, 7,089 numeric digits, or a combination of these. -1 35 -1 36 ## Documentation -1 37 -1 38 [](https://godoc.org/github.com/skip2/go-qrcode) -1 39 -1 40 ## Demoapp -1 41 -1 42 [http://go-qrcode.appspot.com](http://go-qrcode.appspot.com) -1 43 -1 44 ## CLI -1 45 -1 46 A command-line tool `qrcode` will be built into `$GOPATH/bin/`. -1 47 -1 48 ``` -1 49 qrcode -- QR Code encoder in Go -1 50 https://github.com/skip2/go-qrcode -1 51 -1 52 Flags: -1 53 -o string -1 54 out PNG file prefix, empty for stdout -1 55 -s int -1 56 image size (pixel) (default 256) -1 57 -1 58 Usage: -1 59 1. Arguments except for flags are joined by " " and used to generate QR code. -1 60 Default output is STDOUT, pipe to imagemagick command "display" to display -1 61 on any X server. -1 62 -1 63 qrcode hello word | display -1 64 -1 65 2. Save to file if "display" not available: -1 66 -1 67 qrcode "homepage: https://github.com/skip2/go-qrcode" > out.png -1 68 ``` -1 69 -1 70 ## Links -1 71 -1 72 - [http://en.wikipedia.org/wiki/QR_code](http://en.wikipedia.org/wiki/QR_code) -1 73 - [ISO/IEC 18004:2006](http://www.iso.org/iso/catalogue_detail.htm?csnumber=43655) - Main QR Code specification (approx CHF 198,00)<br> -1 74 - [https://github.com/qpliu/qrencode-go/](https://github.com/qpliu/qrencode-go/) - alternative Go QR encoding library based on [ZXing](https://github.com/zxing/zxing)
diff --git a/qrcode.go b/qrcode.go
@@ -41,6 +41,7 @@ import ( 41 41 "image" 42 42 "image/color" 43 43 "image/png" -1 44 "io" 44 45 "io/ioutil" 45 46 "log" 46 47 "os" @@ -278,6 +279,22 @@ func (q *QRCode) PNG(size int) ([]byte, error) { 278 279 return b.Bytes(), nil 279 280 } 280 281 -1 282 // Write writes the QR Code as a PNG image to io.Writer. -1 283 // -1 284 // size is both the image width and height in pixels. If size is too small then -1 285 // a larger image is silently written. -1 286 func (q *QRCode) Write(size int, out io.Writer) error { -1 287 var png []byte -1 288 -1 289 png, err := q.PNG(size) -1 290 -1 291 if err != nil { -1 292 return err -1 293 } -1 294 _, err = out.Write(png) -1 295 return err -1 296 } -1 297 281 298 // WriteFile writes the QR Code as a PNG image to the specified file. 282 299 // 283 300 // size is both the image width and height in pixels. If size is too small then
diff --git a/qrcode/main.go b/qrcode/main.go
@@ -0,0 +1,76 @@
-1 1 // go-qrcode
-1 2 // Copyright 2014 Tom Harwood
-1 3
-1 4 package main
-1 5
-1 6 import (
-1 7 "flag"
-1 8 "fmt"
-1 9 "os"
-1 10 "strings"
-1 11
-1 12 qrcode "github.com/skip2/go-qrcode"
-1 13 )
-1 14
-1 15 func main() {
-1 16 outFile := flag.String("o", "", "out PNG file prefix, empty for stdout")
-1 17 size := flag.Int("s", 256, "image size (pixel)")
-1 18 flag.Usage = func() {
-1 19 fmt.Fprintf(os.Stderr, `qrcode -- QR Code encoder in Go
-1 20 https://github.com/skip2/go-qrcode
-1 21
-1 22 Flags:
-1 23 `)
-1 24 flag.PrintDefaults()
-1 25 fmt.Fprintf(os.Stderr, `
-1 26 Usage:
-1 27 1. Arguments except for flags are joined by " " and used to generate QR code.
-1 28 Default output is STDOUT, pipe to imagemagick command "display" to display
-1 29 on any X server.
-1 30
-1 31 qrcode hello word | display
-1 32
-1 33 2. Save to file if "display" not available:
-1 34
-1 35 qrcode "homepage: https://github.com/skip2/go-qrcode" > out.png
-1 36
-1 37 `)
-1 38 }
-1 39 flag.Parse()
-1 40
-1 41 if *size <= 0 {
-1 42 checkError(fmt.Errorf("Error: value of -s should > 0"))
-1 43 }
-1 44 if len(flag.Args()) == 0 {
-1 45 flag.Usage()
-1 46 checkError(fmt.Errorf("Error: no content given"))
-1 47 }
-1 48
-1 49 content := strings.Join(flag.Args(), " ")
-1 50
-1 51 var err error
-1 52 var q *qrcode.QRCode
-1 53 q, err = qrcode.New(content, qrcode.Highest)
-1 54 checkError(err)
-1 55
-1 56 var png []byte
-1 57 png, err = q.PNG(*size)
-1 58 checkError(err)
-1 59
-1 60 if *outFile == "" {
-1 61 os.Stdout.Write(png)
-1 62 } else {
-1 63 var fh *os.File
-1 64 fh, err = os.Create(*outFile + ".png")
-1 65 checkError(err)
-1 66 defer fh.Close()
-1 67 fh.Write(png)
-1 68 }
-1 69 }
-1 70
-1 71 func checkError(err error) {
-1 72 if err != nil {
-1 73 fmt.Fprintf(os.Stderr, "%s\n", err)
-1 74 os.Exit(0)
-1 75 }
-1 76 }