I am implementing image-occlusion
for Anki ecosystem (more in this branch GitHub - krmanik/anki at image-occlusion).
I need to get base64, width and height of image. I have added this Cargo.toml file in rslib
base64 = "0.13.0"
imagesize = "0.9"
Imported the dependencies
use base64::{encode};
use imagesize::size;
anki/metadata.rs at 72a251e2b34e1669da9cc08d043c81b4cd77a15d · krmanik/anki · GitHub
but getting errors.
error[E0432]: unresolved import `base64`
--> rslib/src/image_occlusion/metadata.rs:4:5
|
4 | use base64::{encode};
| ^^^^^^ use of undeclared crate or module `base64`
error[E0432]: unresolved import `imagesize`
--> rslib/src/image_occlusion/metadata.rs:5:5
|
5 | use imagesize::size;
| ^^^^^^^^^ use of undeclared crate or module `imagesize`
I have tested by creating a new cargo project where I get the result without errors. So, what is correct way to import the dependencies in above code?
use std::{fs, fs::File, io::Read, path::Path};
use base64::encode;
use imagesize::size;
fn main() {
let img = "image.png";
let data = get_base64(img);
fs::write("base64.txt", data).expect("Unable to write file");
let (width, height) = match size(img) {
Ok(dim) => {(dim.width, dim.height)},
Err(why) => {panic!("Error! {}", why)}
};
println!("{}, {}", width.to_string(), height.to_string());
}
fn get_base64(path: &str) -> String {
let ext = Path::new(&path).extension().unwrap().to_str().unwrap();
let mut file = File::open(path).unwrap();
let mut vec = Vec::new();
let _ = file.read_to_end(&mut vec);
let base64 = encode(vec);
return format!("data:image/{};base64,{}", ext, base64.replace("\r\n", ""));
}