can I use this in a web application?
Raj2032 opened this issue · comments
Hi, I wanted to ask you, I wanted to make a web application that uses qrcode-rust
library. Is it possible for me to compile it to wasm and this will work ok?
You can... I used it with seed: https://pslink.teilgedanken.de/app/ (user+passwd demo)
You can find the code at:
https://github.com/enaut/pslink/blob/03e49aaa87844817f26f8fdf307b059401cac326/app/src/pages/list_links.rs#L1019-L1030
No problem it took me quite some time to figure everything out. So I'm glad if that code can help someone to reduce that time for them :)
Alternative to @enaut's generate_qr_png
, using image::codecs:png::PngEncoder
directly:
fn qrcode_png(content: &String) -> Vec<u8> {
let qrcode = QrCode::new(content).unwrap();
let image: image::ImageBuffer<Luma<u8>, Vec<u8>> = qrcode.render::<Luma<u8>>().build();
let mut buf: Vec<u8> = Vec::new();
let wd: u32 = image.width();
let ht: u32 = image.height();
let encoder: PngEncoder<&mut Vec<u8>> = PngEncoder::new(&mut buf);
encoder.encode(&image.into_raw(), wd, ht, ColorType::L8).expect("Cannot encode image");
buf
}
You could return this as a response body, for instance.