Code Monkey home page Code Monkey logo

rxing-wasm's Introduction

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.

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;
}

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.

  • Other: Unspecified, application-specific hint. This can be any string, only some decoders care about this value.
  • PureBarcode: Image is a pure monochrome image of a barcode. Should be a string with either "true" or "false".
  • PossibleFormats: A comma separated list (no spaces between elements), with the names of barcode formats to search for. For example, looking for a QrCode and a Datamatrix, one would pass in the string "qrcode,datamatrix".
  • TryHarder: Spend more time to try to find a barcode; optimize for accuracy, not speed. String with either "true" or "false".
  • CharacterSet: Specifies what character encoding to use when decoding, where applicable. This should be a string mapping to a character encoding.
  • AllowedLengths: Allowed lengths of encoded data -- reject anything else. A comma separated list of integers.
  • AssumeCode39CheckDigit: Assume Code 39 codes employ a check digit. A string with either "true" or "false".
  • AssumeGs1: Assume the barcode is being processed as a GS1 barcode, and modify behavior as needed. For example this affects FNC1 handling for Code 128 (aka GS1-128). A string with either "true" or "false".
  • ReturnCodabarStartEnd: If true, return the start and end digits in a Codabar barcode instead of stripping them. They are alpha, whereas the rest are numeric. By default, they are stripped, but this causes them to not be. A string with either "true" or "false".
  • NeedResultPointCallback: The caller needs to be notified via callback when a possible ResultPoint is found. Currently unsupported. In future will map to a (x: number, y: number) = {} function.
  • AllowedEanExtensions: Allowed extension lengths for EAN or UPC barcodes. Other formats will ignore this. A comma separated list of the allowed extension lengths, for example "2", "5" or "2,5". If it is optional to have an extension, do not set this hint. If this is set, and a UPC or EAN barcode is found but an extension is not, then no result will be returned at all.
  • AlsoInverted: If true, also tries to decode as inverted image. All configured decoders are simply called a second time with an inverted image. A string with either "true" or "false".

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:

  • OTHER
  • Orientation
  • Byte_Segments
  • Error_Correction_Level
  • Issue_Number
  • Suggested_Price
  • Possible_Country
  • UPC/EAN_Extension
  • PDF417_Extra_MetaData
  • Structured_Append_Sequence
  • Structured_Append_Parity
  • Symbology_Identifier
  • Is_Mirrored
  • Content_Type

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>;

rxing-wasm's People

Contributors

hschimke avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.