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
use clap::{Parser, Subcommand};
use qart::qr;
#[derive(Parser)]
#[command(name = "qart")]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Build a functional QR code that looks like the provided image
Build {
/// Version number (size) of the QR code: 1-40
version: u8,
/// URL that the QR code will point to. Should not contain URL fragments or query strings.
url: String,
/// Relative path of the target image that the QR code will look like
image_path: String,
/// Path that the produced QR code will be saved to. Default is "code.png"
save_path: String,
/// The side length of each of the modules of the QR code in pixels. Default is 5
#[arg(long, default_value_t = 5)]
module_size: u32,
/// The brightness value at which brighter pixels will be white, and darker pixels will be black. Default is 128
#[arg(long, default_value_t = 128)]
threshold: u8,
/// Display the time taken to generate the QR code
#[arg(long)]
benchmark: bool,
/// Distribute uncontrollable pixels randomly instead of based off of contrast
#[arg(long)]
random: bool,
/// create debug version of QR codes
#[arg(long, default_value_t = false)]
debug: bool,
},
/// Generate a preview of a QR code that will quickly show what the image will look like as part of the QR code
Preview {
/// Version number (size) of the QR code: 1-40
version: u8,
/// Relative path of the target image that the QR code will look like
image_path: String,
/// Path that the produced QR code will be saved to. Default is "preview.png"
#[arg(long, default_value = "preview.png")]
save_path: String,
/// The brightness value at which brighter pixels will be white, and darker pixels will be black. Default is 128
#[arg(long, default_value_t = 128)]
threshold: u8,
/// Distribute uncontrollable pixels randomly instead of based off of contrast
#[arg(long)]
random: bool,
},
}
fn main() {
env_logger::init();
let cli = Cli::parse();
match cli.command {
Commands::Build {
version,
url,
image_path,
save_path,
module_size,
threshold,
benchmark,
random,
debug,
} => {
let start = std::time::Instant::now();
match qr::build(version, url, module_size, image_path, threshold, random, debug) {
Ok(img) => {
if let Err(e) = img.save(save_path) {
log::error!("Could not save image: {:#}", e);
};
if benchmark {
println!("Time Elapsed: {:?}", start.elapsed());
}
}
Err(e) => log::error!("Could not create QR Code: {}", e),
}
}
Commands::Preview {
version,
image_path,
save_path,
threshold,
random,
} => {
let code = qr::preview(version, image_path, threshold, random);
match code {
Ok(img) => {
if let Err(e) = img.save(save_path) {
log::error!("Could not save image: {:#}", e);
};
}
Err(e) => log::error!("{}", e),
}
}
}
}
// times for v40 qr code
// non threaded: 14.73s
// threaded: 6.52s
// threaded and remove unnecessary calls: 6.72s (?)
// compiled: 284.87ms