-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
155 lines (142 loc) · 3.73 KB
/
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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"crypto/md5"
"encoding/base64"
"encoding/hex"
"flag"
"fmt"
"image"
"image/gif"
"image/png"
"math"
"os"
"path"
"strings"
"sync"
"sync/atomic"
"github.com/sunshineplan/imgconv"
"github.com/yeqown/go-qrcode/v2"
"github.com/yeqown/go-qrcode/writer/standard"
)
const (
DefaultChunkSize = 512
DefaultDelay = 10
StageDir = "stage"
)
func main() {
if len(os.Args) < 3 {
fmt.Println("Usage: qr-stream-gen <input_file> <output_file_in_gif>")
fmt.Println("\tOptions: ")
fmt.Println("\t\t--chunk_size <INT default 512>")
fmt.Println("\t\t--delay <INT default 10>")
os.Exit(1)
}
sourceFileName := os.Args[1]
outputFlieName := os.Args[2]
var chunkSize, delay int
flag.IntVar(&chunkSize, "chunk_size", DefaultChunkSize, "QR stream chunk size")
flag.IntVar(&delay, "delay", DefaultDelay, "Frame delay")
flag.Parse()
fmt.Printf("\rGenerating QR stream [ 0%%][> ]")
fileBytes, err := os.ReadFile(sourceFileName)
if err != nil {
panic(err)
}
fileB64 := base64.StdEncoding.EncodeToString(fileBytes)
var chunks []string
for i := 0; i < len(fileB64); i += chunkSize {
end := i + chunkSize
if end > len(fileB64) {
end = len(fileB64)
}
chunks = append(chunks, fileB64[i:end])
}
os.RemoveAll(StageDir)
os.MkdirAll(StageDir, 0755)
totalSize := int(math.Round(float64(len(fileB64)/chunkSize) + 0.5))
wg := &sync.WaitGroup{}
var finishedChunks int64
for i, chunk := range chunks {
wg.Add(1)
go func(i int, chunk string) {
hashedFileNameBytes := md5.Sum([]byte(sourceFileName))
hashedFileNameHex := hex.EncodeToString(hashedFileNameBytes[:])
header := fmt.Sprintf("[%s:%s:%d:%d]",
sourceFileName, hashedFileNameHex, i+1, totalSize)
generateQRCode(header+chunk, StageDir, fmt.Sprintf("stg-%d", i+1))
fc := atomic.AddInt64(&finishedChunks, 1)
progress := float64(fc) / float64(len(chunks)) * 100
barLen := int(progress / 10)
bar := strings.Repeat("=", barLen) + ">" + strings.Repeat(" ", 10-barLen)
if int(progress) > 0 {
fmt.Printf("\rGenerating QR stream [%3d%%][%s]", int(progress), bar)
}
wg.Done()
}(i, chunk)
}
wg.Wait()
generateGIF(StageDir, outputFlieName, delay)
os.RemoveAll(StageDir)
fmt.Println("\nQR stream generation is done! Output file: " + outputFlieName)
}
func generateQRCode(content string, outputDir string, outputFileName string) {
qrc, err := qrcode.New(content)
if err != nil {
panic(err)
}
w, err := standard.New(path.Join(outputDir, outputFileName+".png"), standard.WithBuiltinImageEncoder(standard.PNG_FORMAT))
if err != nil {
panic(err)
}
if err = qrc.Save(w); err != nil {
panic(err)
}
// convert to gif format
pf, err := os.Open(path.Join(outputDir, outputFileName+".png"))
if err != nil {
panic(err)
}
pImg, err := png.Decode(pf)
if err != nil {
panic(err)
}
gf, err := os.Create(path.Join(outputDir, outputFileName+".gif"))
if err != nil {
panic(err)
}
err = imgconv.Write(gf, pImg, &imgconv.FormatOption{Format: imgconv.GIF})
if err != nil {
panic(err)
}
pf.Close()
os.Remove(path.Join(outputDir, outputFileName+".png"))
}
func generateGIF(inputDir string, outputFilePath string, delay int) {
outGif := &gif.GIF{}
frameFiles, err := os.ReadDir(inputDir)
if err != nil {
panic(err)
}
for _, frameFileEntry := range frameFiles {
frameFile, err := os.Open(path.Join(inputDir, frameFileEntry.Name()))
if err != nil {
panic(err)
}
frame, err := gif.Decode(frameFile)
if err != nil {
panic(err)
}
frameFile.Close()
outGif.Image = append(outGif.Image, frame.(*image.Paletted))
outGif.Delay = append(outGif.Delay, delay)
}
of, err := os.Create(outputFilePath)
if err != nil {
panic(err)
}
defer of.Close()
err = gif.EncodeAll(of, outGif)
if err != nil {
panic(err)
}
}