Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ members = [
resolver = "3"

[workspace.package]
version = "0.0.9"
version = "0.0.10"

[profile.release]
codegen-units = 1
Expand Down
3 changes: 1 addition & 2 deletions rxing-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ name = "rxing-cli"
path = "src/main.rs"

[dependencies]
rxing-reader = { path = "../rxing-reader" }
rxing-reader = { path = "../rxing-reader", features = ["serde"] }
anyhow = "1.0"
clap = { version = "4.5", features = ["derive"] }
image = { version = "0.25", default-features = false, features = ["png", "jpeg"] }
ureq = { version = "2.10", features = ["tls"] }
base64 = "0.22"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
37 changes: 11 additions & 26 deletions rxing-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use base64::engine::general_purpose::STANDARD as BASE64;
use base64::Engine;
use clap::{Parser, ValueEnum};
use image::ImageReader;
use rxing_reader::{decode_qr_codes_luma, rgba_to_luma};
use serde::Serialize;
use rxing_reader::json_view::{SymbolView, symbol_to_view};
use rxing_reader::{QrSymbol, decode_qr_codes_luma, rgba_to_luma};

const MAX_HTTP_BODY: u64 = 64 * 1024 * 1024;

Expand All @@ -33,13 +33,6 @@ enum Format {
Json,
}

#[derive(Serialize)]
#[serde(untagged)]
enum Entry {
Text { text: String },
BytesB64 { bytes_b64: String },
}

fn main() -> ExitCode {
match run() {
Ok(code) => code,
Expand All @@ -59,8 +52,8 @@ fn run() -> Result<ExitCode> {
Format::Text => 1,
Format::Json => 0,
};
let results = decode_payloads(&rgba, w, h, max)?;
render(&results, cli.format)
let symbols = decode_symbols(&rgba, w, h, max)?;
render(symbols, cli.format)
}

fn load_bytes(source: &str) -> Result<Vec<u8>> {
Expand Down Expand Up @@ -107,7 +100,7 @@ fn decode_image_bytes(bytes: &[u8]) -> Result<(Vec<u8>, usize, usize)> {
Ok((rgba.into_raw(), w, h))
}

fn decode_payloads(rgba: &[u8], w: usize, h: usize, max: usize) -> Result<Vec<Vec<u8>>> {
fn decode_symbols(rgba: &[u8], w: usize, h: usize, max: usize) -> Result<Vec<QrSymbol>> {
let luma = rgba_to_luma(rgba, w, h).map_err(anyhow::Error::msg)?;
let primary = decode_qr_codes_luma(&luma, w, h, true, true, true, max)?;
if !primary.is_empty() {
Expand All @@ -117,28 +110,20 @@ fn decode_payloads(rgba: &[u8], w: usize, h: usize, max: usize) -> Result<Vec<Ve
Ok(fallback)
}

fn render(results: &[Vec<u8>], format: Format) -> Result<ExitCode> {
fn render(symbols: Vec<QrSymbol>, format: Format) -> Result<ExitCode> {
match format {
Format::Text => match results.first() {
Format::Text => match symbols.into_iter().next() {
None => Ok(ExitCode::from(1)),
Some(bytes) => {
match std::str::from_utf8(bytes) {
Some(symbol) => {
match std::str::from_utf8(&symbol.bytes) {
Ok(s) => println!("{s}"),
Err(_) => println!("base64:{}", BASE64.encode(bytes)),
Err(_) => println!("base64:{}", BASE64.encode(&symbol.bytes)),
}
Ok(ExitCode::SUCCESS)
}
},
Format::Json => {
let entries: Vec<Entry> = results
.iter()
.map(|bytes| match std::str::from_utf8(bytes) {
Ok(s) => Entry::Text { text: s.to_string() },
Err(_) => Entry::BytesB64 {
bytes_b64: BASE64.encode(bytes),
},
})
.collect();
let entries: Vec<SymbolView> = symbols.into_iter().map(symbol_to_view).collect();
let mut stdout = io::stdout().lock();
serde_json::to_writer(&mut stdout, &entries).context("writing JSON to stdout")?;
stdout.write_all(b"\n").context("writing trailing newline")?;
Expand Down
6 changes: 6 additions & 0 deletions rxing-reader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ description = "Trimmed QR reading-only fork of rxing v0.9.0 for rxing-wasm."
license = "Apache-2.0"
edition = "2024"

[features]
default = []
serde = ["dep:serde", "dep:base64"]

[dependencies]
anyhow = "1.0"
bitvec = { version = "1", default-features = false, features = ["alloc"] }
Expand All @@ -15,6 +19,8 @@ chrono = { version = "0.4", default-features = false, features = ["clock", "wasm
multimap = "0.10"
num = "0.4"
thiserror = "2.0"
serde = { version = "1", features = ["derive"], optional = true }
base64 = { version = "0.22", optional = true }

[dev-dependencies]
image = { version = "0.25", default-features = false, features = ["png", "jpeg"] }
66 changes: 62 additions & 4 deletions rxing-reader/src/common/detect/decoder_result.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,89 @@
use std::sync::Arc;

use crate::common::ECIStringBuilder;
use crate::qrcode::{ErrorCorrectionLevel, Mode};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StructuredAppendInfo {
pub index: u8,
pub count: u8,
pub parity: u8,
}

#[derive(Debug, Default)]
pub struct DecoderResult {
content: ECIStringBuilder,
error: Option<Arc<anyhow::Error>>,
version: u32,
error_correction_level: Option<ErrorCorrectionLevel>,
mask: u8,
modes: Vec<Mode>,
structured_append: Option<StructuredAppendInfo>,
}

impl DecoderResult {
pub fn with_eci_string_builder(src: ECIStringBuilder) -> Self {
DecoderResult {
content: src,
error: None,
version: 0,
error_correction_level: None,
mask: 0,
modes: Vec::new(),
structured_append: None,
}
}

pub fn is_valid(&self) -> bool {
self.content.symbology.code != 0 && self.error.is_none()
}

pub fn into_bytes(self) -> Vec<u8> {
self.content.into_bytes()
}

pub fn with_error(mut self, error: Option<anyhow::Error>) -> DecoderResult {
self.error = error.map(Arc::new);
self
}

pub fn with_format(
mut self,
version: u32,
error_correction_level: ErrorCorrectionLevel,
mask: u8,
) -> DecoderResult {
self.version = version;
self.error_correction_level = Some(error_correction_level);
self.mask = mask;
self
}

pub fn set_modes(&mut self, modes: Vec<Mode>) {
self.modes = modes;
}

pub fn set_structured_append(&mut self, info: StructuredAppendInfo) {
self.structured_append = Some(info);
}

pub fn version(&self) -> u32 {
self.version
}

pub fn error_correction_level(&self) -> Option<ErrorCorrectionLevel> {
self.error_correction_level
}

pub fn mask(&self) -> u8 {
self.mask
}

pub fn modes(&self) -> &[Mode] {
&self.modes
}

pub fn structured_append(&self) -> Option<StructuredAppendInfo> {
self.structured_append
}

pub fn into_content(self) -> ECIStringBuilder {
self.content
}
}
Loading
Loading