# 9.6.图像扫描仪

在 FreeBSD 中，图像扫描仪的访问通过 [SANE (Scanner Access Now Easy)](http://www.sane-project.org/) 提供，该软件在 FreeBSD Ports 中可用。

## 9.6.1. 检查扫描仪

在进行任何配置之前，检查扫描仪是否受到 SANE 支持非常重要。

将扫描仪连接好后，运行以下命令以获取所有连接的 USB 设备：

```sh
# usbconfig list
```

输出应类似于以下内容：

```sh
ugen4.2: <LITE-ON Technology USB NetVista Full Width Keyboard.> at usbus4, cfg=0 md=HOST spd=LOW (1.5Mbps) pwr=ON (70mA)
ugen4.3: <Logitech USB Optical Mouse> at usbus4, cfg=0 md=HOST spd=LOW (1.5Mbps) pwr=ON (100mA)
ugen3.2: <HP Deskjet 1050 J410 series> at usbus3, cfg=0 md=HOST spd=HIGH (480Mbps) pwr=ON (2mA)
```

运行以下命令获取 `idVendor` 和 `idProduct`：

```sh
# usbconfig -d 3.2 dump_device_desc
```

> **注意**
>
> 注意，扫描仪是即插即用设备，更改其连接的 USB 端口将更改 `usbconfig list` 输出中的内容。

输出应类似于以下内容：

```sh
ugen3.2: <HP Deskjet 1050 J410 series> at usbus3, cfg=0 md=HOST spd=HIGH (480Mbps) pwr=ON (2mA)

bLength = 0x0012
bDescriptorType = 0x0001
bcdUSB = 0x0200
bDeviceClass = 0x0000  <Probed by interface class>
bDeviceSubClass = 0x0000
bDeviceProtocol = 0x0000
bMaxPacketSize0 = 0x0040
idVendor = 0x03f0
idProduct = 0x8911
bcdDevice = 0x0100
iManufacturer = 0x0001  <HP>
iProduct = 0x0002  <Deskjet 1050 J410 series>
bNumConfigurations = 0x0001
```

若获得了 `idVendor` 和 `idProduct`，需要在 [SANE 支持的设备列表](http://www.sane-project.org/lists/sane-mfgs-cvs.html) 中检查扫描仪是否受支持，可以通过 `idProduct` 进行筛选。

## 9.6.2. SANE 配置

SANE 通过后端提供对扫描仪的访问。为了能够在 FreeBSD 上进行扫描，必须安装 [graphics/sane-backends](https://cgit.freebsd.org/ports/tree/graphics/sane-backends/) 包，可以通过运行以下命令进行安装：

```sh
# pkg install sane-backends
```

> **技巧**
>
> 某些 USB 扫描仪需要加载固件。例如，上面使用的 HP 扫描仪，需要安装 [print/hplip](https://cgit.freebsd.org/ports/tree/print/hplip/) 包。

安装完必要的包后，必须配置 [devd(8)](https://man.freebsd.org/cgi/man.cgi?query=devd\&sektion=8\&format=html) 以允许 FreeBSD 访问扫描仪。

将 `saned.conf` 文件添加到 /usr/local/etc/devd/saned.conf，内容如下：

```ini
notify 100 {
        match "system" "USB";
        match "subsystem" "INTERFACE";
        match "type" "ATTACH";
        match "cdev" "ugen[0-9].[0-9]";
        match "vendor" "0x03f0"; ①
        match "product" "0x8911"; ②
        action "chown -L cups:saned /dev/\$cdev && chmod -L 660 /dev/\$cdev";
};
```

* ① `vendor`: 是通过运行 `usbconfig -d 3.2 dump_device_desc` 命令获得的 `idVendor`。
* ② `product`: 是通过运行 `usbconfig -d 3.2 dump_device_desc` 命令获得的 `idProduct`。

之后，必须通过运行以下命令重新启动 [devd(8)](https://man.freebsd.org/cgi/man.cgi?query=devd\&sektion=8\&format=html)：

```sh
# service devd restart
```

SANE 后端包括 [scanimage(1)](https://man.freebsd.org/cgi/man.cgi?query=scanimage\&sektion=1\&format=html)，可以用来列出设备并执行图像采集。

执行 [scanimage(1)](https://man.freebsd.org/cgi/man.cgi?query=scanimage\&sektion=1\&format=html) 并使用 `-L` 参数列出扫描仪设备：

```sh
# scanimage -L
```

输出应类似于以下内容：

```sh
device `hpaio:/usb/Deskjet_1050_J410_series?serial=XXXXXXXXXXXXXX' is a Hewlett-Packard Deskjet_1050_J410_series all-in-one
```

如果 [scanimage(1)](https://man.freebsd.org/cgi/man.cgi?query=scanimage\&sektion=1\&format=html) 无法识别扫描仪，将出现以下消息：

```sh
No scanners were identified. If you were expecting something different,
check that the scanner is plugged in, turned on and detected by the
sane-find-scanner tool (if appropriate). Please read the documentation
which came with this software (README, FAQ, manpages).
```

待 [scanimage(1)](https://man.freebsd.org/cgi/man.cgi?query=scanimage\&sektion=1\&format=html) 能够识别扫描仪，配置就完成了，扫描仪现在可以使用。

要启用该服务并让它在启动时运行，执行以下命令：

```sh
# sysrc saned_enable=YES
```

虽然可以通过命令行使用 [scanimage(1)](https://man.freebsd.org/cgi/man.cgi?query=scanimage\&sektion=1\&format=html) 执行图像采集，但通常更倾向于使用图形界面进行图像扫描。

**表 6. 图形扫描程序**

| 名称                | 许可证     | 包                    |
| ----------------- | ------- | -------------------- |
| skanlite          | GPL 2.0 | graphics/skanlite    |
| GNOME Simple Scan | GPL 3.0 | graphics/simple-scan |
| XSANE             | GPL 2.0 | graphics/xsane       |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://handbook.bsdcn.org/di-9-zhang-duo-mei-ti/9.6.-tu-xiang-sao-miao-yi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
