qrlogo

package module
v0.0.0-...-2b7049b Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2022 License: MIT Imports: 10 Imported by: 0

README

A command line tool and library written in Go to generate QR Codes with logos/images embedded. The logos can be padded in different ways to make them stand out.

A generated QRCode with the Github logo centered, directing the user to the repository URL

Contents

Installation

  1. Go needs to be installed then the following command can be used to add qrlogo to your project:
go get -u github.com/alexandernorth/qrlogo
  1. Import qrlogo into your code:
import "github.com/alexandernorth/qrlogo"

Quickstart

Create a file with the following contents:

package main

import (
  "os"
  "path"

  "github.com/alexandernorth/qrlogo"
)

func main() {

	logo, err := qrlogo.OpenImage("logo.png")
	if err != nil {
		panic(err)
	}
	
	qlg := qrlogo.NewQRLogo(logo)
    
	qrLogo, err := qlg.QRCodeForURL("https://github.com/alexandernorth/qrlogo")
    if err != nil {
        panic(err)
    }
	
	outputDir := "qrcodes"
	
    err = os.MkdirAll(outputDir, 0755)
    if err != nil {
        panic(err)
    }
    err = qrlogo.SaveToPNGFile(qrLogo, path.Join(outputDir, "qr.png"))
    if err != nil {
        panic(err)
    }
}

Options

QRLogo can be configured with several options:

BackgroundColor

Sets the background color of the code

qlg := qrlogo.NewQRLogo(
  logo,
  qrlogo.BackgroundColor(colornames.Map["white"]),
)
CodeColour

Sets the colour of the QRCode bars

qlg := qrlogo.NewQRLogo(
  logo,
  qrlogo.CodeColour(colornames.Map["black"]),
)
DisableBorder

Sets whether there is a border around the whole QRCode

qlg := qrlogo.NewQRLogo(
  logo,
  qrlogo.DisableBorder(false),
)
LogoCoverage

Sets the percentage of the QRCode to be covered in range [0,1] with 1 being 100%. The error correction ability of a QRCode is at max 30%, so this value should not be exceeded, but other factors may force this value down

qlg := qrlogo.NewQRLogo(
  logo,
  qrlogo.LogoCoverage(0.2),
)
PaddingType

Sets the type of padding to use when rendering the logo.

Valid options are:

  • dilate
  • none
  • square
  • circle
qlg := qrlogo.NewQRLogo(
  logo,
  qrlogo.PaddingType(qrlogo.LogoPaddingDilate),
)

LogoPaddingType is a string, so it is also possible to specify the type through a string:

qlg := qrlogo.NewQRLogo(
  logo,
  qrlogo.PaddingType(qrlogo.LogoPaddingType("dilate")),
)
PaddingWeight

Sets the amount of padding around the logo

qlg := qrlogo.NewQRLogo(
  logo,
  qrlogo.PaddingWeight(20),
)
QRSize

Sets the size of the output image

qlg := qrlogo.NewQRLogo(
  logo,
  qrlogo.QRSize(2048),
)

Documentation

Index

Constants

View Source
const (
	// LogoPaddingCircle adds a circle border around the logo
	LogoPaddingCircle = "circle"
	// LogoPaddingDilate adds a dilated (traced) border around the logo
	LogoPaddingDilate = "dilate"
	// LogoPaddingNone adds no padding
	LogoPaddingNone = "none"
	// LogoPaddingSquare adds a square border around the logo
	LogoPaddingSquare = "square"
)

Variables

This section is empty.

Functions

func OpenImage

func OpenImage(logoPath string) (image.Image, error)

OpenImage loads an image file from disk, and decodes it into an image.Image

func SaveToPNGFile

func SaveToPNGFile(img image.Image, outFile string) error

SaveToPNGFile saves an image to disk at the given filepath

Types

type LogoPaddingType

type LogoPaddingType string

type Options

type Options func(*QRLogo)

func BackgroundColor

func BackgroundColor(backgroundColor color.Color) Options

BackgroundColor sets the background color of the code

func CodeColour

func CodeColour(codeColor color.Color) Options

CodeColour sets the colour of the QRCode bars

func DisableBorder

func DisableBorder(disableBorder bool) Options

DisableBorder sets whether there is a border around the whole QRCode

func LogoCoverage

func LogoCoverage(logoCoverage float64) Options

LogoCoverage sets the percentage of the QRCode to be covered in range [0,1] with 1 being 100% The error correction ability of a QRCode is at max 30%, so this value should *not* be exceeded, but other factors may force this value down

func PaddingType

func PaddingType(logoPaddingType LogoPaddingType) Options

PaddingType sets the type of padding to use when rendering the logo

func PaddingWeight

func PaddingWeight(paddingWeight int) Options

PaddingWeight sets the amount of padding around the logo

func QRSize

func QRSize(qrSize int) Options

QRSize sets the size of the output image

type QRLogo struct {
	// contains filtered or unexported fields
}
func NewQRLogo(logo image.Image, opts ...Options) *QRLogo

NewQRLogo creates a new QRLogo, taking the logo to be placed in the generated QRCode

func (*QRLogo) QRCodeForURL

func (q *QRLogo) QRCodeForURL(url string) (image.Image, error)

QRCodeForURL generates a new QRCode image.Image for the given URL, containing the logo

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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