Socket
Socket
Sign inDemoInstall

@undecaf/zbar-wasm

Package Overview
Dependencies
0
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @undecaf/zbar-wasm

A WebAssembly build of the C/C++ ZBar barcode reader


Version published
Weekly downloads
5.4K
decreased by-38.49%
Maintainers
1
Install size
3.14 MB
Created
Weekly downloads
 

Readme

Source

A WebAssembly build of the ZBar Bar Code Reader

Open issues Vulnerabilities Total downloads Hits/month License

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

Features

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

Examples based on zbar-wasm

Getting started

Using zbar-wasm as <script type="module">

An example that scans a static image file:

<!DOCTYPE html>
<html>
<body>
  <img id="img" crossorigin="anonymous" src="https://raw.githubusercontent.com/undecaf/zbar-wasm/master/tests/img/qr_code.png">
  <pre id="result"></pre>

  <script type="module">
    import * as zbarWasm from 'https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm@0.10.1/dist/index.js'

    (async () => {
      const
        img = document.getElementById('img'),
        result = document.getElementById('result'),
        canvas = document.createElement('canvas'),
        context = canvas.getContext('2d');

      await img.decode()
      canvas.width = img.naturalWidth
      canvas.height = img.naturalHeight
      context.drawImage(img, 0, 0)

      const
        imageData = context.getImageData(0, 0, canvas.width, canvas.height),
        symbols = await zbarWasm.scanImageData(imageData);
      
      symbols.forEach(s => s.rawData = s.decode())
      result.innerText = JSON.stringify(symbols, null, 2)
    })()
  </script>
</body>
</html>

Using zbar-wasm as plain <script>

Almost identical to the snippet above, just replace the lines

<script type="module">
    import * as zbarWasm from 'https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm@0.10.1/dist/index.js'

with

<script src="https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm@0.10.1/dist/index.js"></script>
  <script>

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

Installing:

$ npm install @undecaf/zbar-wasm@0.10.1
    or
$ yarn add @undecaf/zbar-wasm@0.10.1

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:

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(ssymbols[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

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

Bundling/deploying zbar-wasm

zbar-wasm provides ESM and CommonJS modules for Node.js and browsers. One of them needs to be bundled in your application. In any case, barcode scanning is delegated to the WebAssembly code in file zbar.wasm. At runtime, this file can be provided in different ways depending on the bundling configuration and on which zbar-wasm module was emitted by the bundler:

  • It can be loaded from a CDN by browsers.
  • It 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.

This overview shows which modules are available in zbar-wasm:

Path in packageModule typeNode core modules polyfilled
(suitable for browsers)
zbar.wasm inlined
/dist/index.mjsESM:heavy_check_mark:
/dist/index.jsCommonJS:heavy_check_mark:
/dist/main.mjsESM
/dist/main.cjsCommonJS
/dist/inlined/index.mjsESM:heavy_check_mark::heavy_check_mark:
/dist/inlined/index.jsCommonJS:heavy_check_mark::heavy_check_mark:
/dist/inlined/main.mjsESM:heavy_check_mark:
/dist/inlined/main.cjsCommonJS: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 runs bundling tests with Webpack, Rollup and esbuild and also tests 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.wasmNode module typesBrowser module types
loaded from CDNESM, plain <script>
bundled as assetESM, CommonJSESM
inlined in moduleESM, CommonJSESM, plain <script>

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:

  • SymbolZBarSymbol
  • ImageZBarImage
  • ImageScannerZBarScanner

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:

To build:

  • Clone this repository:
    $ 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 Podman as container engine then replace
    EM_ENGINE = $(EM_DOCKER)
    
    with
    EM_ENGINE = $(EM_PODMAN)
    
    in the provided Makefile.
  • Run the build process:
    $ 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 ...

License

Software: LGPL-2.1

Documentation: CC-BY-SA 4.0

Keywords

FAQs

Last updated on 07 Sep 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc