This commit is contained in:
2020-05-24 22:19:28 +08:00
parent 99a4348c91
commit da918ad5fd
3 changed files with 18 additions and 7 deletions

View File

@@ -1,25 +1,34 @@
use std::ffi::CString;
use std::os::raw::c_char;
pub const SAMPLE_QR_BYTES: &[u8] = std::include_bytes!("sample_qr.png");
pub const SAMPLE_QR_BYTES: &[u8] = std::include_bytes!("sample_qr.png");
pub const SAMPLE_QR_BYTES_2: &[u8] = std::include_bytes!("sample_qr_2.png");
#[no_mangle]
pub extern "C" fn parse_default_qr() -> *mut c_char {
let s = CString::new(parse_qr()).unwrap();
let s = CString::new(parse_qr(SAMPLE_QR_BYTES)).unwrap();
s.into_raw()
}
pub fn parse_qr() -> String {
let img = image::load_from_memory(SAMPLE_QR_BYTES).unwrap();
#[no_mangle]
pub extern "C" fn parse_default_qr_2() -> *mut c_char {
let s = CString::new(parse_qr(SAMPLE_QR_BYTES_2)).unwrap();
s.into_raw()
}
pub fn parse_qr(bs: &[u8]) -> String {
let img = match image::load_from_memory(bs) {
Ok(i) => i, Err(e) => return format!("Load image error: {}", e),
};
let decoder = bardecoder::default_decoder();
let mut ret = vec![];
let results = decoder.decode(&img);
for result in results {
let qr = result.unwrap();
let qr = result.unwrap_or_else(|e| format!("Decode QR error: {}", e));
println!("{}", qr);
ret.push(qr);
}
ret.join(",")
ret.join("||")
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 212 KiB