Documentation ¶
Overview ¶
Package qrcode implements a QR Code encoder.
A QR Code is a matrix (two-dimensional) barcode. Arbitrary content may be encoded.
A QR Code contains error recovery information to aid reading damaged or obscured codes. There are four levels of error recovery: qrcode.{Low, Medium, High, Highest}. QR Codes with a higher recovery level are more robust to damage, at the cost of being physically larger.
Two functions cover most use cases:
- 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.
This package implements a subset of QR Code 2005, as defined in ISO/IEC 18004:2006.
Index ¶
- func Encode(content string, level RecoveryLevel, size int) ([]byte, error)
- func EncodeWithFixPadding(content string, level RecoveryLevel, expectSize, padding int) ([]byte, error)
- func WriteFile(content string, level RecoveryLevel, size int, filename string) error
- type QRCode
- func (q *QRCode) Bitmap() [][]bool
- func (q *QRCode) Image(size int) image.Image
- func (q *QRCode) Image2(expectSize, offset int) image.Image
- func (q *QRCode) PNG(size int) ([]byte, error)
- func (q *QRCode) PNG2(expectSize, padding int) ([]byte, error)
- func (q *QRCode) Write(size int, out io.Writer) error
- func (q *QRCode) WriteFile(size int, filename string) error
- type RecoveryLevel
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Encode ¶
func Encode(content string, level RecoveryLevel, size int) ([]byte, error)
Encode a QR Code and return a raw PNG image.
size is both the image width and height in pixels. If size is too small then a larger image is silently returned.
To serve over HTTP, remember to send a Content-Type: image/png header.
Example ¶
var png []byte png, err := Encode("https://example.org", Medium, 256) if err != nil { fmt.Printf("Error: %s", err.Error()) } else { fmt.Printf("PNG is %d bytes long", len(png)) }
Output:
func EncodeWithFixPadding ¶
func EncodeWithFixPadding(content string, level RecoveryLevel, expectSize, padding int) ([]byte, error)
func WriteFile ¶
func WriteFile(content string, level RecoveryLevel, size int, filename string) error
WriteFile encodes, then writes a QR Code to the given filename in PNG format.
size is both the width and height in pixels. If size is too small then a larger image is silently written.
Example ¶
filename := "example.png" err := WriteFile("https://example.org", Medium, 256, filename) if err != nil { err = os.Remove(filename) } if err != nil { fmt.Printf("Error: %s", err.Error()) }
Output:
Types ¶
type QRCode ¶
type QRCode struct { // Original content encoded. Content string // QR Code type. Level RecoveryLevel VersionNumber int // User settable drawing options. ForegroundColor color.Color BackgroundColor color.Color // contains filtered or unexported fields }
A QRCode represents a valid encoded QRCode.
func New ¶
func New(content string, level RecoveryLevel) (*QRCode, error)
New constructs a QRCode.
var q *qrcode.QRCode q, err := qrcode.New("my content", qrcode.Medium)
An error occurs if the content is too long.
func (*QRCode) Bitmap ¶
Bitmap returns the QR Code as a 2D array of 1-bit pixels.
bitmap[y][x] is true if the pixel at (x, y) is set.
The bitmap includes the required "quiet zone" around the QR Code to aid decoding.
func (*QRCode) Image ¶
Image returns the QR Code as an image.Image.
size is both the width and height in pixels.
func (*QRCode) Image2 ¶
Image returns the QR Code as an image.Image.
size is both the width and height in pixels.
func (*QRCode) PNG ¶
PNG returns the QR Code as a PNG image.
size is both the image width and height in pixels. If size is too small then a larger image is silently returned.
func (*QRCode) PNG2 ¶
PNG returns the QR Code as a PNG image.
size is both the image width and height in pixels. If size is too small then a larger image is silently returned.
type RecoveryLevel ¶
type RecoveryLevel int
Error detection/recovery capacity.
There are several levels of error detection/recovery capacity. Higher levels of error recovery are able to correct more errors, with the trade-off of increased symbol size.
const ( // Level L: 7% error recovery. Low RecoveryLevel = iota // Level M: 15% error recovery. Good default choice. Medium // Level Q: 25% error recovery. High // Level H: 30% error recovery. Highest )
Directories ¶
Path | Synopsis |
---|---|
Package bitset implements an append only bit array.
|
Package bitset implements an append only bit array. |
Package reedsolomon provides error correction encoding for QR Code 2005.
|
Package reedsolomon provides error correction encoding for QR Code 2005. |