@deviceflow/rxing-wasm

中文文档
0.5.1发布于 2025/08/24Apache 2.0TypeScriptES Module零依赖近 30 天下载 2597

wasm bindings for rxing to provide commong barcode operations (decode/encode)

原始使用文档
文档目录8 节
  1. Decode Multi Breaking Change with v0.3.0
  2. Data
  3. Hints
  4. Using the DecodeHintDictionary class
  5. DecodeHintTypes Values
  6. Result Metadata
  7. Functions
  8. Beta Features

rxing-wasm

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

This fork is identical to the upstream package, except wasm-pack is invoked with the --web flag.

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.

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

版本与发布记录

@deviceflow/rxing-wasm 共发布 2 个版本,首次发布于 2024/11/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.5.1latest2025/08/241.2 年前
0.3.12024/11/202 年前

关于 @deviceflow/rxing-wasm 的常见问题

@deviceflow/rxing-wasm 是什么?

wasm bindings for rxing to provide commong barcode operations (decode/encode)

如何安装 @deviceflow/rxing-wasm?

在项目目录运行 npm install @deviceflow/rxing-wasm,或使用 pnpm add / yarn add / bun add。

@deviceflow/rxing-wasm 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@deviceflow/rxing-wasm 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@deviceflow/rxing-wasm 可以商用吗?

@deviceflow/rxing-wasm 使用 Apache 2.0 许可。声明的许可为 Apache 2.0,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@deviceflow/rxing-wasm 的下载量有多少?

根据 npm 官方下载统计,@deviceflow/rxing-wasm 在 2026/10/12 – 2026/11/10 这 30 天内被下载 2,597 次,日均约 87 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@deviceflow/rxing-wasm 还在维护吗?

最新版本 0.5.1 发布于 2025/08/24(1.2 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/11/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。