encoder

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package encoder provides functionality for generating, formatting, and exporting QR codes. It handles data encoding, error correction, and rendering the final QR code into various formats such as images, bit matrices, and terminal strings.

Index

Constants

View Source
const DefaultFileSize = 256

DefaultFileSize defines the standard dimensions used when saving a QR code without a specified size.

Variables

This section is empty.

Functions

func Element

func Element(data byte) gfElement

Element converts a standard byte value into a Galois Field element.

func Encode

func Encode(content string, level RecoveryLevel, size int) ([]byte, error)

Encode generates a PNG byte slice representing the QR code for the given content, recovery level, and size.

func NewGFPolyOperator

func NewGFPolyOperator() polyOperator

NewGFPolyOperator creates and returns a new polyOperator instance.

func WriteColorFile

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

WriteColorFile generates a QR code with custom background and foreground colors and writes it to a file.

func WriteFile

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

WriteFile generates a QR code and writes it to a file with the specified filename and size. If size is 0, it uses the DefaultFileSize.

Types

type Bit

type Bit bool

Bit represents a single bit mapped to a boolean value.

const (
	// White represents an unset bit (false).
	White Bit = false
	// Black represents a set bit (true).
	Black Bit = true
)

func Bits

func Bits(b ...bool) []Bit

Bits converts a slice of booleans into a slice of Bit values.

func (Bit) Bool

func (b Bit) Bool() bool

Bool returns the underlying boolean value of the Bit.

func (Bit) Representation

func (b Bit) Representation() rune

Representation returns the rune representation of the bit ('1' for true, '0' for false).

type Bitset

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

Bitset represents a set of bits backed by a byte array.

func Clone

func Clone(from *Bitset) *Bitset

Clone creates and returns a deep copy of the provided Bitset.

func New

func New(v ...Bit) *Bitset

New creates and returns a new Bitset initialized with the provided bits.

func NewFromBase2String

func NewFromBase2String(b2string string) *Bitset

NewFromBase2String creates a new Bitset from a base-2 string representation (e.g., "1010"). It ignores space characters and panics if an invalid character is encountered.

func (*Bitset) Append

func (b *Bitset) Append(other *Bitset)

Append appends the bits from another Bitset to the current one.

func (*Bitset) AppendBits

func (b *Bitset) AppendBits(bits ...Bit)

AppendBits appends a sequence of Bit values to the Bitset.

func (*Bitset) AppendBools

func (b *Bitset) AppendBools(bits ...bool)

AppendBools appends a sequence of boolean values to the Bitset.

func (*Bitset) AppendByte

func (b *Bitset) AppendByte(value byte, numBits int)

AppendByte appends a specified number of bits from a byte to the Bitset. It panics if numBits is greater than 8.

func (*Bitset) AppendBytes

func (b *Bitset) AppendBytes(data []byte)

AppendBytes appends all the bits from the provided byte slice to the Bitset.

func (*Bitset) AppendNumBools

func (b *Bitset) AppendNumBools(num int, value bool)

AppendNumBools appends a specific boolean value a given number of times to the Bitset.

func (*Bitset) AppendUint32

func (b *Bitset) AppendUint32(value uint32, numBits int)

AppendUint32 appends a specified number of bits from a uint32 value to the Bitset. It panics if numBits is greater than 32.

func (*Bitset) At

func (b *Bitset) At(index int) bool

At returns the boolean value of the bit at the specified index. It panics if the index is out of bounds.

func (*Bitset) Bits

func (b *Bitset) Bits() []Bit

Bits returns all the bits in the Bitset as a slice of Bit values.

func (*Bitset) ByteAt

func (b *Bitset) ByteAt(index int) byte

ByteAt constructs and returns a byte from up to 8 bits starting at the specified index. It panics if the index is out of bounds.

func (*Bitset) Equals

func (b *Bitset) Equals(other *Bitset) bool

Equals returns true if the current Bitset and the other Bitset are equal in length and content.

func (*Bitset) Len

func (b *Bitset) Len() int

Len returns the current number of bits in the Bitset.

func (*Bitset) String

func (b *Bitset) String() string

String returns a string representation of the Bitset, including its length and bit sequence.

func (*Bitset) Substr

func (b *Bitset) Substr(start int, end int) *Bitset

Substr creates and returns a new Bitset containing the bits from the start index up to the end index. It panics if the indices are out of bounds or if start is greater than end.

type QRCode

type QRCode struct {
	Content         string
	Level           RecoveryLevel
	VersionNumber   int
	ForegroundColor color.Color
	BackgroundColor color.Color
	DisableBorder   bool
	// contains filtered or unexported fields
}

QRCode represents a generated QR code with its content, configuration, and structural data.

func NewQRCode

func NewQRCode(content string, level RecoveryLevel) (*QRCode, error)

NewQRCode creates a new QRCode with the given content and error recovery level. It automatically determines the smallest possible QR code version that can fit the content.

func NewWithForcedVersion

func NewWithForcedVersion(content string, version int, level RecoveryLevel) (*QRCode, error)

NewWithForcedVersion creates a new QRCode using a specific version and error recovery level. It returns an error if the content exceeds the capacity of the specified version.

func (*QRCode) Bitmap

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

Bitmap returns a 2D boolean array representing the QR code's modules. True represents a foreground module, and false represents a background module.

func (*QRCode) Image

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

Image returns an image.Image representation of the QR code mapped to the specified pixel size.

func (*QRCode) PNG

func (q *QRCode) PNG(size int) []byte

PNG returns the QR code as a PNG-encoded byte slice mapped to the given size.

func (*QRCode) ToSmallString

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

ToSmallString returns a more compact string representation of the QR code using half-block characters. It is ideal for terminals with limited vertical space.

func (*QRCode) ToString

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

ToString returns a string representation of the QR code using unicode block characters. It is useful for printing the QR code to a standard terminal.

func (*QRCode) WithColors

func (q *QRCode) WithColors(foreground, background color.Color) *QRCode

WithColors sets custom foreground and background colors for the QR code. It returns the modified QRCode instance for chaining.

func (*QRCode) WithNoBorder

func (q *QRCode) WithNoBorder() *QRCode

WithNoBorder disables the quiet zone (border) around the QR code. It returns the modified QRCode instance for chaining.

func (*QRCode) Write

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

Write encodes the QR code as a PNG of the given size and writes it to the provided io.Writer.

func (*QRCode) WriteFile

func (q *QRCode) WriteFile(size int, filename string) error

WriteFile writes the QR code as a PNG to a file with the specified size and filename.

func (*QRCode) WriteFileWithoutSize

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

WriteFileWithoutSize writes the QR code to a file using the DefaultFileSize.

type RecoveryLevel

type RecoveryLevel int
const (
	Low RecoveryLevel = iota
	Medium
	High
	Highest
)

Jump to

Keyboard shortcuts

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