Skip to content

Commit

Permalink
feat: add initial filtered image support
Browse files Browse the repository at this point in the history
  • Loading branch information
hschimke committed Mar 15, 2024
1 parent 96540ce commit 52678d3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 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.2.5"
version = "0.2.6"
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.5.7", default-features = false, features = ["wasm_support"]}
rxing = {version = "~0.5.8", default-features = false, features = ["wasm_support"]}
#rxing = {path="../rxing", version = "~0.2.23", default-features = false, features = ["wasm_support"]}

[dev-dependencies]
Expand Down
3 changes: 2 additions & 1 deletion examples/webpack+js/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ <h1>RXing - Decode Barcodes</h1>
<div id="DecodeHints">
<label for="Other">Other:</label><input type="text" name="Other" id="Other" />
<label for="PureBarcode">PureBarcode:</label><input type="checkbox" name="PureBarcode" id="PureBarcode" />
<label for="FilterInput">FilterInput:</label><input type="checkbox" name="FilterInput" id="FilterInput" />
<label for="PossibleFormats">PossibleFormats:</label><input type="text" name="PossibleFormats"
id="PossibleFormats" />
<label for="TryHarder">TryHarder:</label><input type="checkbox" name="TryHarder" id="TryHarder" checked="true" />
Expand Down Expand Up @@ -68,4 +69,4 @@ <h1>RXing - Decode Barcodes</h1>
</html>

<!-- Copyright 2023 Henry Schimke -->
<!-- Using rxing-wasm v0.2.5 -->
<!-- Using rxing-wasm v0.2.6 -->
3 changes: 2 additions & 1 deletion examples/webpack+js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ function onClickScan() {
const context = canvas.getContext('2d');
const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
const luma_data = convert_js_image_to_luma(imageData.data);
const filter_image = document.getElementById("FilterInput").checked;
const hints = getHints();
let result;
try {
result = decode_barcode_with_hints(luma_data, canvas.width, canvas.height, hints);
result = decode_barcode_with_hints(luma_data, canvas.width, canvas.height, hints, filter_image);
} catch (e) {
alert("Issue decoding: " + e);
}
Expand Down
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.2.5"
"rxing-wasm": "0.2.6"
}
}
43 changes: 33 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ impl From<BarcodeFormat> for rxing::BarcodeFormat {
BarcodeFormat::UnsuportedFormat => rxing::BarcodeFormat::UNSUPORTED_FORMAT,
BarcodeFormat::Telepen => rxing::BarcodeFormat::TELEPEN,
BarcodeFormat::RectangularMicroQR => rxing::BarcodeFormat::RECTANGULAR_MICRO_QR_CODE,

}
}
}
Expand Down Expand Up @@ -127,6 +126,7 @@ impl From<rxing::BarcodeFormat> for BarcodeFormat {
rxing::BarcodeFormat::UNSUPORTED_FORMAT => BarcodeFormat::UnsuportedFormat,
rxing::BarcodeFormat::TELEPEN => BarcodeFormat::Telepen,
rxing::BarcodeFormat::RECTANGULAR_MICRO_QR_CODE => BarcodeFormat::RectangularMicroQR,
_ => BarcodeFormat::UnsuportedFormat,
}
}
}
Expand Down Expand Up @@ -301,6 +301,7 @@ pub fn decode_barcode(
width: u32,
height: u32,
try_harder: Option<bool>,
filter_image: Option<bool>,
) -> Result<BarcodeResult, String> {
let mut hints: rxing::DecodingHintDictionary = HashMap::new();
if let Some(true) = try_harder {
Expand All @@ -309,8 +310,15 @@ pub fn decode_barcode(
rxing::DecodeHintValue::TryHarder(true),
);
}
let Ok(result) =
rxing::helpers::detect_in_luma_with_hints(data, width, height, None, &mut hints)

let detection_function = if matches!(filter_image, Some(true)) {
rxing::helpers::detect_in_luma_filtered_with_hints
}else {
rxing::helpers::detect_in_luma_with_hints
};

let Ok(result) =
detection_function(data, width, height, None, &mut hints)
else {
return Err("not found".to_owned());
};
Expand Down Expand Up @@ -364,6 +372,7 @@ pub fn decode_barcode_rgb(
rxing::DecodeHintValue::TryHarder(true),
);
}

let mut multi_format_reader = rxing::MultiFormatReader::default();

let Ok(result) = multi_format_reader.decode_with_hints(
Expand All @@ -389,14 +398,28 @@ pub fn decode_barcode_with_hints(
width: u32,
height: u32,
hints: &mut decode_hints::DecodeHintDictionary,
filter_image: Option<bool>,
) -> Result<BarcodeResult, String> {
let Ok(result) = rxing::helpers::detect_in_luma_with_hints(
data,
width,
height,
None,
hints.get_dictionary_mut(),
) else {

let results = if matches!(filter_image, Some(true)) {
rxing::helpers::detect_in_luma_filtered_with_hints(
data,
width,
height,
None,
hints.get_dictionary_mut(),
)
}else {
rxing::helpers::detect_in_luma_with_hints(
data,
width,
height,
None,
hints.get_dictionary_mut(),
)
};

let Ok(result) = results else {
return Err("not found".to_owned());
};
Ok(result.into())
Expand Down

0 comments on commit 52678d3

Please sign in to comment.