One minute
Go Qrcode
Step one:
go get github.com/Pallinder/go-randomdata
go get github.com/boombuler/barcode
go get github.com/boombuler/barcode/qr
go get github.com/gorilla/mux
Step two
go run main.go
//go-qr.go
package main
import (
"image/png"
"os"
"log"
"net/http"
"net"
"fmt"
"time"
"io/ioutil"
"github.com/Pallinder/go-randomdata"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"
"github.com/gorilla/mux"
)
//var code *qrCode
func main() {
r:= mux.NewRouter()
srv := &http.Server {
Addr: "0.0.0.0:8000",
WriteTimeout: time.Second * 10,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: r,
}
r.HandleFunc("/qr", record).Methods("GET")
log.Fatal(srv.ListenAndServe())
}
func record(w http.ResponseWriter, r *http.Request) {
start_time := time.Now()
host, _, _ := net.SplitHostPort(r.RemoteAddr)
code, _ := qr.Encode(fmt.Sprintf("Stream:<%v> \033[36m%v <%v>\033[0m", randomdata.Paragraph(), host, start_time), qr.M, qr.Auto)
code, _ = barcode.Scale(code, 200, 200)
bin, _ := os.Create("tmp/bin.png")
defer bin.Close()
png.Encode(bin, code)
r_bin, _ := ioutil.ReadFile("tmp/bin.png")
w.Write([]byte(r_bin))
stop_time := time.Since(start_time)
os.Remove("tmp/bin.png")
fmt.Printf("\t\033[90mDONE %v \033[35mTook: %v\033[0m %v\n", time.Now(), stop_time, host)
}
127 Words
2019-11-13 03:21 +0000
Read other posts