Skip to content

Instantly share code, notes, and snippets.

@maurorappa
Created October 10, 2018 22:11
Show Gist options
  • Save maurorappa/f8d7651bf4e2a2cd1636c47819fe0c3a to your computer and use it in GitHub Desktop.
Save maurorappa/f8d7651bf4e2a2cd1636c47819fe0c3a to your computer and use it in GitHub Desktop.
decode file from a series of QR code images
package main
import (
"flag"
b64 "encoding/base64"
"fmt"
"github.com/clsung/grcode"
//"github.com/maruel/natural"
"io/ioutil"
"os"
//"sort"
"strings"
)
func main() {
image_dir := flag.String("i", "", "Directory containing the QR png images")
filename := flag.String("f", "", "File to be created")
verbose := flag.Bool("v", false, "verbose")
flag.Parse()
if flag.NFlag() < 2 {
fmt.Println("specify the directory and the filename!")
os.Exit(1)
}
original_file := *image_dir + "/" + *filename
dump, err := os.OpenFile(original_file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0640)
if err != nil {
fmt.Printf("%s", err)
}
files, err := ioutil.ReadDir(*image_dir)
if err != nil {
fmt.Println(err)
}
//sort.Sort(natural.StringSlice(files))
for _, file := range files {
filename := file.Name()
if strings.Contains(filename, "png") {
if *verbose {
fmt.Printf("Processing: %s \n", filename)
}
results, err := grcode.GetDataFromFile(*image_dir+"/"+filename)
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
if len(results) == 0 {
fmt.Printf("No qrcode detected from file: %s\n", filename)
os.Exit(1)
}
if err != nil {
fmt.Println(err)
}
for _, result := range results {
//fmt.Printf("%s\n", result)
data, _ := b64.StdEncoding.DecodeString(result)
_,err := dump.Write(data)
if err != nil {
fmt.Printf("%s\n", err)
}
}
}
}
_ = dump.Close()
fmt.Printf("Done, %s created", original_file)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment