-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoqr.go
44 lines (36 loc) · 855 Bytes
/
goqr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package goqr
import (
"image"
"image/png"
"os"
"github.com/skip2/go-qrcode"
)
// GenerateAndSave generates a QR code for the given URL and saves it as a PNG file.
func GenerateAndSave(url string, outputPath string, savePng bool) (string, error) {
// Generate QR code
qrCode, err := qrcode.New(url, qrcode.High)
if err != nil {
return "Found error in generating QR code", err
}
if savePng {
// Save QR code as PNG file
err = savePNG(outputPath, qrCode.Image(256))
if err != nil {
return "Found error in saving PNG", err
}
}
return qrCode.ToSmallString(false), err
}
// savePNG saves an image as a PNG file
func savePNG(filename string, img image.Image) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
err = png.Encode(file, img)
if err != nil {
return err
}
return nil
}