Skip to main content

Crate zedbar

Crate zedbar 

Source
Expand description

Zedbar Barcode Scanning Library

A pure Rust barcode scanning library supporting multiple barcode formats including QR codes, EAN, UPC, Code128, Code39, and more. Based on the zbar C library.

§Quick Start

use zedbar::{Image, Scanner};

// Load an image and convert it for scanning. [`Image::from_dynamic`]
// composites transparency over white; `to_luma8()` would drop the alpha
// channel and leave a barcode on a transparent background black on black.
let img = image::open("barcode.png").unwrap();
let mut img = Image::from_dynamic(&img).unwrap();

// Create a scanner
let mut scanner = Scanner::new();

// Scan for barcodes
let symbols = scanner.scan(&mut img);
for symbol in symbols {
    println!("{:?}: {}", symbol.symbol_type(), symbol.data_string().unwrap_or(""));
}

If you already hold grayscale pixels, Image::from_gray takes them directly.

§Configuration

Use the type-safe config module to customize decoder behavior:

use zedbar::config::*;
use zedbar::{DecoderConfig, Scanner};

let config = DecoderConfig::new()
    .enable(QrCode)
    .enable(Ean13)
    .set_length_limits(Code39, 4, 20)  // Code39 must be 4-20 chars
    .test_inverted(true)               // Try inverted image if no symbols found
    .retry_undecoded_regions(true)     // Crop+upscale small QR codes automatically
    .retry_downscaled(true)            // Re-scan large images at half/quarter size
    .retry_smoothed(true)              // Re-scan a smoothed copy of halftone scans
    .scan_density(2, 2);               // Scan every 2nd line (faster)

let mut scanner = Scanner::with_config(config);

§Small QR Codes

When a QR code is too small to decode (e.g. on a scanned page), ScanResult::finder_regions() reports where finder patterns were detected. Use Image::crop and Image::upscale to retry those regions, or enable retry_undecoded_regions to do this automatically.

§Photos of Screens

A photo of a QR code on a display carries the screen’s pixel grid as a stripe that defeats finder detection at full resolution. Use Image::downscale to average it out, or enable retry_downscaled to re-scan large images at half and quarter size automatically.

§Scans of Printed Codes

A scan or photocopy of a printed QR code renders each dark module as a halftone whose gaps split the finder patterns at full resolution. Use Image::smooth to close them, or enable retry_smoothed to re-scan a smoothed copy automatically.

§Supported Formats

  • 2D Codes: QR Code, SQCode
  • Linear Codes: EAN-13, EAN-8, EAN-2, EAN-5, UPC-A, UPC-E, ISBN-10, ISBN-13
  • Code Family: Code 128, Code 93, Code 39, Codabar
  • Industrial: Interleaved 2 of 5, DataBar (RSS), DataBar Expanded

§Modules

  • scanner - Main scanner API
  • image - Image handling
  • symbol - Decoded barcode symbols
  • config - Type-safe decoder configuration
  • error - Error types

Re-exports§

pub use config::DecoderConfig;
pub use error::Error;
pub use error::Result;
pub use image::Image;
pub use scanner::FinderRegion;
pub use scanner::ScanResult;
pub use scanner::Scanner;
pub use symbol::Bounds;
pub use symbol::Orientation;
pub use symbol::Point;
pub use symbol::SymbolType;

Modules§

config
Type-safe configuration system for Zedbar decoders
error
Error types and result handling
image
Image handling and format support
scanner
Image scanner for finding barcodes in 2D images
symbol
Decoded barcode symbols and types