Skip to content

Commit cf02323

Browse files
authored
Merge pull request skip2#3 from shenwei356/master
add cli, add method QRCode.Write(), update readme
2 parents 5bfceac + 8d45c0c commit cf02323

File tree

5 files changed

+165
-38
lines changed

5 files changed

+165
-38
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
*.sw*
22
*.png
3+
*.directory
4+
qrcode/qrcode

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ language: go
22

33
go:
44
- 1.3
5-
- release
65

76
script:
87
- go test -v ./...

README.md

+70-37
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,73 @@
22

33
<img src='https://skip.org/img/nyancat-youtube-qr.png' align='right'>
44

5-
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>
6-
7-
<br>
8-
A QR Code is a matrix (two-dimensional) barcode. Arbitrary content may be encoded, with URLs being a popular choice :)<br>
9-
<br>
10-
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-
<br>
12-
<h1>Usage</h1>
13-
<pre>import qrcode "github.com/skip2/go-qrcode"
14-
</pre>
15-
16-
<ul><li><b>Create a PNG image:</b><pre>var png []byte
17-
png, err := qrcode.Encode("https://example.org", qrcode.Medium, 256)
18-
</pre></li></ul>
19-
20-
<ul><li><b>Create a PNG image and write to a file:</b>
21-
<pre>err := qrcode.WriteFile("https://example.org", qrcode.Medium, 256, "qr.png")
22-
</pre></li></ul>
23-
24-
Both examples use the <code>qrcode.Medium</code> error Recovery Level and create a 256x256 pixel, black on white QR Code.<br>
25-
<br>
26-
The maximum capacity of a QR Code varies according to the content encoded and<br>
27-
the error recovery level. The maximum capacity is 2,953 bytes, 4,296<br>
28-
alphanumeric characters, 7,089 numeric digits, or a combination of these.<br>
29-
<br>
30-
<h1>Documentation</h1>
31-
32-
<a href='https://godoc.org/github.com/skip2/go-qrcode'><img src='https://godoc.org/github.com/skip2/go-qrcode?status.png' /></a>
33-
34-
<h1>Demoapp</h1>
35-
<a href='http://go-qrcode.appspot.com'>http://go-qrcode.appspot.com</a>
36-
37-
<h1>Links</h1>
38-
39-
<ul><li><a href='http://en.wikipedia.org/wiki/QR_code'>http://en.wikipedia.org/wiki/QR_code</a>
40-
</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-
</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>
5+
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)
6+
7+
A QR Code is a matrix (two-dimensional) barcode. Arbitrary content may be encoded, with URLs being a popular choice :)
8+
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.
10+
11+
## Install
12+
13+
go get -u github.com/skip2/go-qrcode/...
14+
15+
A command-line tool `qrcode` will be built into `$GOPATH/bin/`.
16+
17+
## Usage
18+
19+
import qrcode "github.com/skip2/go-qrcode"
20+
21+
- **Create a PNG image:**
22+
23+
var png []byte
24+
png, err := qrcode.Encode("https://example.org", qrcode.Medium, 256)
25+
26+
- **Create a PNG image and write to a file:**
27+
28+
err := qrcode.WriteFile("https://example.org", qrcode.Medium, 256, "qr.png")
29+
30+
Both examples use the `qrcode.Medium` error Recovery Level and create a 256x256 pixel, black on white QR Code.
31+
32+
The maximum capacity of a QR Code varies according to the content encoded and
33+
the error recovery level. The maximum capacity is 2,953 bytes, 4,296
34+
alphanumeric characters, 7,089 numeric digits, or a combination of these.
35+
36+
## Documentation
37+
38+
[![godoc](https://godoc.org/github.com/skip2/go-qrcode?status.png)](https://godoc.org/github.com/skip2/go-qrcode)
39+
40+
## Demoapp
41+
42+
[http://go-qrcode.appspot.com](http://go-qrcode.appspot.com)
43+
44+
## CLI
45+
46+
A command-line tool `qrcode` will be built into `$GOPATH/bin/`.
47+
48+
```
49+
qrcode -- QR Code encoder in Go
50+
https://github.com/skip2/go-qrcode
51+
52+
Flags:
53+
-o string
54+
out PNG file prefix, empty for stdout
55+
-s int
56+
image size (pixel) (default 256)
57+
58+
Usage:
59+
1. Arguments except for flags are joined by " " and used to generate QR code.
60+
Default output is STDOUT, pipe to imagemagick command "display" to display
61+
on any X server.
62+
63+
qrcode hello word | display
64+
65+
2. Save to file if "display" not available:
66+
67+
qrcode "homepage: https://github.com/skip2/go-qrcode" > out.png
68+
```
69+
70+
## Links
71+
72+
- [http://en.wikipedia.org/wiki/QR_code](http://en.wikipedia.org/wiki/QR_code)
73+
- [ISO/IEC 18004:2006](http://www.iso.org/iso/catalogue_detail.htm?csnumber=43655) - Main QR Code specification (approx CHF 198,00)<br>
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)

qrcode.go

+17
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"image"
4242
"image/color"
4343
"image/png"
44+
"io"
4445
"io/ioutil"
4546
"log"
4647
"os"
@@ -278,6 +279,22 @@ func (q *QRCode) PNG(size int) ([]byte, error) {
278279
return b.Bytes(), nil
279280
}
280281

282+
// Write writes the QR Code as a PNG image to io.Writer.
283+
//
284+
// size is both the image width and height in pixels. If size is too small then
285+
// a larger image is silently written.
286+
func (q *QRCode) Write(size int, out io.Writer) error {
287+
var png []byte
288+
289+
png, err := q.PNG(size)
290+
291+
if err != nil {
292+
return err
293+
}
294+
_, err = out.Write(png)
295+
return err
296+
}
297+
281298
// WriteFile writes the QR Code as a PNG image to the specified file.
282299
//
283300
// size is both the image width and height in pixels. If size is too small then

qrcode/main.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// go-qrcode
2+
// Copyright 2014 Tom Harwood
3+
4+
package main
5+
6+
import (
7+
"flag"
8+
"fmt"
9+
"os"
10+
"strings"
11+
12+
qrcode "github.com/skip2/go-qrcode"
13+
)
14+
15+
func main() {
16+
outFile := flag.String("o", "", "out PNG file prefix, empty for stdout")
17+
size := flag.Int("s", 256, "image size (pixel)")
18+
flag.Usage = func() {
19+
fmt.Fprintf(os.Stderr, `qrcode -- QR Code encoder in Go
20+
https://github.com/skip2/go-qrcode
21+
22+
Flags:
23+
`)
24+
flag.PrintDefaults()
25+
fmt.Fprintf(os.Stderr, `
26+
Usage:
27+
1. Arguments except for flags are joined by " " and used to generate QR code.
28+
Default output is STDOUT, pipe to imagemagick command "display" to display
29+
on any X server.
30+
31+
qrcode hello word | display
32+
33+
2. Save to file if "display" not available:
34+
35+
qrcode "homepage: https://github.com/skip2/go-qrcode" > out.png
36+
37+
`)
38+
}
39+
flag.Parse()
40+
41+
if *size <= 0 {
42+
checkError(fmt.Errorf("Error: value of -s should > 0"))
43+
}
44+
if len(flag.Args()) == 0 {
45+
flag.Usage()
46+
checkError(fmt.Errorf("Error: no content given"))
47+
}
48+
49+
content := strings.Join(flag.Args(), " ")
50+
51+
var err error
52+
var q *qrcode.QRCode
53+
q, err = qrcode.New(content, qrcode.Highest)
54+
checkError(err)
55+
56+
var png []byte
57+
png, err = q.PNG(*size)
58+
checkError(err)
59+
60+
if *outFile == "" {
61+
os.Stdout.Write(png)
62+
} else {
63+
var fh *os.File
64+
fh, err = os.Create(*outFile + ".png")
65+
checkError(err)
66+
defer fh.Close()
67+
fh.Write(png)
68+
}
69+
}
70+
71+
func checkError(err error) {
72+
if err != nil {
73+
fmt.Fprintf(os.Stderr, "%s\n", err)
74+
os.Exit(0)
75+
}
76+
}

0 commit comments

Comments
 (0)