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 ¶
- Constants
- func Decode(img image.Image) (string, error)
- func Encode(data string, opts *EncodeOptions) ([]byte, error)
- func EncodeToFile(data string, filename string, opts *EncodeOptions) error
- func Verify(qrImage []byte, expectedData string) error
- type EncodeOptions
- type Recovery
- type Result
- type VerificationError
Examples ¶
Constants ¶
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
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 ¶
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 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.