Created
October 10, 2018 22:09
-
-
Save maurorappa/ac7bc469cf3ba160e6fbb877e0d8a949 to your computer and use it in GitHub Desktop.
encode file as QRcode image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "flag" | |
| "fmt" | |
| b64 "encoding/base64" | |
| "github.com/skip2/go-qrcode" | |
| "math" | |
| "os" | |
| //"strconv" | |
| "strings" | |
| ) | |
| func main() { | |
| filename := flag.String("f", "", "File to be encoded") | |
| verbose := flag.Bool("v", false, "verbose") | |
| flag.Parse() | |
| if flag.NFlag() == 0 { | |
| fmt.Println("specify a file!") | |
| os.Exit(1) | |
| } | |
| file, err := os.Open(*filename) | |
| if err != nil { | |
| fmt.Println(err) | |
| os.Exit(1) | |
| } | |
| defer file.Close() | |
| fileInfo, _ := file.Stat() | |
| var fileSize int64 = fileInfo.Size() | |
| const fileChunk = 2048 | |
| // calculate total number of parts the file will be chunked into | |
| totalPartsNum := uint64(math.Ceil(float64(fileSize) / float64(fileChunk))) | |
| if *verbose { | |
| fmt.Printf("Splitting to %d pieces.\n", totalPartsNum) | |
| } | |
| for i := uint64(0); i < totalPartsNum; i++ { | |
| partSize := int(math.Min(fileChunk, float64(fileSize-int64(i*fileChunk)))) | |
| partBuffer := make([]byte, partSize) | |
| file.Read(partBuffer) | |
| // write to disk | |
| progressive_string := strings.Repeat("0",int(i)) | |
| fileName := "qr_" + *filename + "_" + progressive_string + ".png" | |
| sEnc := b64.StdEncoding.EncodeToString(partBuffer) | |
| err := qrcode.WriteFile(sEnc, qrcode.Low, 256, fileName) | |
| if *verbose { | |
| fmt.Println("creating ", fileName) | |
| } | |
| if err != nil { | |
| fmt.Printf("%s!\n", err) | |
| } | |
| } | |
| fmt.Printf("Done, check the %d png images in the current directory\n",int(totalPartsNum) ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment