package controller import ( "bytes" "encoding/base64" "errors" "fmt" "image" "image/color" "image/png" "github.com/skip2/go-qrcode" ) func GenerateQRCode(data string) (string, error) { imgBytes, err := qrcode.Encode(data, qrcode.Medium, 256) if err != nil { return "", err } base64Img := base64.StdEncoding.EncodeToString(imgBytes) return base64Img, nil } // vvv DEPRECATED vvv const margin = 4 type QRCodeECCLevel int64 const ( LOW QRCodeECCLevel = iota MEDIUM QUARTILE HIGH ) func noDepsGenerateQRCode() (string, error) { version := 1 size := 0 size = 21 + version * 4 if version > 10 { return "", errors.New(fmt.Sprintf("QR version %d not supported", version)) } img := image.NewGray(image.Rect(0, 0, size + margin * 2, size + margin * 2)) // fill white for y := range size + margin * 2 { for x := range size + margin * 2 { img.Set(x, y, color.White) } } // draw alignment squares drawLargeAlignmentSquare(margin, margin, img) drawLargeAlignmentSquare(margin, margin + size - 7, img) drawLargeAlignmentSquare(margin + size - 7, margin, img) drawSmallAlignmentSquare(size - 5, size - 5, img) /* if version > 4 { space := version * 3 - 2 end := size / space for y := range size / space + 1 { for x := range size / space + 1 { if x == 0 && y == 0 { continue } if x == 0 && y == end { continue } if x == end && y == 0 { continue } if x == end && y == end { continue } drawSmallAlignmentSquare( x * space + margin + 4, y * space + margin + 4, img, ) } } } */ // draw timing bits for i := margin + 6; i < size - 4; i++ { if (i % 2 == 0) { img.Set(i, margin + 6, color.Black) img.Set(margin + 6, i, color.Black) } } img.Set(margin + 8, size - 4, color.Black) var imgBuf bytes.Buffer err := png.Encode(&imgBuf, img) if err != nil { return "", err } base64Img := base64.StdEncoding.EncodeToString(imgBuf.Bytes()) return "data:image/png;base64," + base64Img, nil } func drawLargeAlignmentSquare(x int, y int, img *image.Gray) { for yi := range 7 { for xi := range 7 { if (xi == 0 || xi == 6) || (yi == 0 || yi == 6) { img.Set(x + xi, y + yi, color.Black) } else if (xi > 1 && xi < 5) && (yi > 1 && yi < 5) { img.Set(x + xi, y + yi, color.Black) } } } } func drawSmallAlignmentSquare(x int, y int, img *image.Gray) { for yi := range 5 { for xi := range 5 { if (xi == 0 || xi == 4) || (yi == 0 || yi == 4) { img.Set(x + xi, y + yi, color.Black) } } } img.Set(x + 2, y + 2, color.Black) }