qrcode

package module
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: 9 Imported by: 0

README

QrCode Encoder

  • provides primitives for generating and manipulating QR codes, including support for embedding center icons and exporting to multiple formats.

Usage

package main

import (
	"os"

	"github.com/atendi9/qrcode"
)

func main() {
	content := "https://github.com/atendi9/capivara"
	qr, err := qrcode.Encode(content)
	if err != nil {
		panic(err)
	}
	f, err := os.Create("qrcode.png")
	if err != nil {
		panic(err)
	}
	defer f.Close()
	logo, err := os.Open("logo.png")
	if err != nil {
		panic(err)
	}
	defer logo.Close()
	qr.SetIcon(logo)
	mime := qrcode.PNG
	if err := qr.Scale(500, 500).Image(f, mime); err != nil {
		panic(err)
	}
}

Documentation

Overview

Package qrcode provides primitives for generating and manipulating QR codes, including support for embedding center icons and exporting to multiple formats.

Package qrcode provides primitives for generating and manipulating QR codes, including support for embedding center icons and exporting to multiple formats.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidMime = errors.New("invalid mime type")

ErrInvalidMime is returned when an unsupported Mime type is provided.

Functions

This section is empty.

Types

type CarbonFiberBox added in v1.2.0

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

CarbonFiberBox represents a box that generates a carbon fiber texture pattern.

func (*CarbonFiberBox) Image added in v1.2.0

func (c *CarbonFiberBox) Image() image.Image

Image generates and returns a new image.Image containing a simulated carbon fiber texture. The pattern is created using alternating shades of dark gray blocks based on the configured size of the CarbonFiberBox.

type IconBox added in v1.2.0

type IconBox interface {
	// Image returns the image.Image to be used as the icon's background.
	Image() image.Image
}

IconBox defines the interface for the background container of the icon. Any type implementing this interface can be used to provide the background image.

func NewCarbonFiberBox added in v1.2.0

func NewCarbonFiberBox(size int) IconBox

NewCarbonFiberBox creates and returns a new CarbonFiberBox with the specified size. It returns an IconBox interface implementation.

type Metadata

type Metadata struct {
	Dimensions int
}

Metadata holds structural information about the generated QR code.

type Mime

type Mime string

Mime defines the supported image output formats.

const (
	// PNG represents the Portable Network Graphics format.
	PNG Mime = "image/png"
	// JPG represents the Joint Photographic Experts Group format.
	JPG Mime = "image/jpeg"
)

type Options added in v1.2.0

type Options struct {
	Color color.Color
	Size  int
}

type QRCode

type QRCode interface {
	image.Image
	// Content returns the raw string data encoded in the QR code.
	Content() string
	// Metadata returns technical details about the QR code structure.
	Metadata() Metadata
	// SetIcon overlays an image (logo) in the center of the QR code.
	// It handles the icon scaling and optional background box rendering.
	SetIcon(icon io.Reader, box ...IconBox)
	// Image encodes the QR code into a specific Mime format and writes it to w.
	Image(w io.Writer, m Mime) error
	// Scale resizes the current QR Code to a new width and height.
	Scale(width, height int) QRCode
}

QRCode represents a QR code image with its associated content and metadata. It implements the image.Image interface.

func Encode

func Encode(content string, options ...Options) (QRCode, error)

Encode generates a high-recovery (High) QR Code from the given content. The options parameter is variadic and optional, allowing customization of the QR code's size and color palette.

If no options are provided, or if the specified size is less than 256, it defaults to a size of 1024x1024 pixels. If no custom color is specified within the options, [color.DefaultPalette] is used for the foreground and background.

It returns a QRCode interface containing the generated RGBA image and the original content, or an error if the generation fails.

Directories

Path Synopsis
Package color provides tools for defining and generating high-contrast color palettes suitable for QR codes and other visual elements.
Package color provides tools for defining and generating high-contrast color palettes suitable for QR codes and other visual elements.
Package encoder provides functionality for generating, formatting, and exporting QR codes.
Package encoder provides functionality for generating, formatting, and exporting QR codes.

Jump to

Keyboard shortcuts

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