qrcode

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2021 License: MIT Imports: 13 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go QR Code

Build

QR Code encoder (Go).

Install

go get github.com/RashadAnsari/go-qrcode

Usage

import qrcode "github.com/RashadAnsari/go-qrcode"

Example

package main

import (
	"fmt"
	"image/color"
	"log"
	"os"

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

func main() {
	qr, err := qrcode.New("https://rashadansari.github.io", qrcode.High)
	if err != nil {
		log.Fatal(err.Error())
	}

	opacity := 100
	a := (float64(opacity) / float64(100)) * float64(255)
	qr.ForegroundColor = color.RGBA{R: 255, G: 0, B: 0, A: uint8(a)}

	writeToFile("qr.png", qr.PNG)
	writeToFile("qr.jpeg", qr.JPEG)
	writeToFile("qr.svg", qr.SVG)
	writeToFile("qr.pdf", qr.PDF)

	qr.Base64 = true

	stdoutBase64(qr.PNG)
	fmt.Println("----------")
	stdoutBase64(qr.JPEG)
	fmt.Println("----------")
	stdoutBase64(qr.PDF)
	fmt.Println("----------")
	stdoutBase64(qr.SVG)
}

func writeToFile(fileName string, FormatFunc func(_ int) ([]byte, error)) {
	size := 500
	fileMode := os.FileMode(0644)

	bytes, err := FormatFunc(size)
	if err != nil {
		log.Fatal(err.Error())
	}

	if err := os.WriteFile(fileName, bytes, fileMode); err != nil {
		log.Fatal(err.Error())
	}
}

func stdoutBase64(FormatFunc func(_ int) ([]byte, error)) {
	size := 500

	bytes, err := FormatFunc(size)
	if err != nil {
		log.Fatal(err.Error())
	}

	fmt.Println(string(bytes))
}

Maximum Capacity

The maximum capacity of a QR Code varies according to the content encoded and the error recovery level. The maximum capacity is 2,953 bytes, 4,296 alphanumeric characters, 7,089 numeric digits, or a combination.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type QRCode

type QRCode struct {

	// User settable drawing options.
	ForegroundColor color.Color
	BackgroundColor color.Color

	// Qr Code margin.
	Margin int

	// Base 64 output.
	Base64 bool
	// contains filtered or unexported fields
}

func New

func New(content string, level RecoveryLevel) (*QRCode, error)

func (*QRCode) JPEG

func (q *QRCode) JPEG(size int) ([]byte, error)

func (*QRCode) PDF

func (q *QRCode) PDF(size int) ([]byte, error)

func (*QRCode) PNG

func (q *QRCode) PNG(size int) ([]byte, error)

func (*QRCode) SVG

func (q *QRCode) SVG(size int) ([]byte, error)

type RecoveryLevel

type RecoveryLevel int
const (
	// Level L: 7% error recovery.
	Low RecoveryLevel = iota
	// Level M: 15% error recovery. Good default choice.
	Medium
	// Level Q: 25% error recovery.
	High
	// Level H: 30% error recovery.
	Highest
)

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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