goqrsvg

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2022 License: MIT Imports: 4 Imported by: 2

README

goqrsvg

goqrsvg is an API that makes QR Code to SVG conversions.

A sample server can be found in ./cmd/goqr-svr.

A sample main program (command line interface) can be fond in ./cmd/goqr-cli.

To use:

import "github.com/pschlump/goqrsvg"

Uses:

"github.com/ajstarks/svgo"
"github.com/boombuler/barcode/qr"

Example Usage:

package main

import (
	"fmt"
	"log"
	"net/http"

	svg "github.com/ajstarks/svgo"
	"github.com/boombuler/barcode/qr"
	"github.com/pschlump/goqrsvg"
)

func main() {
	http.Handle("/", http.HandlerFunc(genqr))
	err := http.ListenAndServe(":2003", nil)
	if err != nil {
		log.Fatal("ListenAndServe:", err)
	}
}

func genqr(www http.ResponseWriter, req *http.Request) {

	// Pull in a paramter - 'url' if get or 'url' from post
	var uu string
	if req.Method == "GET" {
		keys, ok := req.URL.Query()["url"]

		if !ok || len(keys[0]) < 1 {
			log.Println("Url Param 'url' is missing")
			www.WriteHeader(http.StatusBadRequest) // 400
			fmt.Fprintf(www, "Missing 'url' paramter\n")
			return
		}

		// Query()["key"] will return an array of items, we only want first item.
		uu = keys[0]
	} else if req.Method == "POST" {
		req.ParseForm()          // Parses the request body
		uu = req.Form.Get("url") // x will be "" if parameter is not set
		if uu == "" {
			log.Println("Url Param 'url' is missing")
			www.WriteHeader(http.StatusBadRequest) // 400
			fmt.Fprintf(www, "Missing 'url' paramter\n")
			return
		}
	} else {
		www.WriteHeader(http.StatusMethodNotAllowed) // 405
		fmt.Fprintf(www, "Invalid Method")
		return
	}

	s := svg.New(www)
	www.Header().Set("Content-Type", "image/svg+xml")

	// Create the QR Code in SVG
	// qrCode, _ := qr.Encode("Hello World", qr.M, qr.Auto)
	qrCode, _ := qr.Encode(uu, qr.M, qr.Auto)

	// Write QR code to SVG
	qs := goqrsvg.NewQrSVG(qrCode, 5)
	qs.StartQrSVG(s)
	qs.WriteQrSVG(s)

	s.End()
}

Documentation:

See GoDoc

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type QrSVG

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

QrSVG holds the data related to the size, location, and block size of the QR Code. Holds unexported fields.

func NewQrSVG

func NewQrSVG(qr barcode.Barcode, blockSize int) QrSVG

NewQrSVG contructs a QrSVG struct. It takes a QR Code in the form of barcode.Barcode and sets the "pixel" or block size of QR Code in the SVG file.

Example
s := svg.New(os.Stdout)

// Create the barcode
qrCode, _ := qr.Encode("Hello World", qr.M, qr.Auto)

// Write QR code to SVG
qs := NewQrSVG(qrCode, 5)
qs.StartQrSVG(s)
qs.WriteQrSVG(s)

s.End()
Output:

func (*QrSVG) SetStartPoint

func (qs *QrSVG) SetStartPoint(x, y int)

SetStartPoint sets the top left start point of QR Code. This takes an X and Y value and then adds four white "blocks" to create the "quiet zone" around the QR Code.

func (*QrSVG) StartQrSVG

func (qs *QrSVG) StartQrSVG(s *svg.SVG)

StartQrSVG creates a start for writing an SVG file that only contains a barcode. This is similar to the svg.Start() method. This fucntion should only be used if you only want to write a QR code to the SVG. Otherwise use the regular svg.Start() method to start your SVG file.

func (*QrSVG) WriteQrSVG

func (qs *QrSVG) WriteQrSVG(s *svg.SVG) error

WriteQrSVG writes the QR Code to SVG.

func (*QrSVG) WriteQrSVGInverse added in v1.0.1

func (qs *QrSVG) WriteQrSVGInverse(s *svg.SVG) error

WriteQrSVG writes the QR Code to SVG.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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