Skip to content

Commit

Permalink
feat: move to rxing v.0.5 with telepen support
Browse files Browse the repository at this point in the history
  • Loading branch information
hschimke committed Jan 2, 2024
1 parent 3341234 commit a757634
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rxing-wasm"
version = "0.1.26"
version = "0.2.0"
edition = "2021"
description = "wasm bindings for rxing to provide commong barcode operations (decode/encode)"
repository = "https://github.com/rxing-core/rxing-wasm"
Expand All @@ -25,7 +25,7 @@ js-sys = "0.3.61"
# code size when deploying.
console_error_panic_hook = { version = "0.1.6", optional = true }

rxing = {version = "~0.4.11", default-features = false, features = ["wasm_support"]}
rxing = {version = "~0.5.0", default-features = false, features = ["wasm_support"]}
#rxing = {path="../rxing", version = "~0.2.23", default-features = false, features = ["wasm_support"]}

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion examples/webpack+js/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ <h1>RXing - Decode Barcodes</h1>
</html>

<!-- Copyright 2023 Henry Schimke -->
<!-- Using rxing-wasm v0.1.26 -->
<!-- Using rxing-wasm v0.2.0 -->
2 changes: 1 addition & 1 deletion examples/webpack+js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
"webpack-dev-server": "^4.11.1"
},
"dependencies": {
"rxing-wasm": "0.1.26"
"rxing-wasm": "0.2.0"
}
}
13 changes: 7 additions & 6 deletions src/decode_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl DecodeHintDictionary {
| rxing::DecodeHintValue::AssumeGs1(val)
| rxing::DecodeHintValue::ReturnCodabarStartEnd(val)
| rxing::DecodeHintValue::TryHarder(val)
| rxing::DecodeHintValue::TelepenAsNumeric(val)
| rxing::DecodeHintValue::AlsoInverted(val) => val.to_string(),

rxing::DecodeHintValue::PossibleFormats(val) => val
Expand Down Expand Up @@ -154,7 +155,7 @@ impl DecodeHintDictionary {
.insert(hint.into(), rxing::DecodeHintValue::Other(value));
}
DecodeHintTypes::PureBarcode => {
let Ok(pure_barcode) = value.parse() else {
let Ok(pure_barcode) = value.parse() else {
return false;
};
self.0.insert(
Expand All @@ -175,7 +176,7 @@ impl DecodeHintDictionary {
);
}
DecodeHintTypes::TryHarder => {
let Ok(try_harder) = value.parse() else {
let Ok(try_harder) = value.parse() else {
return false;
};
self.0
Expand All @@ -192,7 +193,7 @@ impl DecodeHintDictionary {
.insert(hint.into(), rxing::DecodeHintValue::AllowedLengths(lengths));
}
DecodeHintTypes::AssumeCode39CheckDigit => {
let Ok(assume_code_39_check_digit) = value.parse() else {
let Ok(assume_code_39_check_digit) = value.parse() else {
return false;
};
self.0.insert(
Expand All @@ -201,14 +202,14 @@ impl DecodeHintDictionary {
);
}
DecodeHintTypes::AssumeGs1 => {
let Ok(assume_gs1) = value.parse() else {
let Ok(assume_gs1) = value.parse() else {
return false;
};
self.0
.insert(hint.into(), rxing::DecodeHintValue::AssumeGs1(assume_gs1));
}
DecodeHintTypes::ReturnCodabarStartEnd => {
let Ok(return_codebar_start_end) = value.parse() else {
let Ok(return_codebar_start_end) = value.parse() else {
return false;
};
self.0.insert(
Expand All @@ -228,7 +229,7 @@ impl DecodeHintDictionary {
);
}
DecodeHintTypes::AlsoInverted => {
let Ok(also_inverted) = value.parse() else {
let Ok(also_inverted) = value.parse() else {
return false;
};
self.0.insert(
Expand Down
48 changes: 37 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ pub enum BarcodeFormat {

MicroQR,

Telepen,

///
UnsuportedFormat,
}
Expand All @@ -90,6 +92,7 @@ impl From<BarcodeFormat> for rxing::BarcodeFormat {
BarcodeFormat::UpcEanExtension => rxing::BarcodeFormat::UPC_EAN_EXTENSION,
BarcodeFormat::MicroQR => rxing::BarcodeFormat::MICRO_QR_CODE,
BarcodeFormat::UnsuportedFormat => rxing::BarcodeFormat::UNSUPORTED_FORMAT,
BarcodeFormat::Telepen => rxing::BarcodeFormat::TELEPEN,
}
}
}
Expand All @@ -116,6 +119,7 @@ impl From<rxing::BarcodeFormat> for BarcodeFormat {
rxing::BarcodeFormat::UPC_EAN_EXTENSION => BarcodeFormat::UpcEanExtension,
rxing::BarcodeFormat::MICRO_QR_CODE => BarcodeFormat::MicroQR,
rxing::BarcodeFormat::UNSUPORTED_FORMAT => BarcodeFormat::UnsuportedFormat,
rxing::BarcodeFormat::TELEPEN => BarcodeFormat::Telepen,
}
}
}
Expand Down Expand Up @@ -255,7 +259,9 @@ pub fn encode_barcode(
width as i32,
height as i32,
&std::collections::HashMap::new(),
) else { return Err("couldn't encode".to_owned()) };
) else {
return Err("couldn't encode".to_owned());
};
Ok(bit_matrix.to_string())
}

Expand All @@ -274,7 +280,9 @@ pub fn decode_barcode(
rxing::DecodeHintValue::TryHarder(true),
);
}
let Ok(result) = rxing::helpers::detect_in_luma_with_hints(data, width, height, None, &mut hints) else {
let Ok(result) =
rxing::helpers::detect_in_luma_with_hints(data, width, height, None, &mut hints)
else {
return Err("not found".to_owned());
};
Ok(result.into())
Expand All @@ -290,7 +298,7 @@ pub fn decode_barcode(
pub fn convert_js_image_to_luma(data: &[u8]) -> Vec<u8> {
let mut luma_data = Vec::new();
for src_pixel in data.chunks_exact(4) {
let [red,green,blue,alpha] = src_pixel else {
let [red, green, blue, alpha] = src_pixel else {
continue;
};
let pixel = if *alpha == 0 {
Expand Down Expand Up @@ -331,7 +339,11 @@ pub fn decode_barcode_rgb(

let Ok(result) = multi_format_reader.decode_with_hints(
&mut rxing::BinaryBitmap::new(rxing::common::HybridBinarizer::new(
rxing::RGBLuminanceSource::new_with_width_height_pixels( width as usize, height as usize,&data),
rxing::RGBLuminanceSource::new_with_width_height_pixels(
width as usize,
height as usize,
&data,
),
)),
&hints,
) else {
Expand All @@ -349,9 +361,15 @@ pub fn decode_barcode_with_hints(
height: u32,
hints: &mut decode_hints::DecodeHintDictionary,
) -> Result<BarcodeResult, String> {
let Ok(result) = rxing::helpers::detect_in_luma_with_hints(data, width, height, None, hints.get_dictionary_mut()) else {
return Err("not found".to_owned());
};
let Ok(result) = rxing::helpers::detect_in_luma_with_hints(
data,
width,
height,
None,
hints.get_dictionary_mut(),
) else {
return Err("not found".to_owned());
};
Ok(result.into())
}

Expand All @@ -365,13 +383,16 @@ pub struct MultiDecodeResult {
impl MultiDecodeResult {
#[wasm_bindgen(constructor)]
pub fn new() -> MultiDecodeResult {
MultiDecodeResult { pointer: 0, results: Vec::default() }
MultiDecodeResult {
pointer: 0,
results: Vec::default(),
}
}

fn with_results(results: Vec<BarcodeResult>) -> MultiDecodeResult {
MultiDecodeResult {
pointer: 0,
results
results,
}
}

Expand All @@ -391,10 +412,15 @@ pub fn decode_multi(
height: u32,
hints: &mut decode_hints::DecodeHintDictionary,
) -> Result<MultiDecodeResult, String> {
let Ok(results) = rxing::helpers::detect_multiple_in_luma_with_hints(data, width, height, hints.get_dictionary_mut()) else {
let Ok(results) = rxing::helpers::detect_multiple_in_luma_with_hints(
data,
width,
height,
hints.get_dictionary_mut(),
) else {
return Err("not found".to_owned());
};
Ok(MultiDecodeResult::with_results(
results.into_iter().map(|r| r.into()).collect()
results.into_iter().map(|r| r.into()).collect(),
))
}

0 comments on commit a757634

Please sign in to comment.