Skip to content

Commit 37acfc4

Browse files
committed
v0.3.1
Adds the `convert_canvas_to_luma` and `convert_imagedata_to_luma` functions to simplify the task of getting data into the library.
1 parent fd0d43b commit 37acfc4

File tree

7 files changed

+1970
-965
lines changed

7 files changed

+1970
-965
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rxing-wasm"
3-
version = "0.3.0"
3+
version = "0.3.1"
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"
@@ -18,6 +18,7 @@ decode_hints = []
1818
[dependencies]
1919
wasm-bindgen = "0.2.92"
2020
js-sys = "0.3.69"
21+
web-sys = {version = "0.3.69", features = ["CanvasRenderingContext2d", "HtmlCanvasElement", "ImageData"]}
2122

2223
# The `console_error_panic_hook` crate provides better debugging of panics by
2324
# logging them with `console.error`. This is great for development, but requires

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,37 @@ function decodeBarcode(canvas) {
2424
}
2525
```
2626

27+
The `convert_canvas_to_luma` function is used to convert a canvas to the luma 8
28+
format that rxing expects. An example might look like to below.
29+
30+
```javascript
31+
function decodeBarcode(canvas) {
32+
let height = canvas.height;
33+
let width = canvas.width;
34+
let luma8Data = convert_canvas_to_luma(canvas);
35+
let parsedBarcode = decode_barcode(luma8Data, width, height);
36+
37+
return parsedBarcode;
38+
}
39+
```
40+
41+
The `convert_imagedata_to_luma` function is used to convert an ImageData object to the luma 8
42+
format that rxing expects. An example might look like to below.
43+
44+
```javascript
45+
function decodeBarcode(canvas) {
46+
let context = canvas.getContext('2d');
47+
let height = canvas.height;
48+
let width = canvas.width;
49+
let imageData = context.getImageData(0, 0, width, height);
50+
51+
let luma8Data = convert_imagedata_to_luma(imageData);
52+
let parsedBarcode = decode_barcode(luma8Data, width, height);
53+
54+
return parsedBarcode;
55+
}
56+
```
57+
2758
## Hints
2859
### Using the `DecodeHintDictionary` class
2960
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`.

examples/webpack+js/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,4 @@ <h1>RXing - Decode Barcodes</h1>
106106
</html>
107107

108108
<!-- Copyright 2023 Henry Schimke -->
109-
<!-- Using rxing-wasm v0.3.0 -->
109+
<!-- Using rxing-wasm v0.3.1 -->

examples/webpack+js/index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { convert_js_image_to_luma, decode_barcode_with_hints, decode_multi, DecodeHintDictionary, DecodeHintTypes, BarcodeFormat } from "rxing-wasm";
1+
import { convert_js_image_to_luma, convert_canvas_to_luma, decode_barcode_with_hints, decode_multi, DecodeHintDictionary, DecodeHintTypes, BarcodeFormat } from "rxing-wasm";
22

33
const text_hints = ["Other", "PossibleFormats", "CharacterSet", "AllowedLengths", "AllowedEanExtensions"];
44
const bool_hints = ["PureBarcode", "TryHarder", "AssumeCode39CheckDigit", "ReturnCodabarStartEnd", "AssumeGs1", "AlsoInverted", "TelepenAsNumeric"]
@@ -32,16 +32,24 @@ function handleFiles(e) {
3232
function onClickScan() {
3333
output.innerHTML = '';
3434
const canvas = document.getElementById('cvs');
35-
const context = canvas.getContext('2d');
36-
const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
37-
const luma_data = convert_js_image_to_luma(imageData.data);
35+
// const context = canvas.getContext('2d');
36+
// const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
37+
// const luma_data = convert_js_image_to_luma(imageData.data);
3838
const filter_image = document.getElementById("FilterInput").checked;
39+
let luma_data;
40+
try {
41+
luma_data = convert_canvas_to_luma(canvas);
42+
} catch (e) {
43+
alert("Issue decoding: " + e);
44+
return;
45+
}
3946
const hints = getHints();
4047
let result;
4148
try {
4249
result = decode_barcode_with_hints(luma_data, canvas.width, canvas.height, hints, filter_image);
4350
} catch (e) {
4451
alert("Issue decoding: " + e);
52+
return;
4553
}
4654
write_results(result.format(), result.text(), result.raw_bytes(), result.result_points(), result.get_meta_data());
4755

0 commit comments

Comments
 (0)