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
//! # QRC Example
//!
//! This example demonstrates the usage of the `qrc` crate for generating and manipulating QR codes.
//! It includes examples of creating a QR code, adding watermarks, and other functionalities provided by the `qrc` crate.
extern crate image;
use image::{imageops, ImageBuffer, Rgba, RgbaImage};
extern crate qrc;
use self::qrc::{add_image_watermark, qr_code, qr_code_to, QRCode};
use std::fs; // Import the fs module from the standard library // Import the QRCode struct from the mini_functions crate
const URL: &str = "https://minifunctions.com/"; // Define a constant for the URL to be encoded
fn main() {
// Create a new QRCode using the QRCode::from_string() function and convert it to a PNG representation
let qrcode = QRCode::from_string(URL.to_string()); // Create a new QRCode using the QRCode::from_string() function
let png = qrcode.to_png(512); // Convert the QRCode into a PNG representation
let png_data = png.into_raw(); // Convert the PNG representation of the QRCode into a vector of bytes
let png_image = ImageBuffer::<Rgba<u8>, Vec<u8>>::from_raw(21, 21, png_data).unwrap();
println!(
"🦀 fn to_png(): ✅ {:?}",
png_image.save("qrcode.png")
); // Print the PNG representation of the QRCode
match png_image.save("qrcode.png") {
Ok(_) => println!("🦀 png file created: ✅ qrcode.png"), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode1.png"
Err(e) => println!("🦀 png file created: ❌ qrcode.png: {e}"), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode1.png"
}
match fs::remove_file("qrcode.png") {
Ok(_) => println!("🦀 png file removed: ✅ qrcode.png"), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode1.png"
Err(e) => println!("🦀 png file removed: ❌ qrcode.png: {e}"), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode1.png"
}
// Create a new QRCode using the QRCode::from_string() function and convert it to a PNG representation with a custom color
let qrcode = QRCode::from_string(URL.to_string());
let red = Rgba([255, 0, 0, 255]);
let red_qrcode = qrcode.colorize(red); // Create a new QRCode using the QRCode::from_string() function and convert it to a PNG representation with a custom color
let img: RgbaImage = red_qrcode; // Convert the colorized QR code to a PNG image.
let new_width = 512;
let new_height = 512;
let resized_img = imageops::resize(&img, new_width, new_height, imageops::FilterType::Nearest);
let image: ImageBuffer<Rgba<u8>, Vec<u8>> = resized_img; // Convert the colorized QR code to a PNG image.
println!(
"🦀 fn colorize(): ✅ {:?}",
image.save("qrcode_colorized.png")
); // Print the PNG representation of the QRCode
match image.save("qrcode_colorized.png") {
Ok(_) => println!("🦀 colorized png file created: ✅ qrcode_colorized.png"), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode.png"
Err(e) => println!("🦀 colorized png file created: ❌ qrcode_colorized.png: {e}",), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode.png"
}
match fs::remove_file("qrcode_colorized.png") {
Ok(_) => println!("🦀 colorized png file removed: ✅ qrcode_colorized.png"), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode.png"
Err(e) => println!("🦀 colorized png file removed: ❌ qrcode_colorized.png: {e}",), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode.png"
}
// Create a new QRCode using the QRCode::from_string() function and convert it to an SVG representation
let qrcode = QRCode::from_string(URL.to_string());
let qrcode_svg = qrcode.to_svg(512); // Convert the QRCode into an SVG representation
match fs::write("qrcode.svg", qrcode_svg) {
Ok(_) => println!("🦀 svg file created: ✅ qrcode.svg"), // Print the path to the SVG representation of the QRCode that was saved to a file called "qrcode.svg"
Err(e) => println!("🦀 svg file created: ❌ qrcode.svg: {e}"), // Print the path to the SVG representation of the QRCode that was saved to a file called "qrcode.svg"
}
match fs::remove_file("qrcode.svg") {
Ok(_) => println!("🦀 svg file removed: ✅ qrcode.svg"), // Print the path to the SVG representation of the QRCode that was saved to a file called "qrcode.svg"
Err(e) => println!("🦀 svg file removed: ❌ qrcode.svg: {e}"), // Print the path to the SVG representation of the QRCode that was saved to a file called "qrcode.svg"
}
// Create a new QRCode using the QRCode::from_string() function and convert it to a PNG representation with a custom size
let qrcode = QRCode::new(vec![0x61, 0x62, 0x63]);
let resized_image: RgbaImage = qrcode.resize(512, 512);
println!(
"🦀 fn resize(): ✅ {:?}",
resized_image.save("qrcode_resized.png")
); // Print the PNG representation of the QRCode
match resized_image.save("qrcode_resized.png") {
Ok(_) => println!("🦀 resized file created: ✅ qrcode_resized.png"), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode.png"
Err(e) => println!("🦀 resized file created: ❌ qrcode_resized.png: {e}",), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode.png"
}
match fs::remove_file("qrcode_resized.png") {
Ok(_) => println!("🦀 resized file removed: ✅ qrcode_resized.png"), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode.png"
Err(e) => println!("🦀 resized file removed: ❌ qrcode_resized.png: {e}",), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode.png"
}
// Create a new QRCode using the QRCode::from_string() function and convert it to a PNG representation with a custom size
let qrcode = QRCode::new(vec![0x61, 0x62, 0x63]);
let resized_image: RgbaImage = qrcode.resize(512, 512);
println!(
"🦀 fn resize(): ✅ {:?}",
resized_image.save("qrcode_resized.png")
); // Print the PNG representation of the QRCode with a custom size of 512x512
match resized_image.save("qrcode_resized.png") {
Ok(_) => println!("🦀 resized file created: ✅ qrcode_resized.png"), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode.png"
Err(e) => println!("🦀 resized file created: ❌ qrcode_resized.png: {e}",), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode.png"
}
match fs::remove_file("qrcode_resized.png") {
Ok(_) => println!("🦀 resized file removed: ✅ qrcode_resized.png"), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode.png"
Err(e) => println!("🦀 resized file removed: ❌ qrcode_resized.png: {e}",), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode.png"
}
// Create a new QRCode using the macro qr_code and convert it to an SVG representation with a custom size of 512x512
let qrcode = qr_code!(URL.into());
let qrcode_svg = qrcode.to_svg(512); // Convert the QRCode into an SVG representation with a custom size of 512x512
match fs::write("qrcode.svg", qrcode_svg) {
Ok(_) => println!("🦀 svg file created: ✅ qrcode.svg"), // Print the path to the SVG representation of the QRCode that was saved to a file called "qrcode.svg"
Err(e) => println!("🦀 svg file created: ❌ qrcode.svg: {e}"), // Print the path to the SVG representation of the QRCode that was saved to a file called "qrcode.svg"
}
match fs::remove_file("qrcode.svg") {
Ok(_) => println!("🦀 svg file removed: ✅ qrcode.svg"), // Print the path to the SVG representation of the QRCode that was saved to a file called "qrcode.svg"
Err(e) => println!("🦀 svg file removed: ❌ qrcode.svg: {e}"), // Print the path to the SVG representation of the QRCode that was saved to a file called "qrcode.svg"
}
// Create a new QRCode using the macro qr_code_to into a PNG representation with a custom size of 512x512
let qrcode = qr_code_to!(URL.into(), "png", 512);
match qrcode.save("qrcode.png") {
Ok(_) => println!("🦀 png file created: ✅ qrcode.png"), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode.png"
Err(e) => println!("🦀 png file created: ❌ qrcode.png: {e}",), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode.png"
}
match fs::remove_file("qrcode.png") {
Ok(_) => println!("🦀 png file removed: ✅ qrcode.png"), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode.png"
Err(e) => println!("🦀 png file removed: ❌ qrcode.png: {e}",), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode.png"
}
// Create a new QRCode using the macro qr_code_from into a GIF representation with a custom size of 512x512
let qrcode = qr_code_to!(URL.into(), "gif", 512);
match qrcode.save("qrcode.gif") {
Ok(_) => println!("🦀 gif file created: ✅ qrcode.gif"), // Print the path to the GIF representation of the QRCode that was saved to a file called "qrcode.gif"
Err(e) => println!("🦀 gif file created: ❌ qrcode.gif: {e}",), // Print the path to the GIF representation of the QRCode that was saved to a file called "qrcode.gif"
}
match fs::remove_file("qrcode.gif") {
Ok(_) => println!("🦀 gif file removed: ✅ qrcode.gif"), // Print the path to the GIF representation of the QRCode that was saved to a file called "qrcode.gif"
Err(e) => println!("🦀 gif file removed: ❌ qrcode.gif: {e}",), // Print the path to the GIF representation of the QRCode that was saved to a file called "qrcode.gif"
}
// Create a new QRCode using the macro qr_code_to into a JPEG representation with a custom size of 512x512
let qrcode = qr_code_to!(URL.into(), "jpg", 512);
match qrcode.save("qrcode.jpg") {
Ok(_) => println!("🦀 jpg file created: ✅ qrcode.jpg"), // Print the path to the JPG representation of the QRCode that was saved to a file called "qrcode.jpg"
Err(e) => println!("🦀 jpg file created: ❌ qrcode.jpg: {e}",), // Print the path to the JPEG representation of the QRCode that was saved to a file called "qrcode.jpg"
}
match fs::remove_file("qrcode.jpg") {
Ok(_) => println!("🦀 jpg file removed: ✅ qrcode.jpg"), // Print the path to the JPG representation of the QRCode that was saved to a file called "qrcode.jpg"
Err(e) => println!("🦀 jpg file removed: ❌ qrcode.jpg: {e}",), // Print the path to the JPEG representation of the QRCode that was saved to a file called "qrcode.jpg"
}
// Create a new QRCode add a watermark to it and save it as a PNG file
let qrcode = QRCode::from_string(URL.to_string());
let mut qrcode_img = qrcode.to_png(512);
let watermark_img = image::open("bubba.ico").unwrap().into_rgba8();
add_image_watermark!(&mut qrcode_img, &watermark_img);
match qrcode_img.save("qrcode_watermarked.png") {
Ok(_) => println!("🦀 png file with watermark: ✅ qrcode_watermarked.png"), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode1.png"
Err(e) => println!("🦀 png file with watermark: ❌ qrcode_watermarked.png: {e}"), // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode1.png"
}
match fs::remove_file("qrcode_watermarked.png") {
Ok(_) => {
println!("🦀 png file with watermark removed: ✅ qrcode_watermarked.png")
} // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode1.png"
Err(e) => {
println!("🦀 png file with watermark removed: ❌ qrcode_watermarked.png: {e}")
} // Print the path to the PNG representation of the QRCode that was saved to a file called "qrcode1.png"
}
}