rxing-core / rxing-wasm

WASM bindings for common rxing functions
https://www.npmjs.com/package/rxing-wasm
Apache License 2.0
29 stars 5 forks source link
aztec barcode barcode-reader barcode-scanner coda code128 code39 code98 datamatrix javascript maxicode pdf417 qrcode rust wasm webassembly

rxing-wasm

WASM bindings for common rxing functions. The NPM link is https://www.npmjs.com/package/rxing-wasm and the rust source is https://github.com/rxing-core/rxing-wasm.

Decode Multi Breaking Change with v0.3.0

Version 0.3.0 now returns BarcodeResult objects in a native javascript array. This fully deprecates the old method which returned a custom object with internal state.

Data

The convert_js_image_to_luma function is used to convert canvas image data to the luma 8 format that rxing expects. An example might look like to below.

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 data = imageData.data;
    let luma8Data = convert_js_image_to_luma(data);
    let parsedBarcode = decode_barcode(luma8Data, width, height);

    return parsedBarcode;
}

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.

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.

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.

Remove a hint using remove_hint(hint: DecodeHintTypes). The function returns true if the hint was present and removed, and false otherwise. The hint value is the same as in the add_hint function.

DecodeHintTypes Values

The following values are available for the DecodeHintTypes enum.

Result Metadata

Result metadata is now available through the get_result_metadata_name method of the BarcodeResult class. The returned result is a javascript Map object representing availble decoded metadata. The possible keys are:

It is important to note that not all values will be set for all results.

Functions

pub fn convert_js_image_to_luma(data: &[u8]) -> Vec<u8>;
pub fn encode_barcode(
    data: &str,
    width: u32,
    height: u32,
    bc_type: BarcodeFormat,
) -> Result<String, String>;
pub fn decode_barcode(
    data: Vec<u8>,
    width: u32,
    height: u32,
    try_harder: Option<bool>,
) -> Result<BarcodeResult, String>;
pub fn decode_barcode_with_hints(
    data: Vec<u8>,
    width: u32,
    height: u32,
    hints: &mut decode_hints::DecodeHintDictionary,
) -> Result<BarcodeResult, String>;
pub fn decode_multi(
    data: Vec<u8>,
    width: u32,
    height: u32,
    hints: &mut decode_hints::DecodeHintDictionary,
    filter_image: Option<bool>,
) -> Result<Vec<BarcodeResult>, String>;
pub fn encode_barcode_with_hints(
    data: &str,
    width: u32,
    height: u32,
    bc_type: BarcodeFormat,
    hints: &mut EncodeHintDictionary,
) -> Result<String, String>;

Beta Features

encode_barcode_with_hints is currently in alpha. The output and behaviour is unexpected and poorly documented. Use at your own risk, feature may change, unstable interface.