Procházet zdrojové kódy

add cli, add method QRCode.Write(), update readme

shenwei356 před 9 roky
rodič
revize
64729ee8b5
4 změnil soubory, kde provedl 165 přidání a 37 odebrání
  1. 2 0
      .gitignore
  2. 70 37
      README.md
  3. 17 0
      qrcode.go
  4. 76 0
      qrcode/main.go

+ 2 - 0
.gitignore

@@ -1,2 +1,4 @@
 *.sw*
 *.png
+*.directory
+qrcode/qrcode

+ 70 - 37
README.md

@@ -2,40 +2,73 @@
 
 <img src='https://skip.org/img/nyancat-youtube-qr.png' align='right'>
 
-Package qrcode implements a QR Code encoder. [![Build Status](https://travis-ci.org/skip2/go-qrcode.svg?branch=master)](https://travis-ci.org/skip2/go-qrcode) <br>
-
-<br>
-A QR Code is a matrix (two-dimensional) barcode. Arbitrary content may be encoded, with URLs being a popular choice :)<br>
-<br>
-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>
-<br>
-<h1>Usage</h1>
-<pre>import qrcode "github.com/skip2/go-qrcode"
-</pre>
-
-<ul><li><b>Create a PNG image:</b><pre>var png []byte
-png, err := qrcode.Encode("https://example.org", qrcode.Medium, 256)
-</pre></li></ul>
-
-<ul><li><b>Create a PNG image and write to a file:</b>
-<pre>err := qrcode.WriteFile("https://example.org", qrcode.Medium, 256, "qr.png")
-</pre></li></ul>
-
-Both examples use the <code>qrcode.Medium</code> error Recovery Level and create a 256x256 pixel, black on white QR Code.<br>
-<br>
-The maximum capacity of a QR Code varies according to the content encoded and<br>
-the error recovery level. The maximum capacity is 2,953 bytes, 4,296<br>
-alphanumeric characters, 7,089 numeric digits, or a combination of these.<br>
-<br>
-<h1>Documentation</h1>
-
-<a href='https://godoc.org/github.com/skip2/go-qrcode'><img src='https://godoc.org/github.com/skip2/go-qrcode?status.png' /></a>
-
-<h1>Demoapp</h1>
-<a href='http://go-qrcode.appspot.com'>http://go-qrcode.appspot.com</a>
-
-<h1>Links</h1>
-
-<ul><li><a href='http://en.wikipedia.org/wiki/QR_code'>http://en.wikipedia.org/wiki/QR_code</a>
-</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>
-</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>
+Package qrcode implements a QR Code encoder. [![Build Status](https://travis-ci.org/skip2/go-qrcode.svg?branch=master)](https://travis-ci.org/skip2/go-qrcode)
+
+A QR Code is a matrix (two-dimensional) barcode. Arbitrary content may be encoded, with URLs being a popular choice :)
+
+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.
+
+## Install
+
+    go get -u github.com/skip2/go-qrcode
+
+A command-line tool `qrcode` will be built into `$GOPATH/bin/`.
+
+## Usage
+
+    import qrcode "github.com/skip2/go-qrcode"
+
+- **Create a PNG image:**
+
+        var png []byte
+        png, err := qrcode.Encode("https://example.org", qrcode.Medium, 256)
+
+- **Create a PNG image and write to a file:**
+
+        err := qrcode.WriteFile("https://example.org", qrcode.Medium, 256, "qr.png")
+
+Both examples use the `qrcode.Medium` error Recovery Level and create a 256x256 pixel, black on white QR Code.
+
+The maximum capacity of a QR Code varies according to the content encoded and
+the error recovery level. The maximum capacity is 2,953 bytes, 4,296
+alphanumeric characters, 7,089 numeric digits, or a combination of these.
+
+## Documentation
+
+[![godoc](https://godoc.org/github.com/skip2/go-qrcode?status.png)](https://godoc.org/github.com/skip2/go-qrcode)
+
+## Demoapp
+
+[http://go-qrcode.appspot.com](http://go-qrcode.appspot.com)
+
+## CLI
+
+A command-line tool `qrcode` will be built into `$GOPATH/bin/`.
+
+```
+qrcode -- QR Code encoder in Go
+https://github.com/skip2/go-qrcode
+
+Flags:
+  -o string
+        out PNG file prefix, empty for stdout
+  -s int
+        image size (pixel) (default 256)
+
+Usage:
+  1. Arguments except for flags are joined by " " and used to generate QR code.
+     Default output is STDOUT, pipe to imagemagick command "display" to display
+     on any X server.
+
+       qrcode hello word | display
+
+  2. Save to file if "display" not available:
+
+       qrcode "homepage: https://github.com/skip2/go-qrcode" > out.png
+```
+
+## Links
+
+- [http://en.wikipedia.org/wiki/QR_code](http://en.wikipedia.org/wiki/QR_code)
+- [ISO/IEC 18004:2006](http://www.iso.org/iso/catalogue_detail.htm?csnumber=43655) - Main QR Code specification (approx CHF 198,00)<br>
+- [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)

+ 17 - 0
qrcode.go

@@ -41,6 +41,7 @@ import (
 	"image"
 	"image/color"
 	"image/png"
+	"io"
 	"io/ioutil"
 	"log"
 	"os"
@@ -278,6 +279,22 @@ func (q *QRCode) PNG(size int) ([]byte, error) {
 	return b.Bytes(), nil
 }
 
+// Write writes the QR Code as a PNG image to io.Writer.
+//
+// size is both the image width and height in pixels. If size is too small then
+// a larger image is silently written.
+func (q *QRCode) Write(size int, out io.Writer) error {
+	var png []byte
+
+	png, err := q.PNG(size)
+
+	if err != nil {
+		return err
+	}
+	_, err = out.Write(png)
+	return err
+}
+
 // WriteFile writes the QR Code as a PNG image to the specified file.
 //
 // size is both the image width and height in pixels. If size is too small then

+ 76 - 0
qrcode/main.go

@@ -0,0 +1,76 @@
+// go-qrcode
+// Copyright 2014 Tom Harwood
+
+package main
+
+import (
+	"flag"
+	"fmt"
+	"os"
+	"strings"
+
+	qrcode "github.com/skip2/go-qrcode"
+)
+
+func main() {
+	outFile := flag.String("o", "", "out PNG file prefix, empty for stdout")
+	size := flag.Int("s", 256, "image size (pixel)")
+	flag.Usage = func() {
+		fmt.Fprintf(os.Stderr, `qrcode -- QR Code encoder in Go
+https://github.com/skip2/go-qrcode
+
+Flags:
+`)
+		flag.PrintDefaults()
+		fmt.Fprintf(os.Stderr, `
+Usage:
+  1. Arguments except for flags are joined by " " and used to generate QR code.
+     Default output is STDOUT, pipe to imagemagick command "display" to display
+     on any X server.
+
+       qrcode hello word | display
+
+  2. Save to file if "display" not available:
+
+       qrcode "homepage: https://github.com/skip2/go-qrcode" > out.png
+
+`)
+	}
+	flag.Parse()
+
+	if *size <= 0 {
+		checkError(fmt.Errorf("Error: value of -s should > 0"))
+	}
+	if len(flag.Args()) == 0 {
+		flag.Usage()
+		checkError(fmt.Errorf("Error: no content given"))
+	}
+
+	content := strings.Join(flag.Args(), " ")
+
+	var err error
+	var q *qrcode.QRCode
+	q, err = qrcode.New(content, qrcode.Highest)
+	checkError(err)
+
+	var png []byte
+	png, err = q.PNG(*size)
+	checkError(err)
+
+	if *outFile == "" {
+		os.Stdout.Write(png)
+	} else {
+		var fh *os.File
+		fh, err = os.Create(*outFile + ".png")
+		checkError(err)
+		defer fh.Close()
+		fh.Write(png)
+	}
+}
+
+func checkError(err error) {
+	if err != nil {
+		fmt.Fprintf(os.Stderr, "%s\n", err)
+		os.Exit(0)
+	}
+}