qrcode

package module
v0.0.0-...-c335135 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2018 License: MIT Imports: 17 Imported by: 1

README

go-qrcode

Package qrcode implements a QR Code encoder. Build Status

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/yougg/go-qrcode/...

A command-line tool qrcode will be built into $GOPATH/bin/.

Usage

import qrcode "github.com/yougg/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")
    
  • Create a PNG image with custom colors and write to file:

      err := qrcode.WriteColorFile("https://example.org", qrcode.Medium, 256, color.Black, color.White, "qr.png")
    
  • Create a PNG image with logo and custom size and margin: code ,err:=qrcode.EncodeWithLogo(qrcode.Medium, "123", logo, 100, 200, 5) //The function define: func EncodeWithLogo(level RecoveryLevel, str string, logo image.Image,width, height, margin int) (*bytes.Buffer, error){xxx}

  • Create a png qr image with image file:

      pngQr := qrcode.ImageGenerator(qrCode,"background.jpg",200)
    
  • Create a gif qr image with gif file:

      gifQr := qrcode.GifGenerator(qrCode,"background.gif",200)
    

All examples use the qrcode.Medium error Recovery Level and create a fixed 256x256px size QR Code. The last function creates a white on black instead of 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.

TestImage

avatar avatar

Documentation

godoc

Demoapp

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/yougg/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/yougg/go-qrcode" > out.png

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.

Three 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")

- Create a PNG image with custom colors and write to file:

err := qrcode.WriteColorFile("https://example.org", qrcode.Medium, 256, color.Black, color.White, "qr.png")

All examples use the qrcode.Medium error Recovery Level and create a fixed 256x256px size QR Code. The last function creates a white on black instead of black on white QR Code.

To generate a variable sized image instead, specify a negative size (in place of the 256 above), such as -4 or -5. Larger negative numbers create larger images: A size of -5 sets each module (QR Code "pixel") to be 5px wide/high.

- Create a PNG image (variable size, with minimum white padding) and write to a file:

err := qrcode.WriteFile("https://example.org", qrcode.Medium, -5, "qr.png")

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Encode

func Encode(content string, level RecoveryLevel, width, height, margin 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. Negative values for size cause a variable sized image to be returned: See the documentation for Image().

To serve over HTTP, remember to send a Content-Type: image/png header.

func EncodeWithLogo(level RecoveryLevel, str string, logo image.Image, margin int) (*bytes.Buffer, error)

func GifGenerator

func GifGenerator(q *QRCode, g gif.GIF, size int) *gif.GIF

GifGenerator can generate a gif qr code

func ImageGenerator

func ImageGenerator(q *QRCode, g image.Image, size int) image.Image

ImageGenerator can generate a artistic qr code

func WriteColorFile

func WriteColorFile(content string, level RecoveryLevel, size int, background, foreground color.Color, filename string, margin int) error

WriteColorFile encodes, then writes a QR Code to the given filename in PNG format. With WriteColorFile you can also specify the colors you want to use.

size is both the image width and height in pixels. If size is too small then a larger image is silently written. Negative values for size cause a variable sized image to be written: See the documentation for Image().

func WriteFile

func WriteFile(content string, level RecoveryLevel, size int, filename string, margin int) error

WriteFile encodes, then writes a QR Code to the given filename in PNG format.

size is both the image width and height in pixels. If size is too small then a larger image is silently written. Negative values for size cause a variable sized image to be written: See the documentation for Image().

Types

type Option

type Option func(q *QRCode)

func BackgroundColor

func BackgroundColor(c color.Color) Option

func ForegroundColor

func ForegroundColor(c color.Color) Option

func Height

func Height(h int) Option

func Level

func Level(l RecoveryLevel) Option

func Margin

func Margin(m int) Option

func QuitZoneSize

func QuitZoneSize(s int) Option

func Version

func Version(v int) Option

func Width

func Width(w int) Option

type Point

type Point struct {
	X []Uniterm
	Y []Uniterm
}

type QRCode

type QRCode struct {
	// Original content encoded.
	Content string

	VersionNumber int

	// User settable drawing options.
	ForegroundColor color.Color
	BackgroundColor color.Color

	// set white space size.
	QuitZoneSize int
	// contains filtered or unexported fields
}

A QRCode represents a valid encoded QRCode.

func New

func New(content string, opts ...Option) (*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

func (q *QRCode) Bitmap() [][]bool

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

func (q *QRCode) Image() image.Image

Image returns the QR Code as an image.Image.

A positive size sets a fixed image width and height (e.g. 256 yields an 256x256px image).

Depending on the amount of data encoded, fixed size images can have different amounts of padding (white space around the QR Code). As an alternative, a variable sized image can be generated instead:

A negative size causes a variable sized image to be returned. The image returned is the minimum size required for the QR Code. Choose a larger negative number to increase the scale of the image. e.g. a size of -5 causes each module (QR Code "pixel") to be 5px in size.

func (*QRCode) PNG

func (q *QRCode) PNG() ([]byte, error)

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. Negative values for size cause a variable sized image to be returned: See the documentation for Image().

func (*QRCode) Set

func (q *QRCode) Set(opts ...Option)

func (*QRCode) ToString

func (q *QRCode) ToString(inverseColor bool) string

ToString produces a multi-line string that forms a QR-code image.

func (*QRCode) Write

func (q *QRCode) Write(out io.Writer) error

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. Negative values for size cause a variable sized image to be written: See the documentation for Image().

func (*QRCode) WriteFile

func (q *QRCode) WriteFile(filename string) error

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 a larger image is silently written. Negative values for size cause a variable sized image to be written: See the documentation for Image().

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
)

type Uniterm

type Uniterm struct {
	Coefficient float64 //系数
	Variable    string  //变量
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL