From 222324d954bde8b11066cb6895b173385f3df57f Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Tue, 1 Aug 2023 13:48:34 -0700 Subject: [PATCH] add qr module --- src/lib.rs | 1 + src/main.rs | 2 +- src/qr.rs | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/lib.rs create mode 100644 src/qr.rs diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..bae46d1 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +mod qr; diff --git a/src/main.rs b/src/main.rs index 9c59d23..17d8aa0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,7 +58,7 @@ impl eframe::App for Flasher { .build() .unwrap(); let bytes = ImageBuilder::default() - .fit_width(1200) + .fit_width(1100) .to_pixmap(&qr) .encode_png() .unwrap(); diff --git a/src/qr.rs b/src/qr.rs new file mode 100644 index 0000000..e97a850 --- /dev/null +++ b/src/qr.rs @@ -0,0 +1,38 @@ +use fast_qr::ECL; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub enum Mode { + Ascii, + Binary, +} + +pub fn get_size(len: usize, mode: Mode) -> usize { + if mode == Mode::Ascii { + // alphanumeric mode is only 5.5 bits per character, so 5.5 / 8 = 0.6875 + (len as f64 * 0.6875).ceil() as usize + } else { + len + } +} + +pub fn choose_version(size: usize, ecl: ECL) -> fast_qr::Version { + use fast_qr::Version::*; + let ecl = match ecl { + ECL::L => 0.7, + ECL::M => 0.15, + ECL::Q => 0.25, + ECL::H => 0.30, + }; + + let size = (size as f64 / ecl) as usize; + + match size { + 0..=7 => V01, + 8..=14 => V02, + 15..=24 => V03, + 25..=78 => V04, + + // + _ => V40, + } +}