Skip to main content

Crate libsane

Crate libsane 

Source
Expand description

Scan documents and images from Rust: safe, idiomatic bindings to SANE (Scanner Access Now Easy), the scanner API of Linux, the BSDs and macOS, for flatbed scanners, document feeders and multifunction printers.

use libsane::{Sane, well_known};

let sane = Sane::init()?;
for device in sane.devices(false)? {
    println!("{} ({} {}, {})", device.name, device.vendor, device.model, device.kind);
}

let mut device = sane.open("test:0")?;
device.set_value(well_known::MODE, well_known::MODE_GRAY)?;
device.set_value(well_known::RESOLUTION, 150)?;
let frame = device.scan()?;
println!("{} bytes, {:?}", frame.data.len(), frame.parameters);

§Overview

  • Sane initializes the library (once per process at a time) and finds and opens devices. Dropping it calls sane_exit.
  • Device reads and sets options with typed values, and starts scans.
  • Scan reads the image data in chunks (it implements std::io::Read) or whole, as a Frame: raw bytes and their Parameters. With the image feature, Frame::to_image converts it to an image::DynamicImage.
  • Every SANE_Status maps to a variant of Error.

§Blocking and threads

Every call blocks, sometimes for seconds (finding devices, warming up the lamp, scanning). From async code, run the whole SANE session on a blocking thread, for example with tokio::task::spawn_blocking (see Sane).

libsane is not thread-safe: a Sane can move to another thread, but cannot be shared between threads, and its devices stay on its thread.

§System requirements

libsane and its development files, found with pkg-config, and libclang (see the sane-sys crate). Without a scanner, SANE’s test backend provides virtual devices: enable it in dll.conf.

§Features

Re-exports§

pub use sane_sys as sys;

Modules§

well_known
The names of the well-known options and values of the SANE standard.

Structs§

Capabilities
The capabilities of an option (SANE_CAP_*).
Credentials
A username and password, returned by an AuthCallback.
Device
An open device, closed on drop (sane_close).
DeviceInfo
A device found by Sane::devices (SANE_Device).
Fixed
A SANE fixed-point number (SANE_Fixed): 16 integer bits, 16 fractional bits, from about -32768 to 32767.99998 in steps of 1/65536.
Frame
A complete scanned image: the raw bytes of every line, and the parameters that describe them.
OptionDescriptor
The description of an option of a device (SANE_Option_Descriptor).
Parameters
The parameters of a scan (SANE_Parameters).
Range
An inclusive range of values with an optional quantization step.
Sane
An initialized SANE library: the entry point of this crate.
Scan
A scan in progress, started by Device::start.
SetInfo
What changed after setting an option (SANE_INFO_*).
Version
The version of libsane (SANE_VERSION_CODE).

Enums§

Constraint
The values an option accepts (SANE_Constraint_Type).
Error
An error returned by SANE or by this crate.
FrameFormat
The format of a frame (SANE_Frame).
Unit
The physical unit of an option (SANE_Unit).
Value
The value of an option.
ValueType
The type of the value of an option (SANE_Value_Type).

Traits§

OptionId
Identifies an option of a device: its index (usize), its name (&str, String), or its descriptor.

Type Aliases§

AuthCallback
The function called when a backend needs credentials for a resource (for example a password-protected network scanner): receives the resource name, returns the credentials, or None to send none.
Result
A specialized Result for SANE operations.