libsane
Scan documents and images from Rust. Safe, idiomatic bindings to
SANE (Scanner Access Now Easy), the scanner
API of Linux, the BSDs and macOS: find flatbed scanners, document feeders and
multifunction printers, set the resolution, mode and scan area, and get the
page as raw bytes or as an image ready to
save or to feed to OCR.
use ;
let sane = init?;
for device in sane.devices?
let mut scanner = sane.open?;
scanner.set_value?;
scanner.set_value?; // dpi
scanner.scan?.to_image?.save?; // `image` feature
Features
- Every SANE scanner: USB and network scanners and multifunction printers
supported by sane-backends, including driverless eSCL/AirScan devices with
sane-airscan. - Safe by construction: one initialization per process, devices closed
before
sane_exit, buffers sized from the option descriptors;unsafecode is confined to one module of small, documented functions. - Typed options: bool, int, fixed-point, string and button options, with
constraints (range, word list, string list) and capabilities; the option
list reloads itself on
SANE_INFO_RELOAD_OPTIONS. - Every way to read a scan: chunks through
std::io::Read, a wholeFrameof raw bytes and parameters, or animage::DynamicImage(imagefeature). Gray, RGB, 1/8/16-bit, three-pass and hand scanners, and document feeders page after page. - Typed errors: every
SANE_Statusmaps to anErrorvariant (Jammed,NoDocs,CoverOpen...); no panic crosses the FFI boundary.
The API is blocking: from async code, run the SANE session on a blocking
thread, for example with tokio::task::spawn_blocking.
System requirements
libsane and its development files, pkg-config and libclang:
| System | Command |
|---|---|
| Debian, Ubuntu | sudo apt install libsane-dev pkg-config libclang-dev |
| Fedora | sudo dnf install sane-backends-devel pkgconf-pkg-config clang-devel |
| Arch Linux | sudo pacman -S sane pkgconf clang |
| macOS (Homebrew) | brew install sane-backends pkg-config llvm |
No scanner at hand? SANE's test backend provides virtual devices
(test:0, test:1): uncomment test in /etc/sane.d/dll.conf, or point
SANE_CONFIG_DIR to a directory whose dll.conf lists it.
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.