forked from yuichiroaoki/simple-clock-sys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqr.go
74 lines (58 loc) · 1.35 KB
/
qr.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"bytes"
"fmt"
"image/png"
"os"
"os/exec"
"strings"
"github.com/skip2/go-qrcode"
)
// Create a QR code from a hash and save it to a file
func generateQrCodeFromHash(hash, path string) (*qrcode.QRCode, error) {
// Create the QR code
qrCode, err := qrcode.New(hash, qrcode.Medium)
if err != nil {
fmt.Println("Error creating QR code:", err)
return nil, err
}
// Save the QR code to a file
file, err := os.Create(path)
if err != nil {
fmt.Println("Error creating file:", err)
return nil, err
}
defer file.Close()
err = png.Encode(file, qrCode.Image(256))
if err != nil {
fmt.Println("Error encoding QR code as PNG:", err)
return nil, err
}
return qrCode, nil
}
// Get the hash from a QR code
func getHashFromQrCode(q qrcode.QRCode) (string, error) {
hash, err := zbarimgDecode(q)
if err != nil {
fmt.Println("Error decoding QR code:", err)
return "", err
}
return hash, nil
}
func zbarimgDecode(q qrcode.QRCode) (string, error) {
var png []byte
png, err := q.PNG(256)
if err != nil {
return "", err
}
cmd := exec.Command("zbarimg", "--quiet", "-Sdisable",
"-Sqrcode.enable", "-")
var out bytes.Buffer
cmd.Stdin = bytes.NewBuffer(png)
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
return "", err
}
return strings.TrimSuffix(strings.TrimPrefix(out.String(), "QR-Code:"), "\n"), nil
}