Skip to content

Commit 52678d3

Browse files
committed
feat: add initial filtered image support
1 parent 96540ce commit 52678d3

File tree

5 files changed

+40
-15
lines changed

5 files changed

+40
-15
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rxing-wasm"
3-
version = "0.2.5"
3+
version = "0.2.6"
44
edition = "2021"
55
description = "wasm bindings for rxing to provide commong barcode operations (decode/encode)"
66
repository = "https://github.com/rxing-core/rxing-wasm"
@@ -25,7 +25,7 @@ js-sys = "0.3.61"
2525
# code size when deploying.
2626
console_error_panic_hook = { version = "0.1.6", optional = true }
2727

28-
rxing = {version = "~0.5.7", default-features = false, features = ["wasm_support"]}
28+
rxing = {version = "~0.5.8", default-features = false, features = ["wasm_support"]}
2929
#rxing = {path="../rxing", version = "~0.2.23", default-features = false, features = ["wasm_support"]}
3030

3131
[dev-dependencies]

examples/webpack+js/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ <h1>RXing - Decode Barcodes</h1>
1616
<div id="DecodeHints">
1717
<label for="Other">Other:</label><input type="text" name="Other" id="Other" />
1818
<label for="PureBarcode">PureBarcode:</label><input type="checkbox" name="PureBarcode" id="PureBarcode" />
19+
<label for="FilterInput">FilterInput:</label><input type="checkbox" name="FilterInput" id="FilterInput" />
1920
<label for="PossibleFormats">PossibleFormats:</label><input type="text" name="PossibleFormats"
2021
id="PossibleFormats" />
2122
<label for="TryHarder">TryHarder:</label><input type="checkbox" name="TryHarder" id="TryHarder" checked="true" />
@@ -68,4 +69,4 @@ <h1>RXing - Decode Barcodes</h1>
6869
</html>
6970

7071
<!-- Copyright 2023 Henry Schimke -->
71-
<!-- Using rxing-wasm v0.2.5 -->
72+
<!-- Using rxing-wasm v0.2.6 -->

examples/webpack+js/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ function onClickScan() {
3030
const context = canvas.getContext('2d');
3131
const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
3232
const luma_data = convert_js_image_to_luma(imageData.data);
33+
const filter_image = document.getElementById("FilterInput").checked;
3334
const hints = getHints();
3435
let result;
3536
try {
36-
result = decode_barcode_with_hints(luma_data, canvas.width, canvas.height, hints);
37+
result = decode_barcode_with_hints(luma_data, canvas.width, canvas.height, hints, filter_image);
3738
} catch (e) {
3839
alert("Issue decoding: " + e);
3940
}

examples/webpack+js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
"webpack-dev-server": "^4.11.1"
2121
},
2222
"dependencies": {
23-
"rxing-wasm": "0.2.5"
23+
"rxing-wasm": "0.2.6"
2424
}
2525
}

src/lib.rs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ impl From<BarcodeFormat> for rxing::BarcodeFormat {
9898
BarcodeFormat::UnsuportedFormat => rxing::BarcodeFormat::UNSUPORTED_FORMAT,
9999
BarcodeFormat::Telepen => rxing::BarcodeFormat::TELEPEN,
100100
BarcodeFormat::RectangularMicroQR => rxing::BarcodeFormat::RECTANGULAR_MICRO_QR_CODE,
101-
102101
}
103102
}
104103
}
@@ -127,6 +126,7 @@ impl From<rxing::BarcodeFormat> for BarcodeFormat {
127126
rxing::BarcodeFormat::UNSUPORTED_FORMAT => BarcodeFormat::UnsuportedFormat,
128127
rxing::BarcodeFormat::TELEPEN => BarcodeFormat::Telepen,
129128
rxing::BarcodeFormat::RECTANGULAR_MICRO_QR_CODE => BarcodeFormat::RectangularMicroQR,
129+
_ => BarcodeFormat::UnsuportedFormat,
130130
}
131131
}
132132
}
@@ -301,6 +301,7 @@ pub fn decode_barcode(
301301
width: u32,
302302
height: u32,
303303
try_harder: Option<bool>,
304+
filter_image: Option<bool>,
304305
) -> Result<BarcodeResult, String> {
305306
let mut hints: rxing::DecodingHintDictionary = HashMap::new();
306307
if let Some(true) = try_harder {
@@ -309,8 +310,15 @@ pub fn decode_barcode(
309310
rxing::DecodeHintValue::TryHarder(true),
310311
);
311312
}
312-
let Ok(result) =
313-
rxing::helpers::detect_in_luma_with_hints(data, width, height, None, &mut hints)
313+
314+
let detection_function = if matches!(filter_image, Some(true)) {
315+
rxing::helpers::detect_in_luma_filtered_with_hints
316+
}else {
317+
rxing::helpers::detect_in_luma_with_hints
318+
};
319+
320+
let Ok(result) =
321+
detection_function(data, width, height, None, &mut hints)
314322
else {
315323
return Err("not found".to_owned());
316324
};
@@ -364,6 +372,7 @@ pub fn decode_barcode_rgb(
364372
rxing::DecodeHintValue::TryHarder(true),
365373
);
366374
}
375+
367376
let mut multi_format_reader = rxing::MultiFormatReader::default();
368377

369378
let Ok(result) = multi_format_reader.decode_with_hints(
@@ -389,14 +398,28 @@ pub fn decode_barcode_with_hints(
389398
width: u32,
390399
height: u32,
391400
hints: &mut decode_hints::DecodeHintDictionary,
401+
filter_image: Option<bool>,
392402
) -> Result<BarcodeResult, String> {
393-
let Ok(result) = rxing::helpers::detect_in_luma_with_hints(
394-
data,
395-
width,
396-
height,
397-
None,
398-
hints.get_dictionary_mut(),
399-
) else {
403+
404+
let results = if matches!(filter_image, Some(true)) {
405+
rxing::helpers::detect_in_luma_filtered_with_hints(
406+
data,
407+
width,
408+
height,
409+
None,
410+
hints.get_dictionary_mut(),
411+
)
412+
}else {
413+
rxing::helpers::detect_in_luma_with_hints(
414+
data,
415+
width,
416+
height,
417+
None,
418+
hints.get_dictionary_mut(),
419+
)
420+
};
421+
422+
let Ok(result) = results else {
400423
return Err("not found".to_owned());
401424
};
402425
Ok(result.into())

0 commit comments

Comments
 (0)