forked from samirdjelal/captcha-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
180 lines (150 loc) · 4.34 KB
/
lib.rs
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
#![doc(html_root_url = "https://docs.rs/captcha-rs/latest")]
//! Generate a verification image.
//!
//! ```rust
//! use captcha_rs::{CaptchaBuilder};
//!
//! let captcha = CaptchaBuilder::new()
//! .length(5)
//! .width(130)
//! .height(40)
//! .dark_mode(false)
//! .complexity(1) // min: 1, max: 10
//! .build();
//!
//! println!("text: {}", captcha.text);
//! let base_img = captcha.to_base64();
//! println!("base_img: {}", base_img);
//! ```
use image::DynamicImage;
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 image: DynamicImage,
pub dark_mode: bool,
}
impl Captcha {
pub fn to_base64(&self) -> String {
to_base64_str(&self.image)
}
}
#[derive(Default)]
pub struct CaptchaBuilder {
text: Option<String>,
width: Option<u32>,
height: Option<u32>,
dark_mode: Option<bool>,
complexity: Option<u32>,
}
impl CaptchaBuilder {
pub fn new() -> Self {
CaptchaBuilder {
text: None,
width: None,
height: None,
dark_mode: None,
complexity: None,
}
}
pub fn text(mut self, text: String) -> Self {
self.text = Some(text);
self
}
pub fn length(mut self, length: usize) -> Self {
// Generate an array of captcha characters
let res = captcha::get_captcha(length);
self.text = Some(res.join(""));
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 text = self.text.unwrap_or(captcha::get_captcha(5).join(""));
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);
// Create a white background image
let mut image = get_image(width, height, dark_mode);
let res: Vec<String> = text.chars().map(|x| x.to_string()).collect();
// Loop to write the verification code string into the background image
cyclic_write_character(&res, &mut image, dark_mode);
// Draw interference lines
draw_interference_line(&mut image, dark_mode);
draw_interference_line(&mut image, dark_mode);
// Draw a distraction circle
draw_interference_ellipse(2, &mut image, dark_mode);
draw_interference_ellipse(2, &mut image, dark_mode);
if complexity > 1 {
gaussian_noise_mut(&mut image, (complexity - 1) as f64, ((10 * complexity) - 10) as f64, ((5 * complexity) - 5) as u64);
salt_and_pepper_noise_mut(&mut image, (0.001 * complexity as f64) - 0.001, (0.5 * complexity as f64) as u64);
}
Captcha {
text,
image: DynamicImage::ImageRgb8(image),
dark_mode,
}
}
}
#[cfg(test)]
mod tests {
use crate::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 = CaptchaBuilder::new()
.text(String::from("based"))
.width(200)
.height(70)
.dark_mode(false)
.build();
let duration = start.elapsed();
println!("Time elapsed in generating captcha() is: {:?}", duration);
assert_eq!(captcha.text.len(), 5);
let base_img = captcha.to_base64();
assert!(base_img.starts_with("data:image/jpeg;base64,"));
println!("text: {}", captcha.text);
println!("base_img: {}", base_img);
}
#[test]
fn it_generates_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 base_img = captcha.to_base64();
assert!(base_img.starts_with("data:image/jpeg;base64,"));
println!("text: {}", captcha.text);
println!("base_img: {}", base_img);
}
}