| Crates.io | wascan |
| lib.rs | wascan |
| version | 0.1.10 |
| created_at | 2025-11-24 19:10:42.615939+00 |
| updated_at | 2026-01-11 10:42:00.76366+00 |
| description | Production-ready barcode and QR code scanner for web browsers. Handles camera access, streaming, and file input automatically. |
| homepage | |
| repository | https://github.com/intelligent-medical-cloud/pkg-wascan |
| max_upload_size | |
| id | 1948431 |
| size | 103,115 |
A production-ready, plug-and-play WebAssembly barcode scanning library for the browser, built with Rust and wasm-bindgen. wascan handles all the complexity of camera access, streaming, and file input automatically - just integrate and start scanning.
npm install wascan
Or with yarn:
yarn add wascan
Or with pnpm:
pnpm add wascan
Add this to your Cargo.toml:
[dependencies]
wascan = "0.1.10"
For JavaScript/TypeScript projects using npm, the package is ready to use. For Rust projects or custom builds, see the Build section below.
import init, {
init_reader,
init_scanner,
read_from_image,
start_stream_scan,
stop_stream_scan,
on_detect,
on_start,
on_stop,
} from "wascan";
// Initialize the WASM module (automatic path resolution)
await init();
// Initialize the reader and scanner modules
init_reader();
init_scanner();
// Set up event callbacks
on_start(() => {
console.log("Scanning started");
});
on_detect((result) => {
if (result.success) {
console.log("Detected:", result.value);
} else {
console.error("Error:", result.error);
}
});
on_stop(() => {
console.log("Scanning stopped");
});
// Start scanning from camera stream
start_stream_scan("video-element-id");
// Or trigger file input dialog to scan from image
read_from_image();
// Stop scanning programmatically
stop_stream_scan();
When using bundlers like Vite or Webpack, you may need to explicitly specify the WASM file path.
import init, {
init_reader,
init_scanner,
read_from_image,
start_stream_scan,
stop_stream_scan,
on_detect,
on_start,
on_stop,
} from "wascan";
// For Vite: Import WASM as URL
import wasmUrl from "wascan/wascan_bg.wasm?url";
// For other bundlers: Construct URL explicitly
const wasmUrl = new URL("wascan/wascan_bg.wasm", import.meta.url);
// Initialize with explicit WASM path (object format)
await init({ module_or_path: wasmUrl });
// ... rest of initialization code
import { onMounted, onBeforeUnmount } from "vue";
import init, {
init_reader,
init_scanner,
start_stream_scan,
stop_stream_scan,
on_detect,
on_start,
on_stop,
} from "wascan";
// For Vite
import wasmUrl from "wascan/wascan_bg.wasm?url";
// For other bundlers
// const wasmUrl = new URL("wascan/wascan_bg.wasm", import.meta.url);
onMounted(async () => {
try {
await init({ module_or_path: wasmUrl });
init_reader();
init_scanner();
on_start(() => console.log("Scanning started"));
on_detect((result) => {
if (result.success) {
console.log("Detected:", result.value);
}
});
on_stop(() => console.log("Scanning stopped"));
start_stream_scan("video-element-id");
} catch (error) {
console.error("Failed to initialize:", error);
}
});
onBeforeUnmount(() => {
stop_stream_scan();
});
init(module_or_path?) - Initializes the WASM module
{ module_or_path: string | URL } - Explicit path to WASM filewascan_bg.wasm relative to the moduleinit_reader() - Initializes the reader module (required before using read_from_image)init_scanner() - Initializes the scanner module (required before using start_stream_scan)start_stream_scan(video_element_id: &str) - Starts barcode scanning from camera streamread_from_image() - Triggers file input dialog to scan from an image filestop_stream_scan() - Stops the stream scanningon_start(callback: Function) - Register callback for when scanning startson_detect(callback: Function) - Register callback for barcode detection
{ success: boolean, value?: string, error?: string }on_stop(callback: Function) - Register callback for when scanning stopswascan is built on top of the Rxing library (Rust port of ZXing), which supports a wide range of barcode formats. However, the built binary only includes UPC-A and QR Code to keep the WebAssembly bundle size minimal.
The underlying Rxing library supports the following formats:
1D Product Barcodes:
1D Industrial Barcodes:
2D Matrix Barcodes:
The pre-built wascan binary includes only:
You can extend wascan to support additional barcode formats in two ways:
Add specific readers explicitly - Modify src/detector.rs to include additional readers from the Rxing library (e.g., EAN13Reader, Code128Reader, DataMatrixReader, etc.)
Use MultiReader - Replace the individual readers with rxing::MultiReader to support all formats at once
⚠️ Important: Including all barcode formats will approximately double the WASM bundle size. It's recommended to include only the formats required for your specific use case to keep the bundle size minimal.
Example of adding a specific reader:
use rxing::oned::EAN13Reader;
// Add EAN-13 detection alongside UPC-A and QR
let ean13_result = {
let src = Luma8LuminanceSource::new(cropped.clone(), crop_w, crop_h);
let binarizer = HybridBinarizer::new(src);
let mut bitmap = BinaryBitmap::new(binarizer);
let mut reader = EAN13Reader::default();
reader.decode(&mut bitmap)
};
wascan delivers excellent performance characteristics:
Benchmarks are not included in this repository, but users are welcome to measure performance on their own hardware and use cases. The WebAssembly implementation provides near-native performance while maintaining cross-platform compatibility.
This crate is designed to be compiled to WebAssembly. Use the provided Makefile:
make build
This will generate ready to use WebAssembly files in the pkg/ directory.
A working demo is included in the demo/ directory. To run it:
make demo
http://localhost:8000/demoNote: The demo uses the local pkg/ directory. For npm package usage examples, see the Usage section above.
wascan works seamlessly across all modern browsers and platforms:
No platform-specific code needed - the library handles all browser differences automatically.
wasm-bindgen-cli and wasm32-unknown-unknown for building WebAssembly packages (install via make init)Licensed under the Apache License, Version 2.0. See LICENSE-APACHE for details.
This project builds upon the open-source Rxing project, which is also licensed under Apache 2.0.