From 52678d35554525cd1ee6d87d2b0dadef979ebd72 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Fri, 15 Mar 2024 13:31:43 -0500 Subject: [PATCH] feat: add initial filtered image support --- Cargo.toml | 4 +-- examples/webpack+js/index.html | 3 ++- examples/webpack+js/index.js | 3 ++- examples/webpack+js/package.json | 2 +- src/lib.rs | 43 ++++++++++++++++++++++++-------- 5 files changed, 40 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a476c88..924ee16 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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] diff --git a/examples/webpack+js/index.html b/examples/webpack+js/index.html index 8e6c74c..ae1c59a 100644 --- a/examples/webpack+js/index.html +++ b/examples/webpack+js/index.html @@ -16,6 +16,7 @@

RXing - Decode Barcodes

+ @@ -68,4 +69,4 @@

RXing - Decode Barcodes

- \ No newline at end of file + \ No newline at end of file diff --git a/examples/webpack+js/index.js b/examples/webpack+js/index.js index 4a259b9..ad44f14 100644 --- a/examples/webpack+js/index.js +++ b/examples/webpack+js/index.js @@ -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); } diff --git a/examples/webpack+js/package.json b/examples/webpack+js/package.json index ec9e505..d244a6f 100644 --- a/examples/webpack+js/package.json +++ b/examples/webpack+js/package.json @@ -20,6 +20,6 @@ "webpack-dev-server": "^4.11.1" }, "dependencies": { - "rxing-wasm": "0.2.5" + "rxing-wasm": "0.2.6" } } diff --git a/src/lib.rs b/src/lib.rs index 5907e44..a853a6d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -98,7 +98,6 @@ impl From for rxing::BarcodeFormat { BarcodeFormat::UnsuportedFormat => rxing::BarcodeFormat::UNSUPORTED_FORMAT, BarcodeFormat::Telepen => rxing::BarcodeFormat::TELEPEN, BarcodeFormat::RectangularMicroQR => rxing::BarcodeFormat::RECTANGULAR_MICRO_QR_CODE, - } } } @@ -127,6 +126,7 @@ impl From for BarcodeFormat { rxing::BarcodeFormat::UNSUPORTED_FORMAT => BarcodeFormat::UnsuportedFormat, rxing::BarcodeFormat::TELEPEN => BarcodeFormat::Telepen, rxing::BarcodeFormat::RECTANGULAR_MICRO_QR_CODE => BarcodeFormat::RectangularMicroQR, + _ => BarcodeFormat::UnsuportedFormat, } } } @@ -301,6 +301,7 @@ pub fn decode_barcode( width: u32, height: u32, try_harder: Option, + filter_image: Option, ) -> Result { let mut hints: rxing::DecodingHintDictionary = HashMap::new(); if let Some(true) = try_harder { @@ -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()); }; @@ -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( @@ -389,14 +398,28 @@ pub fn decode_barcode_with_hints( width: u32, height: u32, hints: &mut decode_hints::DecodeHintDictionary, + filter_image: Option, ) -> Result { - 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())