Skip to content

Instantly share code, notes, and snippets.

@BolajiOlajide
Created December 14, 2024 17:20
Show Gist options
  • Save BolajiOlajide/2befbaa54f97b7500a51a2e076d7721e to your computer and use it in GitHub Desktop.
Save BolajiOlajide/2befbaa54f97b7500a51a2e076d7721e to your computer and use it in GitHub Desktop.
qrcode-gen.go
package main
import (
"bytes"
"fmt"
"image/color"
"github.com/BolajiOlajide/testrun"
"github.com/yeqown/go-qrcode/v2"
"github.com/yeqown/go-qrcode/writer/standard"
)
// WriteCloserBuffer wraps a bytes.Buffer to implement io.WriteCloser
type WriteCloserBuffer struct {
bytes.Buffer
}
// Close is a no-op to satisfy the io.WriteCloser interface
func (wcb *WriteCloserBuffer) Close() error {
return nil
}
func main() {
qrc, err := qrcode.NewWith("https://github.com/yeqown/go-qrcode",
qrcode.WithEncodingMode(qrcode.EncModeByte),
qrcode.WithErrorCorrectionLevel(qrcode.ErrorCorrectionQuart))
if err != nil {
fmt.Printf("could not generate QRCode: %v", err)
return
}
img, err := testrun.GetLogoImage()
if err != nil {
fmt.Printf("could not get logo image: %v", err)
return
}
var b WriteCloserBuffer
w := standard.NewWithWriter(&b,
standard.WithBgColor(color.RGBA{R: 255, G: 87, B: 3, A: 255}),
standard.WithBgTransparent(),
standard.WithFgColor(color.White),
standard.WithLogoImage(img))
// Save the QR code to the buffer
if err = qrc.Save(w); err != nil {
fmt.Printf("could not save image: %v", err)
return
}
// Convert the buffer to a byte slice
qrCodeBytes := b.Bytes()
// Use qrCodeBytes as needed
fmt.Printf("QR Code as byte slice: %v\n", len(qrCodeBytes), qrCodeBytes)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment