qrverify

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2026 License: MIT Imports: 9 Imported by: 0

README

qrverify

Go Reference CI

Verified QR code generation for Go. Every generated QR code is guaranteed to decode back to the original data.

Installation

go get github.com/13rac1/qrverify

Quick Start

png, err := qrverify.Encode("https://example.com", nil)
if err != nil {
    log.Fatal(err)
}
os.WriteFile("qr.png", png, 0644)

Features

  • Verified output - All generated QR codes are decoded and verified before returning
  • Size validation - Validates data fits within QR capacity limits before encoding
  • Simple API - Encode() with nil options for defaults, or custom options for control

Implementation

The encoder and decoder libraries were selected based on performance and accuracy benchmarks from qr-benchmarks.

API

Function Description
Encode(data, opts) Generate QR code (opts=nil for defaults: 256px, Medium recovery)
EncodeToFile(data, filename, opts) Generate and write to file
EncodeDetailed(data, opts) Generate with metadata result
Decode(img) Decode QR code from image
Verify(png, expected) Verify existing QR code

CLI

go install github.com/13rac1/qrverify/cmd/qrverify@latest

qrverify encode "https://example.com" -o qr.png
qrverify verify qr.png "https://example.com"
qrverify demo

License

MIT License - see LICENSE

Documentation

Overview

Package qrverify provides verified QR code generation.

It wraps github.com/boombuler/barcode (encoding) and github.com/makiuchi-d/gozxing (decoding) to guarantee that every generated QR code can be successfully decoded back to the original input data.

Quick Start

Generate a verified QR code with sensible defaults:

png, err := qrverify.Encode("https://example.com", nil)
if err != nil {
    log.Fatal(err)
}
os.WriteFile("qr.png", png, 0644)

Custom Options

Use EncodeOptions for fine-grained control:

opts := &qrverify.EncodeOptions{
    Recovery: qrverify.High,  // 25% error correction
    Size:     512,            // 512x512 pixels
}
png, err := qrverify.Encode("data", opts)

Decoding

Decode QR codes from images:

img, _ := png.Decode(file)
data, err := qrverify.Decode(img)

Verification

All Encode functions verify the generated QR code decodes correctly. Use Verify to check existing QR code images:

err := qrverify.Verify(pngBytes, "expected data")

Index

Examples

Constants

View Source
const (
	MaxBytesLow     = 2953
	MaxBytesMedium  = 2331
	MaxBytesHigh    = 1663
	MaxBytesHighest = 1273
)

Maximum data capacity in bytes for QR Version 40 (largest standard QR code) at each error correction level. Based on binary/byte mode encoding. See Recovery type for error correction percentages.

Variables

This section is empty.

Functions

func Decode added in v0.2.3

func Decode(img image.Image) (string, error)

Decode decodes a QR code from an image and returns the data. Uses TRY_HARDER and PURE_BARCODE hints for maximum accuracy.

Example
package main

import (
	"bytes"
	"fmt"
	"image/png"

	"github.com/13rac1/qrverify"
)

func main() {
	// First create a QR code
	pngBytes, _ := qrverify.Encode("Hello, World!", nil)

	// Decode the PNG to an image
	img, _ := png.Decode(bytes.NewReader(pngBytes))

	// Decode the QR code
	data, err := qrverify.Decode(img)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	fmt.Println(data)
}
Output:
Hello, World!

func Encode

func Encode(data string, opts *EncodeOptions) ([]byte, error)

Encode generates a verified QR code PNG image. Returns error if the generated code cannot be decoded back to data.

If opts is nil or opts.Recovery is zero, uses Medium recovery (15%).

Example
package main

import (
	"fmt"

	"github.com/13rac1/qrverify"
)

func main() {
	opts := &qrverify.EncodeOptions{
		Recovery: qrverify.High,
		Size:     512,
	}
	png, err := qrverify.Encode("Hello, World!", opts)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	fmt.Printf("Generated %d byte QR code\n", len(png))
}
Output:
Generated 1781 byte QR code

func EncodeToFile

func EncodeToFile(data string, filename string, opts *EncodeOptions) error

EncodeToFile generates a verified QR code and writes it to filename.

Example
package main

import (
	"fmt"
	"os"

	"github.com/13rac1/qrverify"
)

func main() {
	tmpfile, _ := os.CreateTemp("", "qr-*.png")
	defer func() { _ = os.Remove(tmpfile.Name()) }()
	_ = tmpfile.Close()

	err := qrverify.EncodeToFile("https://example.com", tmpfile.Name(), nil)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	fmt.Println("File created successfully")
}
Output:
File created successfully

func Verify

func Verify(qrImage []byte, expectedData string) error

Verify checks that qrImage (PNG bytes) decodes to expectedData. Returns nil on success, VerificationError if mismatch, or error if decode fails.

Example
package main

import (
	"fmt"

	"github.com/13rac1/qrverify"
)

func main() {
	// First create a QR code
	png, _ := qrverify.Encode("test data", nil)

	// Then verify it
	err := qrverify.Verify(png, "test data")
	if err != nil {
		fmt.Println("Verification failed:", err)
		return
	}
	fmt.Println("Verification passed")
}
Output:
Verification passed

Types

type EncodeOptions

type EncodeOptions struct {
	// Recovery specifies error correction level.
	// Zero value uses Medium.
	Recovery Recovery

	// Size is the image dimension in pixels.
	// Zero value uses 256.
	Size int
}

EncodeOptions configures QR code generation. Zero values provide sensible defaults.

type Recovery

type Recovery int

Recovery specifies QR code error correction level.

const (
	Low     Recovery = iota // 7% error correction
	Medium                  // 15% error correction (default)
	High                    // 25% error correction
	Highest                 // 30% error correction
)

func (Recovery) String

func (r Recovery) String() string

String returns the recovery level name.

type Result

type Result struct {
	Image    []byte   // PNG image bytes
	Data     string   // Verified input data
	Recovery Recovery // Final recovery level used
	Size     int      // Image dimensions in pixels
}

Result contains a verified QR code with metadata.

func EncodeDetailed

func EncodeDetailed(data string, opts *EncodeOptions) (*Result, error)

EncodeDetailed returns the verified QR code with metadata.

Example
package main

import (
	"fmt"

	"github.com/13rac1/qrverify"
)

func main() {
	result, err := qrverify.EncodeDetailed("https://example.com", nil)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	fmt.Printf("Recovery: %v\n", result.Recovery)
}
Output:
Recovery: Medium

type VerificationError

type VerificationError struct {
	Original string // What was encoded
	Decoded  string // What was decoded
}

VerificationError indicates decoded data does not match input.

func (*VerificationError) Detail

func (e *VerificationError) Detail() string

Detail returns an error message with full data content for debugging.

func (*VerificationError) Error

func (e *VerificationError) Error() string

Error returns a safe error message without exposing data content.

Directories

Path Synopsis
cmd
qrverify command

Jump to

Keyboard shortcuts

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