Documentation
¶
Overview ¶
Package qrcode provides a high-performance QR code generation library in pure Go with zero external dependencies.
Index ¶
- func BoolP(v bool) *bool
- func ContextWithQR(ctx context.Context, client Client) context.Context
- func DefaultModuleStyle() *renderer.ModuleStyle
- func DurationP(v time.Duration) *time.Duration
- func Float64P(v float64) *float64
- func GetRenderer(f Format) (renderer.Renderer, error)
- func IntP(v int) *int
- func NewBase64Renderer() *renderer.Base64Renderer
- func NewPDFRenderer() *renderer.PDFRenderer
- func NewPNGRenderer() *renderer.PNGRenderer
- func NewSVGRenderer() *renderer.SVGRenderer
- func NewTerminalRenderer() *renderer.TerminalRenderer
- func Quick(data string, size ...int) ([]byte, error)
- func QuickContact(firstName, lastName, phone, email string, size ...int) ([]byte, error)
- func QuickEmail(to, subject, body string, size ...int) ([]byte, error)
- func QuickEvent(title, location string, start, end time.Time, size ...int) ([]byte, error)
- func QuickFile(data, path string, size ...int) error
- func QuickGeo(lat, lng float64, size ...int) ([]byte, error)
- func QuickPayment(username, amount string, size ...int) ([]byte, error)
- func QuickSMS(phone, message string, size ...int) ([]byte, error)
- func QuickSVG(data string, size ...int) (string, error)
- func QuickURL(url string, size ...int) ([]byte, error)
- func QuickWiFi(ssid, password, encryption string, size ...int) ([]byte, error)
- func StringP(v string) *string
- func ValidatePatch(patch *ConfigPatch) error
- type Builder
- func (b *Builder) AutoSize(auto bool) *Builder
- func (b *Builder) BackgroundColor(color string) *Builder
- func (b *Builder) Build() (Client, error)
- func (b *Builder) Clone() *Builder
- func (b *Builder) ErrorCorrection(level ECLevel) *Builder
- func (b *Builder) ForegroundColor(color string) *Builder
- func (b *Builder) Format(f Format) *Builder
- func (b *Builder) Logo(source string, sizeRatio float64) *Builder
- func (b *Builder) LogoOverlay(enabled bool) *Builder
- func (b *Builder) LogoTint(color string) *Builder
- func (b *Builder) Margin(margin int) *Builder
- func (b *Builder) MaskPattern(pattern int) *Builder
- func (b *Builder) MaxVersion(v int) *Builder
- func (b *Builder) MinVersion(v int) *Builder
- func (b *Builder) MustBuild() Client
- func (b *Builder) Options(opts ...Option) *Builder
- func (b *Builder) Prefix(prefix string) *Builder
- func (b *Builder) QueueSize(n int) *Builder
- func (b *Builder) Quick(data string, size ...int) ([]byte, error)
- func (b *Builder) QuickContact(firstName, lastName, phone, email string, size ...int) ([]byte, error)
- func (b *Builder) QuickEmail(to, subject, body string, size ...int) ([]byte, error)
- func (b *Builder) QuickEvent(title, location string, start, end time.Time, size ...int) ([]byte, error)
- func (b *Builder) QuickFile(data, path string, size ...int) error
- func (b *Builder) QuickGeo(lat, lng float64, size ...int) ([]byte, error)
- func (b *Builder) QuickPayment(username, amount string, size ...int) ([]byte, error)
- func (b *Builder) QuickSMS(phone, message string, size ...int) ([]byte, error)
- func (b *Builder) QuickSVG(data string, size ...int) (string, error)
- func (b *Builder) QuickURL(url string, size ...int) ([]byte, error)
- func (b *Builder) QuickWiFi(ssid, password, encryption string, size ...int) ([]byte, error)
- func (b *Builder) Size(size int) *Builder
- func (b *Builder) Version(v int) *Builder
- func (b *Builder) WorkerCount(n int) *Builder
- type Client
- type Config
- type ConfigPatch
- type ECLevel
- type Format
- type ModuleStyle
- type Option
- func WithAutoSize(auto bool) Option
- func WithBackgroundColor(color string) Option
- func WithDefaultFormat(f Format) Option
- func WithDefaultSize(size int) Option
- func WithErrorCorrection(level ECLevel) Option
- func WithForegroundColor(color string) Option
- func WithLogo(logoSource string, sizeRatio float64) Option
- func WithLogoOverlay(enabled bool) Option
- func WithLogoTint(color string) Option
- func WithMaskPattern(pattern int) Option
- func WithMaxVersion(v int) Option
- func WithMinVersion(v int) Option
- func WithPrefix(prefix string) Option
- func WithQueueSize(n int) Option
- func WithQuietZone(zone int) Option
- func WithSlowOperation(d time.Duration) Option
- func WithVersion(v int) Option
- func WithWorkerCount(n int) Option
- type RenderOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextWithQR ¶
ContextWithQR stores a Client in a context value.
func DefaultModuleStyle ¶
func DefaultModuleStyle() *renderer.ModuleStyle
DefaultModuleStyle is a convenience re-export.
func GetRenderer ¶
GetRenderer returns the renderer for the given format. This re-export allows consumers to access renderers without importing internal packages.
func NewBase64Renderer ¶
func NewBase64Renderer() *renderer.Base64Renderer
NewBase64Renderer returns a new Base64 renderer for direct use.
func NewPDFRenderer ¶
func NewPDFRenderer() *renderer.PDFRenderer
NewPDFRenderer returns a new PDF renderer for direct use.
func NewPNGRenderer ¶
func NewPNGRenderer() *renderer.PNGRenderer
NewPNGRenderer returns a new PNG renderer for direct use.
func NewSVGRenderer ¶
func NewSVGRenderer() *renderer.SVGRenderer
NewSVGRenderer returns a new SVG renderer for direct use.
func NewTerminalRenderer ¶
func NewTerminalRenderer() *renderer.TerminalRenderer
NewTerminalRenderer returns a new terminal renderer for direct use.
func QuickContact ¶
QuickContact generates a vCard QR code as PNG bytes.
func QuickEmail ¶
QuickEmail generates an email QR code as PNG bytes.
func QuickEvent ¶
QuickEvent generates a calendar event QR code as PNG bytes.
func QuickFile ¶
QuickFile generates a QR code and saves it to a file. The output format is inferred from the file extension (.png, .svg, .pdf, .txt).
func QuickPayment ¶
QuickPayment generates a PayPal payment QR code as PNG bytes.
func ValidatePatch ¶
func ValidatePatch(patch *ConfigPatch) error
ValidatePatch checks a ConfigPatch for invalid values before applying. Only non-nil fields are validated. Returns nil if all set fields are valid.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder provides a fluent API for constructing a Client. It is a convenience wrapper — all actual generation is delegated to the Client.
func NewBuilder ¶
func NewBuilder() *Builder
NewBuilder creates a new Builder with default configuration.
func (*Builder) BackgroundColor ¶
BackgroundColor sets the background color (hex, e.g., "#FFFFFF").
func (*Builder) ErrorCorrection ¶
ErrorCorrection sets the error correction level.
func (*Builder) ForegroundColor ¶
ForegroundColor sets the foreground color (hex, e.g., "#000000").
func (*Builder) LogoOverlay ¶
LogoOverlay enables or disables logo overlay.
func (*Builder) MaskPattern ¶
MaskPattern sets the mask pattern (-1 for auto, 0-7).
func (*Builder) MaxVersion ¶
MaxVersion sets the maximum QR code version.
func (*Builder) MinVersion ¶
MinVersion sets the minimum QR code version.
func (*Builder) QuickContact ¶
func (b *Builder) QuickContact(firstName, lastName, phone, email string, size ...int) ([]byte, error)
QuickContact generates a vCard QR code as PNG bytes.
func (*Builder) QuickEmail ¶
QuickEmail generates an email QR code as PNG bytes.
func (*Builder) QuickEvent ¶
func (b *Builder) QuickEvent(title, location string, start, end time.Time, size ...int) ([]byte, error)
QuickEvent generates a calendar event QR code as PNG bytes.
func (*Builder) QuickPayment ¶
QuickPayment generates a PayPal QR code as PNG bytes.
func (*Builder) WorkerCount ¶
WorkerCount sets the maximum concurrent workers for batch operations.
type Client ¶
type Client interface {
// Generate encodes a payload into a QR code matrix without rendering.
// Returns the raw QR code structure (modules, version, size).
Generate(ctx context.Context, p payload.Payload) (*encoding.QRCode, error)
// GenerateWithOptions encodes a payload with per-call option overrides.
// Options are applied to a copy of the client's config — the client's
// default configuration is not modified.
GenerateWithOptions(ctx context.Context, p payload.Payload, opts ...Option) (*encoding.QRCode, error)
// GenerateToWriter encodes a payload, renders it, and writes the result
// to w in the specified format. This is the streaming variant of Render
// for callers who want to pipe output directly (e.g., HTTP responses).
GenerateToWriter(ctx context.Context, p payload.Payload, w io.Writer, format Format) error
// Render encodes a payload and returns the rendered output as bytes.
// This is the unified replacement for the former GeneratePNG/SVG/ASCII/Base64 functions.
Render(ctx context.Context, p payload.Payload, format Format) ([]byte, error)
// Save encodes a payload, renders it, and writes the result to a file.
// The output format is inferred from the file extension (.png, .svg, .pdf, .txt, .b64).
Save(ctx context.Context, p payload.Payload, path string) error
// Batch generates multiple QR codes concurrently with bounded worker count.
// Results are returned in the same order as the input payloads.
// Optional per-call options are applied uniformly to all items.
Batch(ctx context.Context, payloads []payload.Payload, opts ...Option) ([]*encoding.QRCode, error)
// Close releases all resources held by the client.
// After Close is called, all other methods return ErrCodeClosed errors.
Close() error
// SetOptions updates the client's default configuration.
// The new configuration is validated before being applied.
// This is safe for concurrent use.
SetOptions(opts ...Option) error
// Closed reports whether the client has been closed.
Closed() bool
}
Client is the primary interface for QR code generation and rendering. It is the stable public contract that all implementations must satisfy. Use New() or NewClient() to create instances; avoid depending on the concrete generator type directly.
All methods accept context.Context as the first parameter, enabling cancellation, timeout, and observability propagation.
func MustNew ¶
MustNew creates a new Client, panicking on invalid configuration. Use this only in package-level init functions or tests where failure is fatal.
func MustNewClient ¶
MustNewClient creates a new Client, panicking on invalid configuration.
func New ¶
New creates a new Client with the given options. It validates the configuration before returning; an error is returned for invalid configurations (e.g., size outside 100-4000, worker count outside 1-64).
This is the primary constructor for the QR code library.
type Config ¶
type Config struct {
DefaultVersion int `json:"default_version"`
DefaultECLevel string `json:"default_ec_level"`
MinVersion int `json:"min_version"`
MaxVersion int `json:"max_version"`
AutoSize bool `json:"auto_size"`
WorkerCount int `json:"worker_count"`
QueueSize int `json:"queue_size"`
DefaultFormat string `json:"default_format"`
DefaultSize int `json:"default_size"`
QuietZone int `json:"quiet_zone"`
ForegroundColor string `json:"foreground_color"`
BackgroundColor string `json:"background_color"`
MaskPattern int `json:"mask_pattern"`
LogoSource string `json:"logo_source"`
LogoSizeRatio float64 `json:"logo_size_ratio"`
LogoOverlay bool `json:"logo_overlay"`
LogoTint string `json:"logo_tint"`
Prefix string `json:"prefix"`
SlowOperation time.Duration `json:"slow_operation"`
}
Config holds all configuration for QR code generation. Fields use value types (not pointers) for straightforward usage. To selectively override specific fields without zero-value ambiguity, use ConfigPatch with ApplyPatch.
func ApplyPatch ¶
func ApplyPatch(base *Config, patch *ConfigPatch) *Config
ApplyPatch applies all non-nil fields from patch to base, returning a new Config. The original base is not modified.
type ConfigPatch ¶
type ConfigPatch struct {
DefaultVersion *int
DefaultECLevel *string
MinVersion *int
MaxVersion *int
AutoSize *bool
WorkerCount *int
QueueSize *int
DefaultFormat *string
DefaultSize *int
QuietZone *int
ForegroundColor *string
BackgroundColor *string
MaskPattern *int
LogoSource *string
LogoSizeRatio *float64
LogoOverlay *bool
LogoTint *string
Prefix *string
SlowOperation *time.Duration
}
ConfigPatch provides explicit, zero-value-safe overrides for Config. Only fields that are non-nil are applied — nil fields are treated as "no change", eliminating the ambiguity of the old Merge method where zero values (0, "", false) could not be distinguished from "not set".
Example:
patch := ConfigPatch{
WorkerCount: intPtr(8),
AutoSize: boolPtr(false),
}
cfg := ApplyPatch(defaultConfig(), &patch)
func ConfigToPatch ¶
func ConfigToPatch(c *Config) ConfigPatch
ConfigToPatch creates a ConfigPatch from a Config, where every field is set. This is useful when you want to serialize or compare a full config as a patch.
type ECLevel ¶
type ECLevel int
ECLevel represents the QR code error correction level. Higher levels provide more error recovery capability but reduce data capacity.
type Format ¶
type Format int
Format represents the output format for a rendered QR code.
const ( // FormatPNG encodes the QR code as a PNG image. FormatPNG Format = iota // FormatSVG encodes the QR code as an SVG document. FormatSVG // FormatTerminal encodes the QR code as terminal/ANSI block characters. FormatTerminal // FormatPDF encodes the QR code as a PDF document. FormatPDF // FormatBase64 encodes the QR code as a Base64 data URL (PNG-based). FormatBase64 )
func FormatFromPath ¶
FormatFromPath returns the QR format inferred from a file path's extension.
type ModuleStyle ¶
type ModuleStyle = renderer.ModuleStyle
ModuleStyle is a type alias for the internal renderer's ModuleStyle, exposed for convenience when using GetRenderer or New*Renderer directly.
type Option ¶
type Option func(*Config)
func WithAutoSize ¶
func WithBackgroundColor ¶
func WithDefaultFormat ¶
func WithDefaultSize ¶
func WithErrorCorrection ¶
func WithForegroundColor ¶
func WithLogoOverlay ¶
func WithLogoTint ¶
func WithMaskPattern ¶
func WithMaxVersion ¶
func WithMinVersion ¶
func WithPrefix ¶
func WithQueueSize ¶
func WithQuietZone ¶
func WithSlowOperation ¶
func WithVersion ¶
func WithWorkerCount ¶
type RenderOption ¶
type RenderOption = renderer.RenderOption
RenderOption is a type alias for the internal renderer's RenderOption.
func WithCircleModules ¶
func WithCircleModules() RenderOption
WithCircleModules is a convenience re-export.
func WithGradient ¶
func WithGradient(startColor, endColor string, angle float64) RenderOption
WithGradient is a convenience re-export.
func WithModuleStyle ¶
func WithModuleStyle(style *renderer.ModuleStyle) RenderOption
WithModuleStyle is a convenience re-export.
func WithRoundedModules ¶
func WithRoundedModules(roundness float64) RenderOption
WithRoundedModules is a convenience re-export.
func WithTransparency ¶
func WithTransparency(alpha float64) RenderOption
WithTransparency is a convenience re-export.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package errors provides enterprise-grade error handling for the QR code library.
|
Package errors provides enterprise-grade error handling for the QR code library. |
|
examples
|
|
|
api-server
command
Package main implements a minimal HTTP API server for QR code generation.
|
Package main implements a minimal HTTP API server for QR code generation. |
|
basic
command
Package main demonstrates basic QR code generation using the qrcode library.
|
Package main demonstrates basic QR code generation using the qrcode library. |
|
batch
command
Package main demonstrates batch QR code generation using the batch.Processor.
|
Package main demonstrates batch QR code generation using the batch.Processor. |
|
cli
command
Package main implements a simple CLI tool for QR code generation.
|
Package main implements a simple CLI tool for QR code generation. |
|
concurrent
command
Package main demonstrates concurrent QR code generation using both client.Batch() and batch.Processor with ProcessWithStats.
|
Package main demonstrates concurrent QR code generation using both client.Batch() and batch.Processor with ProcessWithStats. |
|
custom-config
command
Package main demonstrates custom configuration options for QR code generation.
|
Package main demonstrates custom configuration options for QR code generation. |
|
logo-overlay
command
Package main demonstrates logo overlay on QR codes.
|
Package main demonstrates logo overlay on QR codes. |
|
multi-format
command
Package main generates the same QR code in all 5 supported formats: PNG, SVG, Terminal, PDF, and Base64.
|
Package main generates the same QR code in all 5 supported formats: PNG, SVG, Terminal, PDF, and Base64. |
|
internal
|
|
|
renderer
Package renderer provides modular, pure-rendering QR code output formats.
|
Package renderer provides modular, pure-rendering QR code output formats. |
|
storage
Package storage provides file system abstraction for QR code output.
|
Package storage provides file system abstraction for QR code output. |
|
workerpool
Package workerpool provides a generic, bounded-concurrency worker pool.
|
Package workerpool provides a generic, bounded-concurrency worker pool. |