-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(mini-functions): 🚑 added missing examples for qrc
- Loading branch information
1 parent
566b203
commit b0acf56
Showing
1 changed file
with
137 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,118 +1,155 @@ | ||
// Copyright © 2023 Mini Functions library. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
use image::{Rgba, RgbaImage}; | ||
use mini_functions::qr::QRCode; | ||
use mini_functions::qr::{add_image_watermark, qr_code_to}; | ||
extern crate image; | ||
use std::fs; | ||
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/"; | ||
const URL: &str = "https://minifunctions.com/"; // Define a constant for the URL to be encoded | ||
|
||
fn add_watermark_to_qrcode( | ||
qrcode: &mut RgbaImage, | ||
watermark_path: &str, | ||
) -> Result<(), String> { | ||
match image::open(watermark_path) { | ||
Ok(watermark_img) => { | ||
let watermark_rgba = watermark_img.into_rgba8(); | ||
add_image_watermark!(qrcode, &watermark_rgba); | ||
Ok(()) | ||
} | ||
Err(e) => Err(format!("Failed to open watermark image: {}", e)), | ||
} | ||
} | ||
|
||
fn process_qrcode<F>(url: &str, process_fn: F, file_name: &str) | ||
where | ||
F: FnOnce(&QRCode) -> RgbaImage, | ||
{ | ||
let qrcode = QRCode::from_string(url.to_string()); | ||
let image = process_fn(&qrcode); | ||
save_image(&image, file_name); | ||
} | ||
|
||
fn save_image(image: &RgbaImage, file_name: &str) { | ||
match image.save(file_name) { | ||
Ok(_) => println!("🦀 File created: ✅ {}", file_name), | ||
Err(e) => { | ||
println!("🦀 File creation failed: ❌ {}: {}", file_name, e) | ||
} | ||
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" | ||
} | ||
} | ||
|
||
fn save_svg(data: &str, file_name: &str) { | ||
match fs::write(file_name, data) { | ||
Ok(_) => println!("🦀 File created: ✅ {}", file_name), | ||
Err(e) => { | ||
println!("🦀 File creation failed: ❌ {}: {}", file_name, e) | ||
} | ||
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" | ||
} | ||
} | ||
|
||
fn remove_file(file_name: &str) { | ||
match fs::remove_file(file_name) { | ||
Ok(_) => println!("🦀 File removed: ✅ {}", file_name), | ||
Err(e) => { | ||
println!("🦀 File removal failed: ❌ {}: {}", file_name, e) | ||
} | ||
// 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" | ||
} | ||
} | ||
|
||
fn main() { | ||
// Generate QR Code, save it as a PNG file and remove it after. | ||
process_qrcode(URL, |qrcode| qrcode.to_png(512), "qrcode.png"); | ||
remove_file("qrcode.png"); | ||
|
||
// Generate QR colorized QR Code, save it as a PNG file and remove it after. | ||
process_qrcode( | ||
URL, | ||
|qrcode| qrcode.colorize(Rgba([255, 0, 0, 255])), | ||
"qrcode_colorized.png", | ||
); | ||
remove_file("qrcode_colorized.png"); | ||
|
||
// Generate QR Code, resize it and save it as a PNG file and remove it after. | ||
process_qrcode( | ||
URL, | ||
|qrcode| qrcode.resize(512, 512), | ||
"qrcode_resized.png", | ||
); | ||
remove_file("qrcode_resized.png"); | ||
|
||
// SVG creation with decoupled functions | ||
// 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); | ||
save_svg(&qrcode_svg, "qrcode.svg"); | ||
remove_file("qrcode.svg"); | ||
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" | ||
} | ||
|
||
// QRCode with custom data | ||
let custom_qrcode = QRCode::new(vec![0x61, 0x62, 0x63]); | ||
let custom_qr_image = custom_qrcode.resize(512, 512); | ||
save_image(&custom_qr_image, "qrcode_custom.png"); | ||
remove_file("qrcode_custom.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 | ||
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 | ||
// Note: Assuming you have these macros implemented | ||
let qrcode_png = qr_code_to!(URL.into(), "png", 512); | ||
save_image(&qrcode_png, "qrcode_macro.png"); | ||
remove_file("qrcode_macro.png"); | ||
|
||
// Create a new QRCode using the macro qr_code_to into a GIF representation with a custom size of 512x512 | ||
let qrcode_gif = qr_code_to!(URL.into(), "gif", 512); | ||
save_image(&qrcode_gif, "qrcode_macro.gif"); | ||
remove_file("qrcode_macro.gif"); | ||
|
||
// Add watermark to QRCode and save | ||
let watermark_qrcode = QRCode::from_string(URL.to_string()); | ||
let mut watermark_qr_img = watermark_qrcode.to_png(512); | ||
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" | ||
} | ||
|
||
match add_watermark_to_qrcode(&mut watermark_qr_img, "bubba.ico") { | ||
// 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(_) => { | ||
save_image(&watermark_qr_img, "qrcode_watermarked.png") | ||
} | ||
Err(e) => println!("🦀 Error adding watermark: {}", e), | ||
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" | ||
} | ||
|
||
remove_file("qrcode_watermarked.png"); | ||
} | ||
} |