qrcode

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2023 License: MIT Imports: 7 Imported by: 0

README

qrcode

build-img pkg-img reportcard-img coverage-img version-img

QR code for Go.

Features

  • Fast.
  • Simple API.
  • Dependency-free.
  • Clean and tested code.
  • Based on Russ Cox qr.

See GUIDE.md for more details.

Install

Go version 1.17+

go get github.com/cristalhq/qrcode

Example

url := "otpauth://totp/Example:alice@bob.com?secret=JBSWY3DPEHPK3PXP&issuer=Example"

code, err := qrcode.Encode(url, qrcode.L)
checkErr(err)

f, err := os.Create("qr.jpg")
checkErr(err)
defer f.Close()

err = jpeg.Encode(f, code.Image(), nil)
checkErr(err)

Also see examples: examples_test.go.

Documentation

See these docs.

License

MIT License.

Documentation

Overview

Example
package main

import (
	"image/png"
	"os"

	"github.com/cristalhq/qrcode"
)

func main() {
	url := "otpauth://totp/Example:alice@bob.com?secret=JBSWY3DPEHPK3PXP&issuer=Example"

	code, err := qrcode.Encode(url, qrcode.H)
	checkErr(err)

	f, err := os.Create("qr.png")
	checkErr(err)
	defer f.Close()

	err = png.Encode(f, code.Image())
	checkErr(err)

}

func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Code

type Code struct {
	Bitmap []byte // 1 is black, 0 is white
	Size   int    // number of pixels on a side
	Stride int    // number of bytes per row
	Scale  int    // number of image pixels per QR pixel
}

A Code is a square pixel grid. It implements image.Image and direct PNG encoding.

func Encode

func Encode(text string, level Level) (*Code, error)

Encode returns an encoding of text at the given error correction level.

func EncodeInto

func EncodeInto(bitmap []byte, text string, level Level) (*Code, error)

func (*Code) Image

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

Image returns an Image displaying the code.

func (*Code) IsBlack

func (c *Code) IsBlack(x, y int) bool

IsBlack returns true if the pixel at (x,y) is black.

func (*Code) PNG

func (c *Code) PNG() []byte

PNG returns a PNG image displaying the code.

PNG uses a custom encoder tailored to QR codes. Its compressed size is about 2x away from optimal, but it runs about 20x faster than calling png.Encode on c.Image().

type Level

type Level int

A Level denotes a QR error correction level. From least to most tolerant of errors, they are L, M, Q, H.

const (
	L Level = 0 // 20% redundant
	M Level = 1 // 38% redundant
	Q Level = 2 // 55% redundant
	H Level = 3 // 65% redundant
)

Directories

Path Synopsis
internal
coding
Package coding implements low-level QR coding details.
Package coding implements low-level QR coding details.

Jump to

Keyboard shortcuts

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