使用适用于 Windows 和 Linux 的 Web 应用程序扫描图像

博客类别:TWAINWeb

2024/04/08

TWAIN 是一种标准协议和接口 (API),用于定义程序和图像扫描仪之间的交互。TWAIN 标准由 TWAIN 工作组开发。TWAIN 标准版本 1 定义了与 Windows 系统下图像扫描仪的交互。TWAIN 标准版本 2 定义了与 Windows、macOS 和 Linux 系统下图像扫描仪的交互。
TWAIN标准是Windows系统下图像扫描仪最流行的标准。几乎所有图像扫描仪都有适用于Windows系统的TWAIN驱动程序。
TWAIN 标准在 Linux 中并不流行,我们的研究(截至 2024 年初)表明,只有柯达公司提供 TWAIN 驱动程序,以便在 Linux 中使用其图像扫描仪。

SANE 是一个应用程序编程接口 (API),它为光栅图像扫描设备(平板扫描仪、手持扫描仪等)提供标准化的访问方式。SANE API 属于公共领域,欢迎公众参与讨论和开发。SANE API 是 Linux 系统上最流行的图像扫描仪 API。
许多扫描仪制造商都为其图像扫描仪开发了 SANE 驱动程序。此外,SANE 社区也创建了 SANE 驱动程序。因此,几乎所有现代图像扫描仪都配备了 SANE 驱动程序,使您能够在 Linux 系统中使用该设备。

VintaSoft Twain .NET SDK 提供 VintaSoft TWAIN JavaScript API 和 VintaSoft Web TWAIN 服务,使 Web 开发人员能够快速轻松地创建跨浏览器 Web 应用程序,用于在 Windows 和 Linux 系统中从 TWAIN 或 SANE 扫描仪采集图像。开发的 Web 应用程序可在所有主流 Web 浏览器中使用。

VintaSoft TWAIN JavaScript API 允许您在 Windows 和 Linux 系统中使用 TWAIN 和 SANE 图像扫描仪。也就是说,Web 应用程序的客户端使用相同的代码来操作 Windows 和 Linux 系统中的 TWAIN 和 SANE 图像扫描仪。要使用 VintaSoft TWAIN JavaScript API,您必须在本地计算机上安装 VintaSoft Web TWAIN 服务,因为 VintaSoft TWAIN JavaScript API 会向 VintaSoft Web TWAIN 服务发送请求以获取对本地 TWAIN 和 SANE 图像扫描仪的访问权限。有关 VintaSoft TWAIN JavaScript API 的详细信息,请参阅此处面向 Web 开发人员的在线文档:https://www.vintasoft.com/docs/vstwain-dotnet-web/

VintaSoft Web TWAIN 服务为本地计算机的所有用户提供了一个 Web API,用于访问本地 TWAIN 扫描仪。VintaSoft Web TWAIN 服务有 Windows 和 Linux 版本。
VintaSoft Web TWAIN 服务的 Windows 版本可以使用 Windows 安装程序安装在任何 Windows 计算机上。有关 Windows 版 VintaSoft Web TWAIN 服务的详细信息,请阅读此处:https://www.vintasoft.com/docs/vstwain-dotnet-web/Programming-Twain_Web-Vintasoft_Web_TWAIN_service.html
Linux 版 VintaSoft Web TWAIN 服务可以使用 .deb 或 .rpm 软件包安装在 Ubuntu、Debian 和 Fedora 计算机上。有关 VintaSoft Web TWAIN 服务的 Linux 版本的详细信息,请阅读此处:https://www.vintasoft.com/docs/vstwain-dotnet-web/Programming-Twain_Web-Vintasoft_Web_TWAIN_service_for_Linux.html

要在 Windows 和 Linux 的 Web 浏览器中创建从 TWAIN 或 SANE 图像扫描仪获取图像的 Web 应用程序,您应该完成以下步骤:

以下是 JavaScript 代码,展示了如何在 Windows 和 Linux 系统中的 Web 浏览器(Firefox、Chrome、Edge、Opera 等)中从 TWAIN 或 SANE 图像扫描仪获取图像:
// synchronously acquire images from TWAIN/SANE scanner and saves acquired images to PDF file
__synchronouslyAcquireImagesFromTwainScannerAndSaveToPdfFile();



/**
 * Synchronously acquires images from TWAIN/SANE scanner and saves acquired images to PDF file.
 */
function __synchronouslyAcquireImagesFromTwainScannerAndSaveToPdfFile() {
    // register the evaluation version of VintaSoft Web TWAIN service
    // please read how to get evaluation license in documentation: https://www.vintasoft.com/docs/vstwain-dotnet-web/Licensing-Twain_Web-Evaluation.html
    Vintasoft.Twain.WebTwainGlobalSettingsJS.register('REG_USER', 'REG_URL', 'REG_CODE', 'EXPIRATION_DATE');

    // URL to the VintaSoft Web TWAIN service
    var serviceUrl = 'https://localhost:25329/api/VintasoftTwainApi';
    // a Web API controller that allows to work with TWAIN and SANE devices
    var twainService = new Vintasoft.Shared.WebServiceControllerJS(serviceUrl);

    // TWAIN/SANE device manager
    var deviceManager = new Vintasoft.Twain.WebTwainDeviceManagerJS(twainService);

    // the default settings of device manager
    var deviceManagerInitSetting = new Vintasoft.Twain.WebTwainDeviceManagerInitSettingsJS();

    var device = null;
    try {
        // open device manager
        deviceManager.open(deviceManagerInitSetting);

        // get the default TWAIN/SANE device
        device = deviceManager.get_DefaultDevice();

        // open device without UI
        device.open(false);

        // create collection for images acquired from device
        var acquiredImages = new Vintasoft.Twain.WebAcquiredImageCollectionJS(deviceManager);

        var acquireModalState;
        do {
            // do one step of modal image acquisition process
            var acquireModalResult = device.acquireModalSync();
            // get state of image acquisition
            acquireModalState = acquireModalResult.get_AcquireModalState().valueOf();

            switch (acquireModalState) {
                case 2:   // image is acquired
                    // get acquired image
                    var acquiredImage = acquireModalResult.get_AcquiredImage();
                    // add acquired image to the collection of acquired images
                    acquiredImages.add(acquiredImage);
                    // save acquired image to "result.pdf" file
                    acquiredImages.saveImages("d:\\result.pdf", false, [ acquiredImage.get_Id() ]);
                    break;
                case 4:   // image scan is failed
                    alert(acquireModalResult.get_ErrorMessage());
                    break;
                case 9:   // image scan is finished
                    break;
            }
        }
        while (acquireModalState !== 0);
    }
    catch (ex) {
        alert(ex);
    }
    finally {
        if (device != null) {
            // close the device
            device.close();
        }
        // close the device manager
        deviceManager.close();
    }
}