Scan images using web application for Windows and Linux

Blog category: TWAINWeb

April 8, 2024

TWAIN is a standard protocol and interface (API) that defines the interaction between a program and an image scanner. TWAIN standard is being developed by TWAIN Working Group. Version 1 of TWAIN standard defines interaction with image scanners for Windows. Version 2 of TWAIN standard defines interaction with image scanners for Windows, macOS, Linux.
TWAIN standard is the most popular standard for image scanners for Windows. Almost any image scanner has a TWAIN driver for usage in Windows.
TWAIN standard is not as popular in Linux and our research (as of early 2024) showed that only Kodak provides TWAIN drivers for using their image scanners in Linux.

SANE is an application programming interface (API) that provides standardized access to raster image scanning devices (flatbed scanners, handheld scanners, etc). SANE API is a public domain and it is open to public discussion and development. SANE API is the most popular image scanner API for Linux.
Many scanner manufacturers have created SANE drivers for their image scanners. There are also SANE drivers created by SANE community. As a result, almost any modern image scanner has SANE driver that allows you to use the device in Linux.

VintaSoft Twain .NET SDK provides VintaSoft TWAIN JavaScript API and VintaSoft Web TWAIN service, which allow the web developer quickly and easily create a cross-browser web application for acquiring images from TWAIN or SANE scanner in Windows and Linux. The developed web applications can be used in all popular web browsers.

VintaSoft TWAIN JavaScript API is an API allowing you to work with TWAIN and SANE image scanners in Windows and Linux, i.e. the client side of web application uses the same code to work with TWAIN and SANE image scanners in Windows and Linux. For work of VintaSoft TWAIN JavaScript API you must install the VintaSoft Web TWAIN service on your local computer, because VintaSoft TWAIN JavaScript API sends requests to VintaSoft Web TWAIN service to get access to local TWAIN and SANE image scanners. The detailed information about VintaSoft TWAIN JavaScript API please read in on-line documentation for web developer here: https://www.vintasoft.com/docs/vstwain-dotnet-web/

VintaSoft Web TWAIN service provides a Web API for accessing local TWAIN scanners for all users of local computer. There is a version of VintaSoft Web TWAIN service for Windows and Linux.
The Windows version of VintaSoft Web TWAIN service can be installed on any Windows computer using a Windows installer. The detailed information about Windows version of VintaSoft Web TWAIN service please read here: https://www.vintasoft.com/docs/vstwain-dotnet-web/Programming-Twain_Web-Vintasoft_Web_TWAIN_service.html
The Linux version of VintaSoft Web TWAIN service can be installed on Ubuntu, Debian, Fedora computer using .deb- or .rpm-packet. The detailed information about Linux version of VintaSoft Web TWAIN service please read here: https://www.vintasoft.com/docs/vstwain-dotnet-web/Programming-Twain_Web-Vintasoft_Web_TWAIN_service_for_Linux.html

To create a web application that acquires images from TWAIN or SANE image scanner in a web browser in Windows and Linux, you should complete the following steps:

Here is JavaScript code, which shows how to acquire images from TWAIN or SANE image scanner in a web browser (Firefox, Chrome, Edge, Opera, etc) in Windows and Linux:
// 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();
    }
}