qrcode-rust icon indicating copy to clipboard operation
qrcode-rust copied to clipboard

can I use this in a web application?

Open Raj2032 opened this issue 3 years ago • 4 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?

Raj2032 avatar Dec 25 '21 13:12 Raj2032

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

enaut avatar May 10 '22 08:05 enaut

@enaut I see thanks mate

Raj2032 avatar May 10 '22 09:05 Raj2032

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 :)

enaut avatar May 10 '22 18:05 enaut

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.

fc7 avatar Jun 19 '22 14:06 fc7