-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdecode.go
55 lines (45 loc) · 1.02 KB
/
decode.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
/*
* Copyright (c) 2018 LI Zhennan
*
* Use of this work is governed by an MIT License.
* You may find a license copy in project root.
*/
package qrcode
import (
"errors"
"fmt"
"image"
"github.com/PeterCxy/gozbar"
)
// DecodeQRCode decodes QR Code content from image
//
// content contains multiple string if there are more than one QR Code
// got decoded.
// content and err are both nil when no QR Code found.
func DecodeQRCode(img image.Image) (content []string, err error) {
defer func() {
if fatal := recover(); fatal != nil {
if err == nil {
err = fmt.Errorf("fatal error: %v", fatal)
} else {
err = fmt.Errorf("fatal error : %v: %v", fatal, err)
}
}
}()
input := zbar.FromImage(img)
s := zbar.NewScanner()
s.SetConfig(zbar.QRCODE, zbar.CFG_ENABLE, 1)
result := s.Scan(input)
if result < 0 {
err = errors.New("error occurred when scanning")
return
}
if result == 0 {
// no result
return
}
input.First().Each(func(item string) {
content = append(content, item)
})
return
}