kennytm / qrcode-rust

QR code encoder in Rust.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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?

@enaut I see thanks mate

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.