gqr

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2023 License: MIT Imports: 13 Imported by: 1

README

gqr

gqr is a QR code generation library focused on creating customizable QR codes.

This project is a fork of github.com/yeqown/go-qrcode with updated API (see Changes) So big thanks to the initial project! 🙏

Features

Features marked with * are inherited from go-qrcode

  • Normally generate QR code across version 1 to version 40 *
  • Automatically analyze QR version by source text *
  • Applying image size with WithImageSize
  • Applying padding for QR code with WithQuietZone
  • Full customization of shapes:
    • Customize modules with WithModuleShape
      • Squares
      • Rounded,
      • Connected (no gaps between adjacent modules)
      • Lines
    • Customize finders (eyes) with WithFinderShape
    • "Connected" rounded modules with go gaps between
  • Setting colors *: WithBgColor(Hex), WithFgColor(Hex)
    • Added support for hex colors with alpha channel (8 digits)
    • Gradient with customizable direction via WithGradient(dir, colors...).
      • Overrides foreground color
      • Colors will be automatically placed evenly on the gradient map
  • Advanced logo placing:
    • WithLogo places image at center and rescales it to be 1/5 of qr code
    • WithSpaceAroundLogo add white space at the center so logo is not placed on top of modules
  • Support Halftones

Install

go get -u github.com/quickqr/gqr

Using

Check other examples

package main

import (
  "github.com/quickqr/gqr"
  export "github.com/quickqr/gqr/export/image"
  "github.com/quickqr/gqr/export/image/shapes"
  "image"
  "image/png"
  "log"
  "os"
)

func main() {
  qr, e := gqr.NewWith(
    "https://github.com/quickqr/gqr",
    // These are defaults, can be omitted (as it is in other examples)
    gqr.WithErrorCorrectionLevel(gqr.ErrorCorrectionHighest),
    gqr.WithEncodingMode(gqr.EncModeAuto),
    // Uncomment to force QR code version 23.
    // Note: you might get an error if supplied length is more than capacity of a specified version
    // gqr.WithVersion(23)
  )

  if e != nil {
    log.Fatal(e)
  }

  logoFile, _ := os.Open("./gopher.png")
  logo, _, _ := image.Decode(logoFile)

  // Export QR code to image
  img := export.
          NewExporter(
            export.WithBgColorHex("#ffffff"),

            export.WithLogo(logo),
            export.WithSpaceAroundLogo(),

            export.WithFinderShape(shapes.RoundedFinderShape(0.3)),
            export.WithModuleShape(shapes.RoundedModuleShape(0.5, true)),

            // Apply gap between modules (note: this particular example will not use it because of connected modules)
            // You can see this working as expected in invert.go and  custom-shapes.go
            export.WithModuleGap(0.1),
            // Size of the outputted image in pixels
            export.WithImageSize(1024),

            export.WithLogoScale(0.8),  // default
            // Padding around QR code
            // Note: actual QR code size will be (image size - quiet zone * 2)
            export.WithQuietZone(60),

            // Gradient for foreground with direction from Top Right to Bottom Left
            export.WithGradient(export.GradientDirectionLTR,
              export.ParseFromHex("#336FE1"),
              export.ParseFromHex("#2799C9"),
              // You also can use any color.Color instance
            ),
          ).
    Export(*qr)

  // Save the image
  f, _ := os.OpenFile("../assets/main.png", os.O_CREATE|os.O_WRONLY, 0644)
  if err := png.Encode(f, img); err != nil {
    log.Fatal(err)
  }
}

Showcase

All of these pictures are generated by programs in examples:

main lines default inverted inverted

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 FINDER_SIZE = 7

FINDER_SIZE size of finder in modules

Variables

View Source
var (
	// ErrorOutRangeOfW x out of range of ModSize
	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(QRType_INIT | 0)
	QRValue_INIT_V0 = qrvalue(QRType_INIT)

	// QRValue_DATA_V0 represents the block has been Set to false qrvalue(QRType_DATA | 0)
	QRValue_DATA_V0 = qrvalue(QRType_DATA)
	// 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(QRType_VERSION | 0)
	QRValue_VERSION_V0 = qrvalue(QRType_VERSION)
	// 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(QRType_FORMAT | 0)
	QRValue_FORMAT_V0 = qrvalue(QRType_FORMAT)
	// 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(QRType_FINDER | 0)
	QRValue_FINDER_V0 = qrvalue(QRType_FINDER)
	// 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(QRType_DARK | 0)
	QRValue_DARK_V0 = qrvalue(QRType_DARK)
	// 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(QRType_SPLITTER | 0)
	QRValue_SPLITTER_V0 = qrvalue(QRType_SPLITTER)
	// 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(QRType_TIMING | 0)
	QRValue_TIMING_V0 = qrvalue(QRType_TIMING)
	// 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 ErrorCorrectionLevel) EncodeOption

WithErrorCorrectionLevel sets the error correction level.

func WithVersion

func WithVersion(version int) EncodeOption

WithVersion sets the version of target QR code.

type ErrorCorrectionLevel

type ErrorCorrectionLevel int

ErrorCorrectionLevel error correction level

const (
	// ErrorCorrectionLow :Level L: 7% error recovery.
	ErrorCorrectionLow ErrorCorrectionLevel = iota + 1

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

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

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

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 New

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

New generate a qrcode struct to create

func NewWith

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

NewWith generates Matrix that contains QR code with supplied data

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) Set added in v0.3.0

func (m *Matrix) Set(w, h int, c qrvalue) error

Set [w][h] as true

func (*Matrix) ValueAtClamped added in v0.3.0

func (m *Matrix) ValueAtClamped(w, h int) QRValue

func (*Matrix) Width

func (m *Matrix) Width() int

Width ... width

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

Directories

Path Synopsis
export
Package reedsolomon ...
Package reedsolomon ...
binary
Package binary ...
Package binary ...

Jump to

Keyboard shortcuts

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