qr

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package qr can be used to create QR barcodes.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrValidGradient  = errors.New("invalid gradient")
	ErrInvalidBarcode = errors.New("invalid barcode")
)
View Source
var DotMask = [][]int{
	{0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0},
	{0, 0, 1, 1, 1, 0, 0},
	{0, 0, 1, 1, 1, 0, 0},
	{0, 0, 1, 1, 1, 0, 0},
	{0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0},
}
View Source
var ErrorCorrectionPercents = map[ErrorCorrectionLevel]float64{
	L: 0.07,
	M: 0.15,
	Q: 0.25,
	H: 0.3,
}
View Source
var QrTypes = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
	21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40}
View Source
var SquareMask = [][]int{
	{1, 1, 1, 1, 1, 1, 1},
	{1, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 1},
	{1, 1, 1, 1, 1, 1, 1},
}

Functions

func Encode

func Encode(content string, level ErrorCorrectionLevel, mode Encoding) (barcode.Barcode, error)

Encode returns a QR barcode with the given content, error correction level and uses the given encoding

Example
f, _ := os.Create("QRCode.png")
defer f.Close()

qrcode, err := Encode("hello world", L, Auto)
if err != nil {
	fmt.Println(err)
} else {
	code, err := barcode.Scale(qrcode, 100, 100)
	if err != nil {
		fmt.Println(err)
	} else {
		png.Encode(f, code)
	}
}
Output:

Types

type BackgroundOption added in v1.2.0

type BackgroundOption common.Color

type Canvas added in v1.2.0

type Canvas struct {
	// contains filtered or unexported fields
}

func NewCanvas added in v1.2.0

func NewCanvas(option CanvasOption, code barcode.Barcode) *Canvas

func (*Canvas) Clear added in v1.2.0

func (c *Canvas) Clear()

func (*Canvas) Draw added in v1.2.0

func (c *Canvas) Draw() (image.Image, error)

Draw 必须要记住的一点是 canvas 库的左下角为坐标原点,而二维码则是左上角为坐标原点

type CanvasOption added in v1.2.0

type CanvasOption struct {
	Width  int
	Height int
	Margin int

	TypeNumber           int
	Encoding             Encoding
	ErrorCorrectionLevel ErrorCorrectionLevel

	DotOption          feature.DotOption
	CornerSquareOption feature.CornerSquareOption
	CornerDotOption    feature.CornerDotOption
	BackgroundOption   BackgroundOption
	Image              string
	ImageOption        ImageOption

	HalftoneOption HalftoneOption
}

func (CanvasOption) Merge added in v1.2.0

func (o CanvasOption) Merge(other CanvasOption)

type Code added in v1.3.0

type Code struct {
	// contains filtered or unexported fields
}

func (*Code) At added in v1.3.0

func (qr *Code) At(x, y int) color.Color

func (*Code) Bounds added in v1.3.0

func (qr *Code) Bounds() image.Rectangle

func (*Code) ColorModel added in v1.3.0

func (qr *Code) ColorModel() color.Model

func (*Code) Content added in v1.3.0

func (qr *Code) Content() string

func (*Code) Dimension added in v1.3.0

func (qr *Code) Dimension() int

func (*Code) Get added in v1.3.0

func (qr *Code) Get(x, y int) bool

func (*Code) Metadata added in v1.3.0

func (qr *Code) Metadata() barcode.Metadata

func (*Code) Set added in v1.3.0

func (qr *Code) Set(x, y int, val bool)

type Encoding

type Encoding byte

Encoding mode for QR Codes.

const (
	// Auto will choose ths best matching encoding
	Auto Encoding = iota
	// Numeric encoding only encodes numbers [0-9]
	Numeric
	// AlphaNumeric encoding only encodes uppercase letters, numbers and  [Space], $, %, *, +, -, ., /, :
	AlphaNumeric
	// Unicode encoding encodes the string as utf-8
	Unicode
)

func (Encoding) String

func (e Encoding) String() string

type ErrorCorrectionLevel

type ErrorCorrectionLevel byte

ErrorCorrectionLevel indicates the amount of "backup data" stored in the QR code

const (
	// L recovers 7% of data
	L ErrorCorrectionLevel = iota
	// M recovers 15% of data
	M
	// Q recovers 25% of data
	Q
	// H recovers 30% of data
	H
)

func (ErrorCorrectionLevel) String

func (ecl ErrorCorrectionLevel) String() string

type Filter added in v1.2.0

type Filter func(i, j int) bool

type HalftoneOption added in v1.2.0

type HalftoneOption struct {
	HalftoneImage image.Image
}

type ImageOption added in v1.2.0

type ImageOption struct {
	HideBackgroundDot bool
	ImageSize         float64
	Margin            float64
}

type Option added in v1.2.0

type Option func(o *CanvasOption)

func WithBackgroundOption added in v1.2.0

func WithBackgroundOption(backgroundOption BackgroundOption) Option

func WithCornerDotOption added in v1.2.0

func WithCornerDotOption(cornerDotOption feature.CornerDotOption) Option

func WithCornerSquareOption added in v1.2.0

func WithCornerSquareOption(cornerSquareOption feature.CornerSquareOption) Option

func WithDotOption added in v1.2.0

func WithDotOption(dotOption feature.DotOption) Option

func WithHalftoneOption added in v1.2.0

func WithHalftoneOption(halftoneOption HalftoneOption) Option

func WithImageOption added in v1.2.0

func WithImageOption(imageOption ImageOption) Option

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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