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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#![doc(html_root_url = "https://docs.rs/captcha-rs/latest")]
use imageproc::noise::{gaussian_noise_mut, salt_and_pepper_noise_mut};
use crate::captcha::{cyclic_write_character, draw_interference_ellipse, draw_interference_line, get_image, to_base64_str};
mod captcha;
pub struct Captcha {
pub text: String,
pub base_img: String,
pub dark_mode: bool,
}
impl Captcha {
pub fn new(length: usize, width: u32, height: u32, dark_mode: bool) -> Self {
let res = captcha::get_captcha(length);
let text = res.join("");
let base_img = captcha::get_captcha_img(res, width, height, dark_mode);
Captcha {
text,
base_img,
dark_mode,
}
}
}
pub struct CaptchaBuilder {
length: Option<usize>,
width: Option<u32>,
height: Option<u32>,
dark_mode: Option<bool>,
complexity: Option<u32>,
}
impl CaptchaBuilder {
pub fn new() -> Self {
CaptchaBuilder {
length: None,
width: None,
height: None,
dark_mode: None,
complexity: None,
}
}
pub fn length(mut self, length: usize) -> Self {
self.length = Some(length);
self
}
pub fn width(mut self, width: u32) -> Self {
self.width = Some(width);
self
}
pub fn height(mut self, height: u32) -> Self {
self.height = Some(height);
self
}
pub fn dark_mode(mut self, dark_mode: bool) -> Self {
self.dark_mode = Some(dark_mode);
self
}
pub fn complexity(mut self, complexity: u32) -> Self {
let mut complexity = complexity;
if complexity > 10 { complexity = 10; }
if complexity < 1 { complexity = 1; }
self.complexity = Some(complexity);
self
}
pub fn build(self) -> Captcha {
let length = self.length.unwrap_or(5);
let width = self.width.unwrap_or(130);
let height = self.height.unwrap_or(40);
let dark_mode = self.dark_mode.unwrap_or(false);
let complexity = self.complexity.unwrap_or(1);
let res = captcha::get_captcha(length);
let text = res.join("");
let mut image = get_image(width, height, dark_mode);
cyclic_write_character(&res, &mut image, dark_mode);
draw_interference_line(&mut image, dark_mode);
draw_interference_line(&mut image, dark_mode);
draw_interference_ellipse(2, &mut image, dark_mode);
draw_interference_ellipse(2, &mut image, dark_mode);
gaussian_noise_mut(&mut image, (complexity.clone() - 1) as f64, ((10 * complexity.clone()) - 10) as f64, ((5 * complexity.clone()) - 5) as u64);
salt_and_pepper_noise_mut(&mut image, ((0.001 * complexity.clone() as f64) - 0.001) as f64, (0.5 * complexity.clone() as f64) as u64);
let base_img = to_base64_str(image);
Captcha {
text,
base_img,
dark_mode,
}
}
}
#[cfg(test)]
mod tests {
use crate::{Captcha, CaptchaBuilder};
#[test]
fn it_generates_a_captcha() {
let dark_mode = false;
let text_length = 5;
let width = 130;
let height = 40;
let start = std::time::Instant::now();
let captcha = Captcha::new(text_length, width, height, dark_mode);
let duration = start.elapsed();
println!("Time elapsed in generating captcha() is: {:?}", duration);
assert_eq!(captcha.text.len(), 5);
let start_with = captcha.base_img.starts_with("data:image/png;base64,");
assert_eq!(start_with, true);
println!("text: {}", captcha.text);
println!("base_img: {}", captcha.base_img);
}
#[test]
fn it_generate_captcha_using_builder() {
let start = std::time::Instant::now();
let captcha = CaptchaBuilder::new()
.length(5)
.width(200)
.height(70)
.dark_mode(false)
.complexity(5)
.build();
let duration = start.elapsed();
println!("Time elapsed in generating captcha() is: {:?}", duration);
assert_eq!(captcha.text.len(), 5);
let start_with = captcha.base_img.starts_with("data:image/png;base64,");
assert_eq!(start_with, true);
println!("text: {}", captcha.text);
println!("base_img: {}", captcha.base_img);
}
}