refine qrcode handling

- expand max text.len to 4096
- throw error on overrun
- simplify qrcode rendering (reduced memory & math)
- correct module size by 1px
This commit is contained in:
nworbnhoj 2022-05-08 10:00:41 +10:00
parent ea6cf27ed9
commit dd1ce051d1
3 changed files with 47 additions and 31 deletions

View file

@ -52,5 +52,8 @@
"ja": "[ 何かのキーを押してください。]", "ja": "[ 何かのキーを押してください。]",
"zh": "[ 按任意键 ]", "zh": "[ 按任意键 ]",
"en-tts": "Press any key" "en-tts": "Press any key"
},
"notification.qrcode.error": {
"en": "Error: too much data for qrcode"
} }
} }

View file

@ -2,8 +2,8 @@ use crate::*;
use graphics_server::api::*; use graphics_server::api::*;
use locales::t;
use core::fmt::Write; use core::fmt::Write;
use locales::t;
use qrcode::{Color, QrCode}; use qrcode::{Color, QrCode};
use std::convert::TryInto; use std::convert::TryInto;
@ -42,20 +42,29 @@ impl Notification {
pub fn set_qrcode(&mut self, setting: Option<&str>) { pub fn set_qrcode(&mut self, setting: Option<&str>) {
match setting { match setting {
Some(setting) => { Some(setting) => {
let qrcode = QrCode::new(setting).unwrap(); let qrcode = match QrCode::new(setting) {
self.qrwidth = qrcode.width(); Ok(code) => code,
self.qrcode = Vec::new(); Err(e) => QrCode::new(t!("notification.qrcode.error", xous::LANG)).unwrap(),
for color in qrcode.to_colors().iter() { };
match color { self.qrwidth = qrcode.width();
Color::Dark => self.qrcode.push(true), log::info!(
Color::Light => self.qrcode.push(false), "qrcode {}x{} : {} bytes ",
} self.qrwidth,
} self.qrwidth,
}, setting.len()
);
self.qrcode = Vec::new();
for color in qrcode.to_colors().iter() {
match color {
Color::Dark => self.qrcode.push(true),
Color::Light => self.qrcode.push(false),
}
}
}
None => { None => {
self.qrcode = Vec::new(); self.qrcode = Vec::new();
self.qrwidth = 0; self.qrwidth = 0;
}, }
} }
} }
fn draw_text(&self, at_height: i16, modal: &Modal) { fn draw_text(&self, at_height: i16, modal: &Modal) {
@ -106,30 +115,33 @@ impl Notification {
let qrcode_width_px = qrcode_modules * mod_size_px; let qrcode_width_px = qrcode_modules * mod_size_px;
let quiet_px: i16 = (canvas_width - qrcode_width_px) / 2; let quiet_px: i16 = (canvas_width - qrcode_width_px) / 2;
// Iterate thru qrcode and draw each module as a rectangle (square) // Iterate thru qrcode and stamp each square module like a typewriter
let black = DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1); let black = DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1);
let top = at_height + 4 * modal.margin + quiet_px; let top = at_height + 4 * modal.margin + quiet_px;
let left = modal.margin + quiet_px; let left = modal.margin + quiet_px;
let mut top_left; let right = left + qrcode_modules * mod_size_px;
let mut bot_right; let mut module = Rectangle::new_with_style(
let mut x: i16; Point::new(0, 0),
let mut y: i16; Point::new(mod_size_px-1, mod_size_px-1),
black,
);
let step = Point::new(mod_size_px, 0);
let cr_lf = Point::new(-qrcode_modules * mod_size_px, mod_size_px);
let mut j: i16; let mut j: i16;
for (i, module) in self.qrcode.iter().enumerate() { module.translate(Point::new(right, top));
for (i, stamp) in self.qrcode.iter().enumerate() {
j = i.try_into().unwrap(); j = i.try_into().unwrap();
x = left + (j % qrcode_modules) * mod_size_px; if j % qrcode_modules == 0 {
y = top + (j / qrcode_modules) * mod_size_px; module.translate(cr_lf);
if *module { }
top_left = Point::new(x, y); if *stamp {
bot_right = Point::new(x + mod_size_px, y + mod_size_px);
modal modal
.gam .gam
.draw_rectangle( .draw_rectangle(modal.canvas, module)
modal.canvas,
Rectangle::new_with_style(top_left, bot_right, black),
)
.expect("couldn't draw qrcode module"); .expect("couldn't draw qrcode module");
} }
module.translate(step);
} }
} }
} }

View file

@ -37,7 +37,8 @@ pub struct ManagedPromptWithTextResponse {
pub struct ManagedNotification { pub struct ManagedNotification {
pub token: [u32; 4], pub token: [u32; 4],
pub message: xous_ipc::String<1024>, pub message: xous_ipc::String<1024>,
pub qrtext: Option<xous_ipc::String<1024>>, // A Type 40 (177x177) qrcode with Medium data correction can encode max 3391 alphanumeric characters
pub qrtext: Option<xous_ipc::String<4096>>,
} }
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)] #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
pub struct ManagedProgress { pub struct ManagedProgress {