-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.go
More file actions
49 lines (44 loc) · 926 Bytes
/
code.go
File metadata and controls
49 lines (44 loc) · 926 Bytes
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
package barcode
import (
"bytes"
"image/png"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/code128"
"github.com/boombuler/barcode/qr"
)
func GenerateCode(t string, input string) ([]byte, error) {
switch t {
case "qr":
return QRCode(input)
case "code128":
return Barcode128(input)
default:
return Barcode128(input)
}
}
func QRCode(input string) ([]byte, error) {
qrCode, err := qr.Encode(input, qr.M, qr.Auto)
if err != nil {
return nil, err
}
qrCode, err = barcode.Scale(qrCode, 130, 130)
if err != nil {
return nil, err
}
b := &bytes.Buffer{}
png.Encode(b, qrCode)
return b.Bytes(), nil
}
func Barcode128(input string) ([]byte, error) {
barcode128, err := code128.Encode(input)
if err != nil {
return nil, err
}
scaled, err := barcode.Scale(barcode128, 300, 60)
if err != nil {
return nil, err
}
b := &bytes.Buffer{}
png.Encode(b, scaled)
return b.Bytes(), nil
}