Documentation
¶
Overview ¶
Package qrcode is the Go port of simplesoftwareio/simple-qrcode for styled SVG, PNG and EPS QR codes.
Community package, not affiliated with Laravel.
Example ¶
The quick start: the README shows this example, and CI runs it.
g, err := qrcode.New(qrcode.Size(200), qrcode.Margin(4))
if err != nil {
log.Fatal(err)
}
svg, err := g.SVG("https://example.com")
if err != nil {
log.Fatal(err)
}
fmt.Println(strings.HasPrefix(string(svg), `<svg xmlns="http://www.w3.org/2000/svg" width="200"`))
src, err := g.DataURI("https://example.com", qrcode.PNGFormat)
if err != nil {
log.Fatal(err)
}
fmt.Println(strings.HasPrefix(src, "data:image/png;base64,"))
Output: true true
Example (Payload) ¶
password := "guest-code"
text, err := qrcode.WiFi{SSID: "Guest", Auth: qrcode.WiFiWPA, Password: &password}.Payload()
if err != nil {
log.Fatal(err)
}
fmt.Println(text)
Output: WIFI:T:WPA;S:Guest;P:guest-code;
Index ¶
- Constants
- Variables
- type Bitcoin
- type Email
- type Eye
- type EyeShape
- type Format
- type Generator
- func (g Generator) DataURI(text string, format Format) (string, error)
- func (g Generator) EPS(text string) ([]byte, error)
- func (g Generator) Encode(text string) (Matrix, error)
- func (g Generator) Generate(text string, format Format) ([]byte, error)
- func (g Generator) PNG(text string) ([]byte, error)
- func (g Generator) SVG(text string) ([]byte, error)
- func (g Generator) With(opts ...Option) (Generator, error)
- func (g Generator) WriteTo(w io.Writer, text string, format Format) error
- type Geo
- type GradientType
- type Level
- type Matrix
- type Option
- func Background(c color.NRGBA) Option
- func Encoding(name string) Option
- func ErrorCorrection(level Level) Option
- func EyeColor(eye Eye, outer, inner color.NRGBA) Option
- func EyeStyle(shape EyeShape) Option
- func Foreground(c color.NRGBA) Option
- func Gradient(from, to color.NRGBA, kind GradientType) Option
- func Logo(source image.Image, fraction float64) Option
- func LogoBytes(data []byte, fraction float64) Option
- func Margin(modules int) Option
- func ModuleStyle(style Style, amount float64) Option
- func Size(px int) Option
- type PhoneNumber
- type SMS
- type Style
- type WiFi
- type WiFiAuth
Examples ¶
Constants ¶
const MaxSize = 4096
MaxSize is the largest supported width and height in pixels.
const Version = "0.1.0"
Version is the version of this module.
Variables ¶
var ( // ErrInvalidOption indicates an option or option combination cannot be used. ErrInvalidOption = errors.New("qrcode: invalid option") // ErrInvalidPayload indicates empty text or an invalid structured payload. ErrInvalidPayload = errors.New("qrcode: invalid payload") // ErrUnencodable indicates text cannot be encoded with the selected character set. ErrUnencodable = errors.New("qrcode: text cannot be encoded") // ErrDataTooLong indicates the payload exceeds QR capacity at the selected correction level. ErrDataTooLong = errors.New("qrcode: payload exceeds QR capacity") )
Functions ¶
This section is empty.
Types ¶
type Bitcoin ¶
type Bitcoin struct {
// Address is a caller-provided Bitcoin address.
Address string
// Amount is an optional decimal string denominated in BTC.
Amount *string
// Label is the optional recipient label.
Label *string
// Message is the optional payment message.
Message *string
// ReturnURL fills the legacy r query parameter if provided.
ReturnURL *string
}
Bitcoin builds a payment request payload, equivalent to PHP BTC.
type Email ¶
type Email struct {
// To is an optional single mailbox address.
To string
// Subject is the optional subject line.
Subject *string
// Body is the optional message body.
Body *string
}
Email builds a mailto payload, equivalent to PHP email. Nil fields are omitted.
type Format ¶
type Format uint8
Format selects a rendered output type.
func (Format) ContentType ¶
ContentType returns the format's MIME type, or an empty string for invalid formats.
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator holds reusable QR settings. Its zero value uses the default settings.
func (Generator) EPS ¶
EPS renders an encapsulated PostScript document, equivalent to PHP generate with format eps.
type Geo ¶
type Geo struct {
// Latitude is in decimal degrees from -90 to 90.
Latitude float64
// Longitude is in decimal degrees from -180 to 180.
Longitude float64
}
Geo builds a geographic location payload, equivalent to PHP geo.
type GradientType ¶
type GradientType uint8
GradientType determines the orientation of a two-color foreground gradient.
const ( // Vertical runs from top to bottom. Vertical GradientType = iota // Horizontal runs from left to right. Horizontal // Diagonal runs from top left to bottom right. Diagonal // InverseDiagonal runs from bottom left to top right. InverseDiagonal // Radial runs from the center to the outer edge. Radial )
type Matrix ¶
type Matrix struct {
// contains filtered or unexported fields
}
Matrix is an immutable QR symbol without a quiet zone. Coordinates are zero-based.
func (Matrix) At ¶
At reports whether the module at column x, row y is dark. Outside the symbol it returns false.
type Option ¶
Option configures a Generator. Use the option functions to construct one.
func Background ¶
Background sets the symbol's background color, equivalent to PHP backgroundColor.
func Encoding ¶
Encoding selects the symbol's character set, equivalent to PHP encoding. The text passed to Encode and the render methods is always UTF-8.
func ErrorCorrection ¶
ErrorCorrection selects the correction level, equivalent to PHP errorCorrection.
func EyeColor ¶
EyeColor colors one finder pattern's outer ring and inner center, equivalent to PHP eyeColor. The arguments are named for their physical positions.
func Foreground ¶
Foreground sets the default color of dark modules, equivalent to PHP color.
func Gradient ¶
func Gradient(from, to color.NRGBA, kind GradientType) Option
Gradient applies a two-color foreground blend, equivalent to PHP gradient. Explicit finder colors override it.
func Logo ¶
Logo adds a centered logo to PNG output, equivalent to PHP merge. Fraction sets the logo's maximum width relative to the output, typically 0.2. The option snapshots the image and can be reused across generators.
func LogoBytes ¶
LogoBytes decodes PNG, JPEG or the first frame of a GIF, equivalent to PHP mergeString.
func ModuleStyle ¶
ModuleStyle sets the shape and diameter or roundness, equivalent to PHP style. Square ignores amount. Use 0.5 for PHP's default amount.
type PhoneNumber ¶
type PhoneNumber struct {
// Number is the telephone number, which may contain vanity letters.
Number string
}
PhoneNumber builds a telephone payload, equivalent to PHP phoneNumber.
func (PhoneNumber) Payload ¶
func (p PhoneNumber) Payload() (string, error)
Payload returns a tel URI.
type SMS ¶
type SMS struct {
// Number is the message recipient.
Number string
// Body is the optional message to prefill.
Body *string
}
SMS builds a text message payload, equivalent to PHP SMS.
type WiFi ¶
type WiFi struct {
// SSID is the network name.
SSID string
// Auth is the authentication scheme.
Auth WiFiAuth
// Password is optional. Nil omits the field.
Password *string
// Hidden states whether to connect to a hidden network. Nil omits the field.
Hidden *bool
}
WiFi builds a scanner configuration payload, equivalent to PHP WiFi.
type WiFiAuth ¶
type WiFiAuth string
WiFiAuth names a network's authentication scheme.
const ( // WiFiOpen omits the authentication field. WiFiOpen WiFiAuth = "" // WiFiNoPassword explicitly declares an open network. WiFiNoPassword WiFiAuth = "nopass" // WiFiWEP is WEP authentication. WiFiWEP WiFiAuth = "WEP" // WiFiWPA is WPA or WPA2 authentication. WiFiWPA WiFiAuth = "WPA" // WiFiSAE is WPA3-Personal authentication. WiFiSAE WiFiAuth = "SAE" )