百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
Z

zbar-wasm

> 编程语言
开源

ZBar条形码阅读器的WebAssembly构建版

193 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

ZBar条形码阅读器的WebAssembly构建版

A WebAssembly build of the ZBar Bar Code Reader

This project was forked from ZBar.wasm, a WebAssembly build of the ZBar Bar Code Reader written in C/C++.

Features

  • Provided as minified ES module, CommonJS module and plain script
  • Runs in modern browsers, in Node.js and also in workers
  • Deployment size approx. 330 kByte
  • Supports Code-39, Code-93, Code-128, Codabar, Databar/Expanded, EAN/GTIN-5/8/13, ISBN-10/13, ISBN-13+2, ISBN-13+5, ITF (Interleaved 2 of 5), QR Code, UPC-A/E.
  • Detects multiple barcodes per frame, also with different types
  • Barcodes may be oriented horizontally or vertically
  • Scans ImageData and RGB/grayscale ArrayBuffer objects
  • Outperforms pure JavaScript barcode scanners

:warning: zbar-wasm versions 0.10 and above contain breaking changes with respect to version 0.9, please refer to section Bundling/deploying zbar-wasm.

Examples based on zbar-wasm

  • A simple example: on GitHub (source code), on CodePen

  • A polyfill for the BarcodeDetector Web API: on GitHub (source code with build scripts for Rollup and esbuild), on CodePen

Getting started

Using zbar-wasm as `


### Using zbar-wasm as plain `

  
    ⁝

Using zbar-wasm as an ESM or CommonJS module in Node.js

Installing:

bash
$ npm install @undecaf/[email protected]
    or
$ yarn add @undecaf/[email protected]

Using:

import ... from '@undecaf/zbar-wasm' pulls the ES module from the package, require('@undecaf/zbar-wasm') pulls the CommonJS module.

Please refer to the API documentation for what can be imported/required.

A simple ES module that scans a static image file:

javascript
import { createCanvas, loadImage }  from 'canvas';
import { scanImageData } from '@undecaf/zbar-wasm';

(async (url) => {
  const
          img = await loadImage(url),
          canvas = createCanvas(img.width, img.height),
          ctx = canvas.getContext('2d');

  ctx.drawImage(img, 0, 0)

  const
          imageData = ctx.getImageData(0, 0, img.width, img.height),
          symbols = await scanImageData(imageData);

  console.log(symbols[0]?.typeName, symbols[0]?.decode())
})('https://raw.githubusercontent.com/undecaf/zbar-wasm/master/tests/img/qr_code.png')

For a CommonJS module, just replace the first lines with

javascript
const { createCanvas, loadImage } = require('canvas');
const { scanImageData } = require('@undecaf/zbar-wasm');

Bundling/deploying zbar-wasm

Barcode scanning is always delegated to the WebAssembly code in file zbar.wasm. zbar-wasm provides various functionally equivalent ESM and CommonJS modules for Node.js and for browsers that differ in how zbar.wasm is to be provided at runtime:

  • zbar.wasm can be loaded from a CDN by browsers.
  • zbar.wasm can be bundled as an asset. That asset should be served to browsers as application/wasm so that it can be compiled in parallel with being received.
  • Several zbar-wasm modules contain zbar.wasm as inline data.

The following overview shows the modules that are available in zbar-wasm. One of them needs to be bundled in your application.

| Path in package | Module type | Node core modules polyfilled

(suitable for browsers) | zbar.wasm inlined | |:--------------------------|:-----------:|:-------------------------------------------------------:|:-------------------:| | /dist/index.mjs | ESM | :heavy_check_mark: | | | /dist/index.js | CommonJS | :heavy_check_mark: | | | /dist/main.mjs | ESM | | | | /dist/main.cjs | CommonJS | | | | /dist/inlined/index.mjs | ESM | :heavy_check_mark: | :heavy_check_mark: | | /dist/inlined/index.js | CommonJS | :heavy_check_mark: | :heavy_check_mark: | | /dist/inlined/main.mjs | ESM | | :heavy_check_mark: | | /dist/inlined/main.cjs | CommonJS | | :heavy_check_mark: |

The package entry points of zbar-wasm have been chosen so that bundlers will emit the appropriate module by default in most cases. However, zbar.wasm as inline data requires a suitable export condition in the bundler configuration, typically 'zbar-inlined'. Please refer to the exports section of package.json for details.

Building zbar-wasm includes testing the bundling process with Webpack, Rollup and esbuild and also testing the resulting bundles. The bundler configuration files tests/{webpack,rollup,esbuild}.config.js may be used as a reference of how to achieve a particular bundling result. Each of them covers the following combinations of platforms, module types and zbar.wasm provisioning for the respective bundler:

zbar.wasm Node module types Browser module types
loaded from CDN ESM, plain ``
bundled as asset ESM, CommonJS ESM
inlined in module ESM, CommonJS ESM, plain ``

Loading zbar.wasm from a custom location

As a last resort, if you cannot make your bundler place zbar.wasm where it can be located by the script, you can specify an URL or path for that WASM file at runtime:

…

API documentation

Owing to the predecessor of this project, samsam2310/zbar.wasm, a wiki and an extensive API Reference are already available. Many thanks to the author!

Please note that a few classes have been renamed compared to the documentation in order to avoid conflicts with built-in JavaScript class names:

  • Symbol → ZBarSymbol
  • Image → ZBarImage
  • ImageScanner → ZBarScanner

BarcodeDetector Web API

The BarcodeDetector polyfill package (in this repository, by the same author) is based on zbar-wasm but provides a standardized, higher-level and more flexible API.

Building zbar-wasm from source

Prerequisites:

  • A Linux platform
  • GNU make, tar and curl
  • Podman or Docker
  • Node.js v16+
  • At least one of the browsers supported by TestCafé

To build:

  • Clone this repository:
    bash
    $ git clone https://github.com/undecaf/zbar-wasm
    $ cd zbar-wasm
  • Enter your browser(s) in .testcaferc.json (supported browsers).
  • Enter two available port numbers in tests/src/ports.js.
  • If you prefer Docker over Podman as container engine then replace
    EM_ENGINE = $(EM_PODMAN)
    with
    EM_ENGINE = $(EM_DOCKER)
    in the provided Makefile.
  • Run the build process:
    bash
    $ make
    The make command runs emscripten in a container, compiling the C/C++ sources of the ZBar Bar Code Reader to WebAssembly. It also compiles and bundles the TypeScript glue code and runs the tests in Node.js and in the selected browser(s) on the host machine.

Credits to ...

  • samsam2310 for providing invaluable information in his zbar.wasm repository
  • mchehab for maintaining zbar
  • the emscripten folks for their compiler toolchain
  • the contributors to this package

License

Software: LGPL-2.1

Documentation: CC-BY-SA 4.0

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptbarcode-scanneremscriptenjavascript-libraryqrcode-scanner

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言