qrcode

package module
v0.0.0-...-85a591d Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2021 License: MIT Imports: 16 Imported by: 0

README

go-qrcode

Go Report Card go.dev reference

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.

Install
go get -u github.com/yeqown/go-qrcode
Usage

link to CODE

package main

import (
	"fmt"

	qrcode "github.com/yeqown/go-qrcode"
)

func main() {
	qrc, err := qrcode.New("https://github.com/yeqown/go-qrcode")
	if err != nil {
		fmt.Printf("could not generate QRCode: %v", err)
	}

	// save file
	if err := qrc.Save("../testdata/repo-qrcode.jpeg"); err != nil {
		fmt.Printf("could not save image: %v", err)
	}
}
Options

now go-qrcode provides some options to customize output QRCode.

// WithBgColor background color
func WithBgColor(c color.Color) ImageOption {}

// WithBgColorRGBHex background color
func WithBgColorRGBHex(hex string) ImageOption {}

// WithFgColor QR color
func WithFgColor(c color.Color) ImageOption {}

// WithFgColorRGBHex Hex string to set QR Color
func WithFgColorRGBHex(hex string) ImageOption {}

// WithLogoImage .
func WithLogoImage(img image.Image) ImageOption {}

// WithLogoImageFilePNG load image from file, PNG is required
func WithLogoImageFilePNG(f string) ImageOption {}

// WithLogoImageFileJPEG load image from file, JPEG is required
func WithLogoImageFileJPEG(f string) ImageOption {}

// WithQRWidth specify width of each qr block
func WithQRWidth(width uint8) ImageOption {}

// WithCircleShape use circle shape as rectangle(default)
func WithCircleShape() ImageOption {}

// WithCustomShape use custom shape as rectangle(default)
func WithCustomShape(shape IShape) ImageOption {}

// WithBuiltinImageEncoder option includes: JPEG_FORMAT as default, PNG_FORMAT.
// This works like WithBuiltinImageEncoder, the different between them is
// formatTyp is enumerated in (JPEG_FORMAT, PNG_FORMAT)
func WithBuiltinImageEncoder(format formatTyp) ImageOption

// WithCustomImageEncoder to use custom image encoder to encode image.Image into
// io.Writer
func WithCustomImageEncoder(encoder ImageEncoder) ImageOption

// WithBorderWidth specify the both 4 sides' border width. Notice that
// WithBorderWidth(a) means all border width use this variable `a`,
// WithBorderWidth(a, b) mean top/bottom equal to `a`, left/right equal to `b`.
// WithBorderWidth(a, b, c, d) mean top, right, bottom, left.
func WithBorderWidth(widths ...int) ImageOption

use options in New and NewWithConfig.

NOTICE: NewWithSpecV is deprecated

import (
	qrcode "github.com/yeqown/go-qrcode"
)

// generating QR Code with source text and output image options.
qrc, _ := qrcode.New("text", WithQRWidth(x)) // x is uint8 (0 - 255)

// OR generating QR Code with specified ErrorCorrection Level and Encode Mode,
// output image options are also available.
qrc, _ := qrcode.NewWithConfig("text", config, WithQRWidth(x))

qrc.Save("path/to/qrcode.png")

following are some shots:

Documention

Jump to go.dev/github/yeqown/go-qrcode

Documentation

Overview

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

Index

Constants

View Source
const (
	// a value 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 (
	// JPEG_FORMAT as default output file format.
	JPEG_FORMAT formatTyp = iota
	// PNG_FORMAT .
	PNG_FORMAT
)
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

This section is empty.

Functions

func SetDebugMode

func SetDebugMode()

SetDebugMode open debug mode

Types

type Config

type Config = outputEncodingOption

Config alias of outputEncodingOption.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig with EncMode = EncModeAuto, EcLevel = ErrorCorrectionQuart

type DrawContext

type DrawContext struct {
	*gg.Context
	// contains filtered or unexported fields
}

DrawContext is a rectangle area

func (*DrawContext) Color

func (dc *DrawContext) Color() color.Color

Color returns the color which should be fill into the shape. Note that if you're not using this color but your coded color.Color, some ImageOption functions those set foreground color would take no effect.

func (*DrawContext) Edge

func (dc *DrawContext) Edge() (width, height int)

Edge returns width and height of each shape could take at most.

func (*DrawContext) UpperLeft

func (dc *DrawContext) UpperLeft() image.Point

UpperLeft returns the point which indicates the upper left position.

type IShape

type IShape interface {
	// Draw to fill the IShape of qrcode.
	Draw(ctx *DrawContext)

	// DrawFinder to fill the finder pattern of QRCode, what's finder? google it for more information.
	DrawFinder(ctx *DrawContext)
}

type ImageEncoder

type ImageEncoder interface {
	// Encode specify which format to encode image into w io.Writer.
	Encode(w io.Writer, img image.Image) error
}

ImageEncoder is an interface which describes the rule how to encode image.Image into io.Writer

type ImageOption

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

func WithBgColor

func WithBgColor(c color.Color) ImageOption

WithBgColor background color

func WithBgColorRGBHex

func WithBgColorRGBHex(hex string) ImageOption

WithBgColorRGBHex background color

func WithBorderWidth

func WithBorderWidth(widths ...int) ImageOption

WithBorderWidth specify the both 4 sides' border width. Notice that WithBorderWidth(a) means all border width use this variable `a`, WithBorderWidth(a, b) mean top/bottom equal to `a`, left/right equal to `b`. WithBorderWidth(a, b, c, d) mean top, right, bottom, left.

func WithBuiltinImageEncoder

func WithBuiltinImageEncoder(format formatTyp) ImageOption

WithBuiltinImageEncoder option includes: JPEG_FORMAT as default, PNG_FORMAT. This works like WithBuiltinImageEncoder, the different between them is formatTyp is enumerated in (JPEG_FORMAT, PNG_FORMAT)

func WithCircleShape

func WithCircleShape() ImageOption

WithCircleShape use circle shape as rectangle(default)

func WithCustomImageEncoder

func WithCustomImageEncoder(encoder ImageEncoder) ImageOption

WithCustomImageEncoder to use custom image encoder to encode image.Image into io.Writer

func WithCustomShape

func WithCustomShape(shape IShape) ImageOption

WithCustomShape use custom shape as rectangle(default)

func WithFgColor

func WithFgColor(c color.Color) ImageOption

WithFgColor QR color

func WithFgColorRGBHex

func WithFgColorRGBHex(hex string) ImageOption

WithFgColorRGBHex Hex string to set QR Color

func WithLogoImage

func WithLogoImage(img image.Image) ImageOption

WithLogoImage image should only has 1/5 width of QRCode at most

func WithLogoImageFileJPEG

func WithLogoImageFileJPEG(f string) ImageOption

WithLogoImageFileJPEG load image from file, jpeg is required. image should only has 1/5 width of QRCode at most

func WithLogoImageFilePNG

func WithLogoImageFilePNG(f string) ImageOption

WithLogoImageFilePNG load image from file, PNG is required. image should only has 1/5 width of QRCode at most

func WithQRWidth

func WithQRWidth(width uint8) ImageOption

WithQRWidth specify width of each qr block

type QRCode

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

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

func New

func New(text string, opts ...ImageOption) (*QRCode, error)

New generate a QRCode struct to create

func NewWithConfig

func NewWithConfig(text string, encOpts *Config, opts ...ImageOption) (*QRCode, error)

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

func NewWithSpecV

func NewWithSpecV(text string, ver int, ecLv ecLevel, opts ...ImageOption) (*QRCode, error)

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

func (*QRCode) Save

func (q *QRCode) Save(saveToPath string) (err error)

Save QRCode image into saveToPath DONE(@yeqown): use SaveTo rather than drawAndSaveToFile

func (*QRCode) SaveTo

func (q *QRCode) SaveTo(w io.Writer) error

SaveTo QRCode image into `w`(io.Writer)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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