Skip to content

Commit

Permalink
v0.3.1
Browse files Browse the repository at this point in the history
Adds the `convert_canvas_to_luma` and `convert_imagedata_to_luma` functions to simplify the task of getting data into the library.
  • Loading branch information
hschimke committed Aug 5, 2024
1 parent fd0d43b commit 37acfc4
Show file tree
Hide file tree
Showing 7 changed files with 1,970 additions and 965 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rxing-wasm"
version = "0.3.0"
version = "0.3.1"
edition = "2021"
description = "wasm bindings for rxing to provide commong barcode operations (decode/encode)"
repository = "https://github.com/rxing-core/rxing-wasm"
Expand All @@ -18,6 +18,7 @@ decode_hints = []
[dependencies]
wasm-bindgen = "0.2.92"
js-sys = "0.3.69"
web-sys = {version = "0.3.69", features = ["CanvasRenderingContext2d", "HtmlCanvasElement", "ImageData"]}

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,37 @@ function decodeBarcode(canvas) {
}
```

The `convert_canvas_to_luma` function is used to convert a canvas to the luma 8
format that rxing expects. An example might look like to below.

```javascript
function decodeBarcode(canvas) {
let height = canvas.height;
let width = canvas.width;
let luma8Data = convert_canvas_to_luma(canvas);
let parsedBarcode = decode_barcode(luma8Data, width, height);

return parsedBarcode;
}
```

The `convert_imagedata_to_luma` function is used to convert an ImageData object to the luma 8
format that rxing expects. An example might look like to below.

```javascript
function decodeBarcode(canvas) {
let context = canvas.getContext('2d');
let height = canvas.height;
let width = canvas.width;
let imageData = context.getImageData(0, 0, width, height);

let luma8Data = convert_imagedata_to_luma(imageData);
let parsedBarcode = decode_barcode(luma8Data, width, height);

return parsedBarcode;
}
```

## Hints
### Using the `DecodeHintDictionary` class
Add a hint with `set_hint(hint: DecodeHintTypes, value: string)`. The function returns `true` if the hint was added and `false` if it was not. The value of hint must be a `number` representing on of the enum values for `DecodeHintTypes`. The easiest way to use this is to simply pass in one of the values from `DecodeHintTypes`.
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 @@ -106,4 +106,4 @@ <h1>RXing - Decode Barcodes</h1>
</html>

<!-- Copyright 2023 Henry Schimke -->
<!-- Using rxing-wasm v0.3.0 -->
<!-- Using rxing-wasm v0.3.1 -->
16 changes: 12 additions & 4 deletions examples/webpack+js/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { convert_js_image_to_luma, decode_barcode_with_hints, decode_multi, DecodeHintDictionary, DecodeHintTypes, BarcodeFormat } from "rxing-wasm";
import { convert_js_image_to_luma, convert_canvas_to_luma, decode_barcode_with_hints, decode_multi, DecodeHintDictionary, DecodeHintTypes, BarcodeFormat } from "rxing-wasm";

const text_hints = ["Other", "PossibleFormats", "CharacterSet", "AllowedLengths", "AllowedEanExtensions"];
const bool_hints = ["PureBarcode", "TryHarder", "AssumeCode39CheckDigit", "ReturnCodabarStartEnd", "AssumeGs1", "AlsoInverted", "TelepenAsNumeric"]
Expand Down Expand Up @@ -32,16 +32,24 @@ function handleFiles(e) {
function onClickScan() {
output.innerHTML = '';
const canvas = document.getElementById('cvs');
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 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;
let luma_data;
try {
luma_data = convert_canvas_to_luma(canvas);
} catch (e) {
alert("Issue decoding: " + e);
return;
}
const hints = getHints();
let result;
try {
result = decode_barcode_with_hints(luma_data, canvas.width, canvas.height, hints, filter_image);
} catch (e) {
alert("Issue decoding: " + e);
return;
}
write_results(result.format(), result.text(), result.raw_bytes(), result.result_points(), result.get_meta_data());

Expand Down
Loading

0 comments on commit 37acfc4

Please sign in to comment.