qrcode

package module
v2.2.2 Latest Latest
Warning

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

Go to latest
Published: May 31, 2022 License: MIT Imports: 13 Imported by: 1

README

go-qrcode

Go Report Card go.dev reference Go GitHub release (latest SemVer) GitHub go.mod Go version License

QR code (abbreviated from Quick Response Code) is the trademark for a type of matrix barcode (or two-dimensional barcode) first designed in 1994 for the automotive industry in Japan. A barcode is a machine-readable optical label that contains information about the item to which it is attached. A QR code uses four standardized encoding modes (numeric, alphanumeric, byte/binary, and kanji) to store data efficiently; extensions may also be used
Features
  • Normally generate QR code across version 1 to version 40.
  • Automatically analyze QR version by source text.
  • Specifying cell shape allowably with WithCustomShape, WithCircleShape (default is rectangle)
  • Specifying output file's format with WithBuiltinImageEncoder, WithCustomImageEncoder (default is JPEG)
  • Not only shape of cell, but also color of QR Code background and foreground color.
  • WithLogoImage, WithLogoImageFilePNG, WithLogoImageFileJPEG help you add an icon at the central of QR Code.
  • WithBorderWidth allows to specify any width of 4 sides around the qrcode.
  • WebAssembly support, check out the Example and README for more detail.
  • support Halftone QR Codes, check out the Example.
Install
go get -u github.com/WilliamNHarvey/go-qrcode/v2
Quick Start

link to CODE

package main

import (
	"github.com/WilliamNHarvey/go-qrcode/v2"
	"github.com/WilliamNHarvey/go-qrcode/writer/standard"
)

func main() {
	qrc, err := qrcode.New("https://github.com/WilliamNHarvey/go-qrcode")
	if err != nil {
		fmt.Printf("could not generate QRCode: %v", err)
		return
	}
	
	w, err := standard.New("../assets/repo-qrcode.jpeg")
	if err != nil {
		fmt.Printf("standard.New failed: %v", err)
		return
	}
	
	// save file
	if err = qrc.Save(w); err != nil {
		fmt.Printf("could not save image: %v", err)
	}
}
Options
const (
	// EncModeNone mode ...
	EncModeNone encMode = 1 << iota
	// EncModeNumeric mode ...
	EncModeNumeric
	// EncModeAlphanumeric mode ...
	EncModeAlphanumeric
	// EncModeByte mode ...
	EncModeByte
	// EncModeJP mode ...
	EncModeJP
)

// WithEncodingMode sets the encoding mode.
func WithEncodingMode(mode encMode) EncodeOption {}

const (
	// ErrorCorrectionLow :Level L: 7% error recovery.
	ErrorCorrectionLow ecLevel = iota + 1
	
	// ErrorCorrectionMedium :Level M: 15% error recovery. Good default choice.
	ErrorCorrectionMedium
	
	// ErrorCorrectionQuart :Level Q: 25% error recovery.
	ErrorCorrectionQuart
	
	// ErrorCorrectionHighest :Level H: 30% error recovery.
	ErrorCorrectionHighest
)

// WithErrorCorrectionLevel sets the error correction level.
func WithErrorCorrectionLevel(ecLevel ecLevel) EncodeOption {}

following are some shots:


Built-in Writers

Of course, you can also code your own writer, just implement Writer interface.

Migrating from v1

go-qrcode.v2 is a major upgrade from v1, and it is not backward compatible. v2 redesigned the API, and it is more flexible and powerful. Features are split into different modules (according to functionality).

  • github.com/WilliamNHarvey/go-qrcode/v2 core
  • github.com/WilliamNHarvey/go-qrcode/writer/standard writer/imageFile
  • github.com/WilliamNHarvey/go-qrcode/writer/terminal writer/terminal

Check example/migrating-from-v1 for more details.

Documentation

Overview

Package qrcode ... encoder.go working for data encoding

Index

Constants

View Source
const (
	// a qrbool of EncModeAuto will trigger a detection of the letter set from the input data,
	EncModeAuto = 0
	// EncModeNone mode ...
	EncModeNone encMode = 1 << iota
	// EncModeNumeric mode ...
	EncModeNumeric
	// EncModeAlphanumeric mode ...
	EncModeAlphanumeric
	// EncModeByte mode ...
	EncModeByte
	// EncModeJP mode ...
	EncModeJP
)
View Source
const (
	// IterDirection_ROW for row first
	IterDirection_ROW iterDirection = iota + 1

	// IterDirection_COLUMN for column first
	IterDirection_COLUMN
)
View Source
const (
	// QRType_INIT represents the initial block state of the matrix
	QRType_INIT qrtype = 1 << 1
	// QRType_DATA represents the data block state of the matrix
	QRType_DATA qrtype = 2 << 1
	// QRType_VERSION indicates the version block of matrix
	QRType_VERSION qrtype = 3 << 1
	// QRType_FORMAT indicates the format block of matrix
	QRType_FORMAT qrtype = 4 << 1
	// QRType_FINDER indicates the finder block of matrix
	QRType_FINDER qrtype = 5 << 1
	// QRType_DARK ...
	QRType_DARK     qrtype = 6 << 1
	QRType_SPLITTER qrtype = 7 << 1
	QRType_TIMING   qrtype = 8 << 1
)
View Source
const (
	// ErrorCorrectionLow :Level L: 7% error recovery.
	ErrorCorrectionLow ecLevel = iota + 1

	// ErrorCorrectionMedium :Level M: 15% error recovery. Good default choice.
	ErrorCorrectionMedium

	// ErrorCorrectionQuart :Level Q: 25% error recovery.
	ErrorCorrectionQuart

	// ErrorCorrectionHighest :Level H: 30% error recovery.
	ErrorCorrectionHighest
)

Variables

View Source
var (
	// ErrorOutRangeOfW x out of range of Width
	ErrorOutRangeOfW = errors.New("out of range of width")

	// ErrorOutRangeOfH y out of range of Height
	ErrorOutRangeOfH = errors.New("out of range of height")
)
View Source
var (
	// QRValue_INIT_V0 represents the value 0
	QRValue_INIT_V0 = qrvalue(QRType_INIT | 0)

	// QRValue_DATA_V0 represents the block has been set to false
	QRValue_DATA_V0 = qrvalue(QRType_DATA | 0)
	// QRValue_DATA_V1 represents the block has been set to TRUE
	QRValue_DATA_V1 = qrvalue(QRType_DATA | 1)

	// QRValue_VERSION_V0 represents the block has been set to false
	QRValue_VERSION_V0 = qrvalue(QRType_VERSION | 0)
	// QRValue_VERSION_V1 represents the block has been set to TRUE
	QRValue_VERSION_V1 = qrvalue(QRType_VERSION | 1)

	// QRValue_FORMAT_V0 represents the block has been set to false
	QRValue_FORMAT_V0 = qrvalue(QRType_FORMAT | 0)
	// QRValue_FORMAT_V1 represents the block has been set to TRUE
	QRValue_FORMAT_V1 = qrvalue(QRType_FORMAT | 1)

	// QRValue_FINDER_V0 represents the block has been set to false
	QRValue_FINDER_V0 = qrvalue(QRType_FINDER | 0)
	// QRValue_FINDER_V1 represents the block has been set to TRUE
	QRValue_FINDER_V1 = qrvalue(QRType_FINDER | 1)

	// QRValue_DARK_V0 represents the block has been set to false
	QRValue_DARK_V0 = qrvalue(QRType_DARK | 0)
	// QRValue_DARK_V1 represents the block has been set to TRUE
	QRValue_DARK_V1 = qrvalue(QRType_DARK | 1)

	// QRValue_SPLITTER_V0 represents the block has been set to false
	QRValue_SPLITTER_V0 = qrvalue(QRType_SPLITTER | 0)
	// QRValue_SPLITTER_V1 represents the block has been set to TRUE
	QRValue_SPLITTER_V1 = qrvalue(QRType_SPLITTER | 1)

	// QRValue_TIMING_V0 represents the block has been set to false
	QRValue_TIMING_V0 = qrvalue(QRType_TIMING | 0)
	// QRValue_TIMING_V1 represents the block has been set to TRUE
	QRValue_TIMING_V1 = qrvalue(QRType_TIMING | 1)
)

Functions

func DefaultEncodingOption

func DefaultEncodingOption() *encodingOption

DefaultEncodingOption with EncMode = EncModeAuto, EcLevel = ErrorCorrectionQuart

func SetDebugMode

func SetDebugMode()

SetDebugMode open debug switch, you can also enable debug by runtime environments variables: QRCODE_DEBUG=1 [1, true, TRUE, enabled, ENABLED] which is recommended.

Types

type EncodeOption

type EncodeOption interface {
	// contains filtered or unexported methods
}

func WithEncodingMode

func WithEncodingMode(mode encMode) EncodeOption

WithEncodingMode sets the encoding mode.

func WithErrorCorrectionLevel

func WithErrorCorrectionLevel(ecLevel ecLevel) EncodeOption

WithErrorCorrectionLevel sets the error correction level.

func WithVersion

func WithVersion(version int) EncodeOption

WithVersion sets the version of target QR code.

type Matrix

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

Matrix is a matrix data type width:3 height: 4 for [3][4]int

func (*Matrix) Col

func (m *Matrix) Col(cur int) []qrvalue

Col return a slice of column, cur should be x dimension.

func (*Matrix) Copy

func (m *Matrix) Copy() *Matrix

Copy matrix into a new Matrix

func (*Matrix) Height

func (m *Matrix) Height() int

Height ... height

func (*Matrix) Iterate

func (m *Matrix) Iterate(direction iterDirection, fn func(x, y int, s QRValue))

Iterate the Matrix with loop direction IterDirection_ROW major or IterDirection_COLUMN major. IterDirection_COLUMN is recommended.

func (*Matrix) Row

func (m *Matrix) Row(cur int) []qrvalue

Row return a row of matrix, cur should be y dimension.

func (*Matrix) Width

func (m *Matrix) Width() int

Width ... width

type QRCode

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

QRCode contains fields to generate QRCode matrix, outputImageOptions to Draw image, etc.

func New

func New(text string) (*QRCode, error)

New generate a QRCode struct to create

func NewWith

func NewWith(text string, opts ...EncodeOption) (*QRCode, error)

NewWith generate a QRCode struct with specified `ver`(QR version) and `ecLv`(Error Correction level)

func (*QRCode) Dimension

func (q *QRCode) Dimension() int

func (*QRCode) Save

func (q *QRCode) Save(w Writer) error

type QRType

type QRType = qrtype

type QRValue

type QRValue = qrvalue

func (QRValue) IsSet

func (v QRValue) IsSet() bool

func (QRValue) Type

func (v QRValue) Type() qrtype

type Writer

type Writer interface {
	// Write writes the code image into itself stream, such as io.Writer,
	// terminal output stream, and etc
	Write(mat Matrix) error

	// Close the writer stream if it exists after QRCode.Save() is called.
	Close() error
}

Writer is the interface of a QR code writer, it defines the rule of how to `print` the code image from matrix. There's built-in writer to output into file, terminal.

Jump to

Keyboard shortcuts

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