From 270ff055b9874c618575ad33a2ef45ae3d7e7f59 Mon Sep 17 00:00:00 2001 From: Eero Kelly Date: Wed, 18 Feb 2026 21:28:20 +0000 Subject: [PATCH] Lower QR error correction if needed when creating When generating large QR codes, try lowering error correction to be able to store more data. --- src/offline.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/offline.rs b/src/offline.rs index 7e4ec8e..16f7c1b 100644 --- a/src/offline.rs +++ b/src/offline.rs @@ -69,7 +69,7 @@ pub fn output_message(json: String, format: &OfflineOutput) -> Result<()> { Engine, }; use libflate::gzip; - use qrcode::{render::unicode, QrCode}; + use qrcode::{render::unicode, types::QrError, EcLevel, QrCode}; use std::io::Write; eprintln!("json length: {}", json.len()); let mut encoder = gzip::Encoder::new(Vec::new())?; @@ -86,7 +86,14 @@ pub fn output_message(json: String, format: &OfflineOutput) -> Result<()> { OfflineOutput::Ascii(url) | OfflineOutput::Png(url) => url.to_owned() + &base64, _ => base64, }; - let code = QrCode::new(msg)?; + let code = QrCode::new(&msg).or_else(|e| { + // Try to lower the error correction level to make the data fit. + if let QrError::DataTooLong = e { + QrCode::with_error_correction_level(&msg, EcLevel::L) + } else { + Err(e) + } + })?; match format { OfflineOutput::Ascii(_) | OfflineOutput::AsciiNoUrl => { let img = code.render::().build();