-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
48 lines (39 loc) · 855 Bytes
/
main.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
package main
import (
"errors"
"fmt"
"image"
_ "image/png"
"log"
"os"
"github.com/liyue201/goqr"
)
func scanQRCode(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", errors.New(fmt.Sprintf("Could not open file: %v\n", err))
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
log.Fatal(err)
}
}(file)
img, _, err := image.Decode(file)
if err != nil {
return "", errors.New(fmt.Sprintf("Could not decode image: %v\n", err))
}
qrCodes, err := goqr.Recognize(img)
if err != nil {
return "", errors.New(fmt.Sprintf("Recognize failed: %v\n", err))
}
return fmt.Sprintf("QR Code Content: %s", qrCodes[0].Payload), nil
}
func main() {
if len(os.Args) < 2 {
log.Fatal("No File given.")
}
file := os.Args[1]
qrContent, _ := scanQRCode(file)
fmt.Println(qrContent)
}