Skip to content

Instantly share code, notes, and snippets.

@jackyef
Created July 15, 2019 16:49
Show Gist options
  • Save jackyef/253c06549efe52aac42267586957f572 to your computer and use it in GitHub Desktop.
Save jackyef/253c06549efe52aac42267586957f572 to your computer and use it in GitHub Desktop.
Rust Wasm medium writing
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use image;
use rqrr;
#[wasm_bindgen]
pub fn decode_qr(bytes: &[u8]) -> String {
let img = match image::load_from_memory(&bytes) {
Ok(v) => v,
Err(_e) => return format!("{}", "[Error] Failed when trying to load image"),
};
let img = img.to_luma();
// Prepare for detection
let mut img = rqrr::PreparedImage::prepare(img);
// Search for grids, without decoding
let grids = img.detect_grids();
if grids.len() != 1 {
return format!("{}", "[Error] No QR code detected in image")
}
// Decode the grid
let (_meta, content) = match grids[0].decode() {
Ok(v) => v,
Err(_e) => return format!("{}", "[Error] Failed decoding the image"),
};
return format!("{}", content);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment