rxing-wasm 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,7 +1,30 @@
1
1
  # rxing-wasm
2
- WASM bindings for common rxing functions
2
+ WASM bindings for common rxing functions. The NPM link is [https://www.npmjs.com/package/rxing-wasm](https://www.npmjs.com/package/rxing-wasm) and the rust source is [https://github.com/hschimke/rxing-wasm](https://github.com/hschimke/rxing-wasm).
3
+
4
+ ## Data
5
+ The `convert_js_image_to_luma` function is used to convert canvas image data to the luma 8
6
+ format that rxing expects. An example might look like to below.
7
+
8
+ ```javascript
9
+ function decodeBarcode(canvas) {
10
+ let context = canvas.getContext('2d');
11
+ let height = canvas.height;
12
+ let width = canvas.width;
13
+ let imageData = context.getImageData(0, 0, width, height);
14
+
15
+ let data = imageData.data;
16
+ let luma8Data = convert_js_image_to_luma(data);
17
+ let parsedBarcode = decode_barcode(luma8Data, width, height);
18
+
19
+ return parsedBarcode;
20
+ }
21
+ ```
3
22
 
4
23
  ## Functions
24
+ ```rust
25
+ pub fn convert_js_image_to_luma(data: &[u8]) -> Vec<u8>;
26
+ ```
27
+
5
28
  ```rust
6
29
  pub fn encode_barcode(
7
30
  data: &str,
@@ -18,4 +41,13 @@ pub fn decode_barcode(
18
41
  height: u32,
19
42
  try_harder: Option<bool>,
20
43
  ) -> Result<BarcodeResult, String>;
44
+ ```
45
+
46
+ ```rust
47
+ pub fn decode_barcode_with_hints(
48
+ data: Vec<u8>,
49
+ width: u32,
50
+ height: u32,
51
+ hints: &mut decode_hints::DecodeHintDictionary,
52
+ ) -> Result<BarcodeResult, String>;
21
53
  ```
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rxing-wasm",
3
3
  "description": "wasm bindings for rxing to provide commong barcode operations (decode/encode)",
4
- "version": "0.1.2",
4
+ "version": "0.1.4",
5
5
  "license": "Apache 2.0",
6
6
  "repository": {
7
7
  "type": "git",
package/rxing_wasm.d.ts CHANGED
@@ -19,6 +19,17 @@ export function encode_barcode(data: string, width: number, height: number, bc_t
19
19
  */
20
20
  export function decode_barcode(data: Uint8Array, width: number, height: number, try_harder?: boolean): BarcodeResult;
21
21
  /**
22
+ * Convert a javascript image context's data into luma 8.
23
+ *
24
+ * Data for this function can be found from any canvas object
25
+ * using the `data` property of an `ImageData` object.
26
+ * Such an object could be obtained using the `getImageData`
27
+ * method of a `CanvasRenderingContext2D` object.
28
+ * @param {Uint8Array} data
29
+ * @returns {Uint8Array}
30
+ */
31
+ export function convert_js_image_to_luma(data: Uint8Array): Uint8Array;
32
+ /**
22
33
  * Decode a barcode from an array of rgba data.
23
34
  * Pixel data is in the form of:
24
35
  * Each pixel is one u32, [r,g,b].
@@ -30,6 +41,14 @@ export function decode_barcode(data: Uint8Array, width: number, height: number,
30
41
  */
31
42
  export function decode_barcode_rgb(data: Uint32Array, width: number, height: number, try_harder?: boolean): BarcodeResult;
32
43
  /**
44
+ * @param {Uint8Array} data
45
+ * @param {number} width
46
+ * @param {number} height
47
+ * @param {DecodeHintDictionary} hints
48
+ * @returns {BarcodeResult}
49
+ */
50
+ export function decode_barcode_with_hints(data: Uint8Array, width: number, height: number, hints: DecodeHintDictionary): BarcodeResult;
51
+ /**
33
52
  * Available barcode types
34
53
  */
35
54
  export enum BarcodeFormat {
@@ -105,6 +124,96 @@ export enum BarcodeFormat {
105
124
  }
106
125
  /**
107
126
  */
127
+ export enum DecodeHintTypes {
128
+ /**
129
+ *
130
+ * * Unspecified, application-specific hint. Maps to an unspecified {@link Object}.
131
+ *
132
+ */
133
+ Other,
134
+ /**
135
+ *
136
+ * * Image is a pure monochrome image of a barcode. Doesn't matter what it maps to;
137
+ * * use {@link Boolean#TRUE}.
138
+ *
139
+ */
140
+ PureBarcode,
141
+ /**
142
+ *
143
+ * * Image is known to be of one of a few possible formats.
144
+ * * Maps to a {@link List} of {@link BarcodeFormat}s.
145
+ *
146
+ */
147
+ PossibleFormats,
148
+ /**
149
+ *
150
+ * * Spend more time to try to find a barcode; optimize for accuracy, not speed.
151
+ * * Doesn't matter what it maps to; use {@link Boolean#TRUE}.
152
+ *
153
+ */
154
+ TryHarder,
155
+ /**
156
+ *
157
+ * * Specifies what character encoding to use when decoding, where applicable (type String)
158
+ *
159
+ */
160
+ CharacterSet,
161
+ /**
162
+ *
163
+ * * Allowed lengths of encoded data -- reject anything else. Maps to an {@code int[]}.
164
+ *
165
+ */
166
+ AllowedLengths,
167
+ /**
168
+ *
169
+ * * Assume Code 39 codes employ a check digit. Doesn't matter what it maps to;
170
+ * * use {@link Boolean#TRUE}.
171
+ *
172
+ */
173
+ AssumeCode39CheckDigit,
174
+ /**
175
+ *
176
+ * * Assume the barcode is being processed as a GS1 barcode, and modify behavior as needed.
177
+ * * For example this affects FNC1 handling for Code 128 (aka GS1-128). Doesn't matter what it maps to;
178
+ * * use {@link Boolean#TRUE}.
179
+ *
180
+ */
181
+ AssumeGs1,
182
+ /**
183
+ *
184
+ * * If true, return the start and end digits in a Codabar barcode instead of stripping them. They
185
+ * * are alpha, whereas the rest are numeric. By default, they are stripped, but this causes them
186
+ * * to not be. Doesn't matter what it maps to; use {@link Boolean#TRUE}.
187
+ *
188
+ */
189
+ ReturnCodabarStartEnd,
190
+ /**
191
+ *
192
+ * * The caller needs to be notified via callback when a possible {@link RXingResultPoint}
193
+ * * is found. Maps to a {@link RXingResultPointCallback}.
194
+ *
195
+ */
196
+ NeedResultPointCallback,
197
+ /**
198
+ *
199
+ * * Allowed extension lengths for EAN or UPC barcodes. Other formats will ignore this.
200
+ * * Maps to an {@code int[]} of the allowed extension lengths, for example [2], [5], or [2, 5].
201
+ * * If it is optional to have an extension, do not set this hint. If this is set,
202
+ * * and a UPC or EAN barcode is found but an extension is not, then no result will be returned
203
+ * * at all.
204
+ *
205
+ */
206
+ AllowedEanExtensions,
207
+ /**
208
+ *
209
+ * * If true, also tries to decode as inverted image. All configured decoders are simply called a
210
+ * * second time with an inverted image. Doesn't matter what it maps to; use {@link Boolean#TRUE}.
211
+ *
212
+ */
213
+ AlsoInverted,
214
+ }
215
+ /**
216
+ */
108
217
  export class BarcodeResult {
109
218
  free(): void;
110
219
  /**
@@ -133,3 +242,27 @@ export class BarcodeResult {
133
242
  */
134
243
  text(): string;
135
244
  }
245
+ /**
246
+ */
247
+ export class DecodeHintDictionary {
248
+ free(): void;
249
+ /**
250
+ */
251
+ constructor();
252
+ /**
253
+ * @param {number} hint
254
+ * @returns {string}
255
+ */
256
+ get_hint(hint: number): string;
257
+ /**
258
+ * @param {number} hint
259
+ * @param {string} value
260
+ * @returns {boolean}
261
+ */
262
+ set_hint(hint: number, value: string): boolean;
263
+ /**
264
+ * @param {number} hint
265
+ * @returns {boolean}
266
+ */
267
+ remove_hint(hint: number): boolean;
268
+ }
package/rxing_wasm_bg.js CHANGED
@@ -197,6 +197,32 @@ export function decode_barcode(data, width, height, try_harder) {
197
197
  }
198
198
  }
199
199
 
200
+ /**
201
+ * Convert a javascript image context's data into luma 8.
202
+ *
203
+ * Data for this function can be found from any canvas object
204
+ * using the `data` property of an `ImageData` object.
205
+ * Such an object could be obtained using the `getImageData`
206
+ * method of a `CanvasRenderingContext2D` object.
207
+ * @param {Uint8Array} data
208
+ * @returns {Uint8Array}
209
+ */
210
+ export function convert_js_image_to_luma(data) {
211
+ try {
212
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
213
+ const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
214
+ const len0 = WASM_VECTOR_LEN;
215
+ wasm.convert_js_image_to_luma(retptr, ptr0, len0);
216
+ var r0 = getInt32Memory0()[retptr / 4 + 0];
217
+ var r1 = getInt32Memory0()[retptr / 4 + 1];
218
+ var v1 = getArrayU8FromWasm0(r0, r1).slice();
219
+ wasm.__wbindgen_free(r0, r1 * 1);
220
+ return v1;
221
+ } finally {
222
+ wasm.__wbindgen_add_to_stack_pointer(16);
223
+ }
224
+ }
225
+
200
226
  let cachedUint32Memory0 = new Uint32Array();
201
227
 
202
228
  function getUint32Memory0() {
@@ -240,6 +266,38 @@ export function decode_barcode_rgb(data, width, height, try_harder) {
240
266
  }
241
267
  }
242
268
 
269
+ function _assertClass(instance, klass) {
270
+ if (!(instance instanceof klass)) {
271
+ throw new Error(`expected instance of ${klass.name}`);
272
+ }
273
+ return instance.ptr;
274
+ }
275
+ /**
276
+ * @param {Uint8Array} data
277
+ * @param {number} width
278
+ * @param {number} height
279
+ * @param {DecodeHintDictionary} hints
280
+ * @returns {BarcodeResult}
281
+ */
282
+ export function decode_barcode_with_hints(data, width, height, hints) {
283
+ try {
284
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
285
+ const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
286
+ const len0 = WASM_VECTOR_LEN;
287
+ _assertClass(hints, DecodeHintDictionary);
288
+ wasm.decode_barcode_with_hints(retptr, ptr0, len0, width, height, hints.ptr);
289
+ var r0 = getInt32Memory0()[retptr / 4 + 0];
290
+ var r1 = getInt32Memory0()[retptr / 4 + 1];
291
+ var r2 = getInt32Memory0()[retptr / 4 + 2];
292
+ if (r2) {
293
+ throw takeObject(r1);
294
+ }
295
+ return BarcodeResult.__wrap(r0);
296
+ } finally {
297
+ wasm.__wbindgen_add_to_stack_pointer(16);
298
+ }
299
+ }
300
+
243
301
  /**
244
302
  * Available barcode types
245
303
  */
@@ -314,6 +372,95 @@ UpcE:15,"15":"UpcE",
314
372
  UpcEanExtension:16,"16":"UpcEanExtension",UnsuportedFormat:17,"17":"UnsuportedFormat", });
315
373
  /**
316
374
  */
375
+ export const DecodeHintTypes = Object.freeze({
376
+ /**
377
+ *
378
+ * * Unspecified, application-specific hint. Maps to an unspecified {@link Object}.
379
+ *
380
+ */
381
+ Other:0,"0":"Other",
382
+ /**
383
+ *
384
+ * * Image is a pure monochrome image of a barcode. Doesn't matter what it maps to;
385
+ * * use {@link Boolean#TRUE}.
386
+ *
387
+ */
388
+ PureBarcode:1,"1":"PureBarcode",
389
+ /**
390
+ *
391
+ * * Image is known to be of one of a few possible formats.
392
+ * * Maps to a {@link List} of {@link BarcodeFormat}s.
393
+ *
394
+ */
395
+ PossibleFormats:2,"2":"PossibleFormats",
396
+ /**
397
+ *
398
+ * * Spend more time to try to find a barcode; optimize for accuracy, not speed.
399
+ * * Doesn't matter what it maps to; use {@link Boolean#TRUE}.
400
+ *
401
+ */
402
+ TryHarder:3,"3":"TryHarder",
403
+ /**
404
+ *
405
+ * * Specifies what character encoding to use when decoding, where applicable (type String)
406
+ *
407
+ */
408
+ CharacterSet:4,"4":"CharacterSet",
409
+ /**
410
+ *
411
+ * * Allowed lengths of encoded data -- reject anything else. Maps to an {@code int[]}.
412
+ *
413
+ */
414
+ AllowedLengths:5,"5":"AllowedLengths",
415
+ /**
416
+ *
417
+ * * Assume Code 39 codes employ a check digit. Doesn't matter what it maps to;
418
+ * * use {@link Boolean#TRUE}.
419
+ *
420
+ */
421
+ AssumeCode39CheckDigit:6,"6":"AssumeCode39CheckDigit",
422
+ /**
423
+ *
424
+ * * Assume the barcode is being processed as a GS1 barcode, and modify behavior as needed.
425
+ * * For example this affects FNC1 handling for Code 128 (aka GS1-128). Doesn't matter what it maps to;
426
+ * * use {@link Boolean#TRUE}.
427
+ *
428
+ */
429
+ AssumeGs1:7,"7":"AssumeGs1",
430
+ /**
431
+ *
432
+ * * If true, return the start and end digits in a Codabar barcode instead of stripping them. They
433
+ * * are alpha, whereas the rest are numeric. By default, they are stripped, but this causes them
434
+ * * to not be. Doesn't matter what it maps to; use {@link Boolean#TRUE}.
435
+ *
436
+ */
437
+ ReturnCodabarStartEnd:8,"8":"ReturnCodabarStartEnd",
438
+ /**
439
+ *
440
+ * * The caller needs to be notified via callback when a possible {@link RXingResultPoint}
441
+ * * is found. Maps to a {@link RXingResultPointCallback}.
442
+ *
443
+ */
444
+ NeedResultPointCallback:9,"9":"NeedResultPointCallback",
445
+ /**
446
+ *
447
+ * * Allowed extension lengths for EAN or UPC barcodes. Other formats will ignore this.
448
+ * * Maps to an {@code int[]} of the allowed extension lengths, for example [2], [5], or [2, 5].
449
+ * * If it is optional to have an extension, do not set this hint. If this is set,
450
+ * * and a UPC or EAN barcode is found but an extension is not, then no result will be returned
451
+ * * at all.
452
+ *
453
+ */
454
+ AllowedEanExtensions:10,"10":"AllowedEanExtensions",
455
+ /**
456
+ *
457
+ * * If true, also tries to decode as inverted image. All configured decoders are simply called a
458
+ * * second time with an inverted image. Doesn't matter what it maps to; use {@link Boolean#TRUE}.
459
+ *
460
+ */
461
+ AlsoInverted:11,"11":"AlsoInverted", });
462
+ /**
463
+ */
317
464
  export class BarcodeResult {
318
465
 
319
466
  static __wrap(ptr) {
@@ -404,6 +551,70 @@ export class BarcodeResult {
404
551
  }
405
552
  }
406
553
  }
554
+ /**
555
+ */
556
+ export class DecodeHintDictionary {
557
+
558
+ static __wrap(ptr) {
559
+ const obj = Object.create(DecodeHintDictionary.prototype);
560
+ obj.ptr = ptr;
561
+
562
+ return obj;
563
+ }
564
+
565
+ __destroy_into_raw() {
566
+ const ptr = this.ptr;
567
+ this.ptr = 0;
568
+
569
+ return ptr;
570
+ }
571
+
572
+ free() {
573
+ const ptr = this.__destroy_into_raw();
574
+ wasm.__wbg_decodehintdictionary_free(ptr);
575
+ }
576
+ /**
577
+ */
578
+ constructor() {
579
+ const ret = wasm.decodehintdictionary_new();
580
+ return DecodeHintDictionary.__wrap(ret);
581
+ }
582
+ /**
583
+ * @param {number} hint
584
+ * @returns {string}
585
+ */
586
+ get_hint(hint) {
587
+ try {
588
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
589
+ wasm.decodehintdictionary_get_hint(retptr, this.ptr, hint);
590
+ var r0 = getInt32Memory0()[retptr / 4 + 0];
591
+ var r1 = getInt32Memory0()[retptr / 4 + 1];
592
+ return getStringFromWasm0(r0, r1);
593
+ } finally {
594
+ wasm.__wbindgen_add_to_stack_pointer(16);
595
+ wasm.__wbindgen_free(r0, r1);
596
+ }
597
+ }
598
+ /**
599
+ * @param {number} hint
600
+ * @param {string} value
601
+ * @returns {boolean}
602
+ */
603
+ set_hint(hint, value) {
604
+ const ptr0 = passStringToWasm0(value, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
605
+ const len0 = WASM_VECTOR_LEN;
606
+ const ret = wasm.decodehintdictionary_set_hint(this.ptr, hint, ptr0, len0);
607
+ return ret !== 0;
608
+ }
609
+ /**
610
+ * @param {number} hint
611
+ * @returns {boolean}
612
+ */
613
+ remove_hint(hint) {
614
+ const ret = wasm.decodehintdictionary_remove_hint(this.ptr, hint);
615
+ return ret !== 0;
616
+ }
617
+ }
407
618
 
408
619
  export function __wbindgen_string_new(arg0, arg1) {
409
620
  const ret = getStringFromWasm0(arg0, arg1);
Binary file