0

I use qrcode-rust which returns its result as image::ImageBuffer. I need to post the png result to thumbor server. How to convert this image buffer to Vec ?.

I try code below but my buf moved and I can do nothing about it.

let code = QrCode::new(arg1.as_bytes()).unwrap();
let images = code.render::<Luma<u8>>().build();
let mut buf: Vec<u8> = Vec::new();
let dims = images.dimensions();
let mut headers = HeaderMap::new();

image::png::PNGEncoder::new(buf)
    .encode(&images.into_raw(), dims.0, dims.1, ColorType::Gray(8))
    .expect("Error on encoding to png");

let len = buf.len(); //error here
headers.insert(CONTENT_TYPE, "image/png".parse().unwrap());
headers.insert("Slug", "photo.jpg".parse().unwrap());
hellow
  • 12,430
  • 7
  • 56
  • 79
henky
  • 1

1 Answers1

0

I copied an answer from How do I encode a Rust Piston image and get the result in memory? and changed

image::png::PNGEncoder::new(buf.by_ref()))
    .encode(
         &images.into_raw(), 
         dims.0, 
         dims.1, 
         ColorType::Gray(8),
    ).expect("Error on encoding to png");

And everything works now.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
henky
  • 1