VintaSoft Twain .NET SDK 15.1: Documentation for Web developer
In This Topic
    JavaScript classes for image acquisition from TWAIN/SANE scanner
    In This Topic
    Vintasoft.Twain.js file contains classes, which allow to acquire images from local TWAIN/SANE image scanner:
    Vintasoft.Twain.d.ts file is TypeScript module for Vintasoft.Twain.js file and it contains definitions of classes and enumeration for image acquisition from TWAIN/SANE scanner in TypeScript.


    Important: Vintasoft.Twain.js file has references to Vintasoft.Shared.js file.
    Important: Vintasoft.Twain.d.ts file must be used together with Vintasoft.Twain.js, Vintasoft.Shared.js and Vintasoft.Shared.d.ts files.
    Important: Vintasoft.Twain.js and Vintasoft.Twain.d.ts files use VintaSoft Web TWAIN service to communicate with local TWAIN and SANE scanners in Windows and Linux.


    For image acquisition from TWAIN/SANE scanner it is necessary to:
    1. 1. Create an instance of WebServiceJS class that will be used for communication with local Vintasoft Web TWAIN service.
    2. 2. Create an instance of WebTwainDeviceManagerJS class and specify that TWAIN device manager must use the web service created on step 1.
    3. 3. Create an instance of WebTwainDeviceManagerInitSettingsJS class and specify parameters of TWAIN/SANE device manager.
    4. 4. Call WebTwainDeviceManagerJS.open function for opening the TWAIN/SANE device manager.
    5. 5. If WebTwainDeviceManagerJS.open function threw an exception, display the error message and go to step 19; otherwise, go to next step.
    6. 6. Call WebTwainDeviceManagerJS.get_Devices function for getting list of available TWAIN/SANE devices.
    7. 7. If WebTwainDeviceManagerJS.get_Devices function threw an exception, display error message and go to step 17; otherwise, go to next step.
    8. 8. Select the necessary device (an instance of WebTwainDeviceJS class) from available TWAIN/SANE devices.
    9. 9. Call WebTwainDeviceJS.open function for opening the TWAIN/SANE device.
    10. 10. If WebTwainDeviceJS.open function threw an exception, display the error message and go to step 17; otherwise, go to next step.
    11. 11. Set TWAIN device capabilities (pixel type, image resolution, enable document feeder, etc) or SANE device options if necessary.
    12. 12. Call WebTwainDeviceJS.acquireModalSync function for executing one step of image acquisition process.
    13. 13. If WebTwainDeviceJS.acquireModalSync function threw an exception, display error message and go to step 15; otherwise, go to next step.
    14. 14. Process results of image acquisition step:
      1. If the image is acquired from device (acquireModalResult.acquireModalState === 2), save the acquired image to an image collection for further processing (display, process, save, upload) and go to step 12.
      2. If the scan is failed (acquireModalResult.acquireModalState === 4), display the error message, go to step 12 because we should wait until the image acquisition is finished (data.acquireModalState === 0).
      3. If the scan is finished (acquireModalResult.acquireModalState === 9), go to step 12 because we should wait until the image acquisition is finished (data.acquireModalState === 0).
      4. If the image acquisition is finished (acquireModalResult.acquireModalState === 0), go to step 15.
    15. 15. Call WebTwainDeviceJS.close function for closing the TWAIN/SANE device.
    16. 16. If WebTwainDeviceJS.close threw an exception, display the error message and go to step 17; otherwise, go to next step.
    17. 17. Call WebTwainDeviceManagerJS.close function for closing the TWAIN/SANE device manager.
    18. 18. If WebTwainDeviceManagerJS.close function threw an exception, display the error message; otherwise, go to next step.
    19. 19. Image acquisition is finished.


    Here is JavaScript code that demonstrates how to acquire images from TWAIN/SANE scanner:
    <script type="text/javascript">
        // acquire images from TWAIN/SANE scanner
        __acquireImageFromTwainScanner();
    
    
    
        /**
         * Acquires images from TWAIN/SANE scanner.
         */
        function __acquireImageFromTwainScanner() {
            // 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);
    
                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();
                            // get image as Base64 string
                            var bitmapAsBase64String = acquiredImage.getAsBase64String();
                            // update image preview
                            var previewImageElement = document.getElementById('previewImage');
                            previewImageElement.src = bitmapAsBase64String;
                            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();
            }
        }
    </script>
    
    


    Here is TypeScript code of Angular component that demonstrates how to acquire images from TWAIN/SANE scanner:
    import { Component } from '@angular/core';
    
    @Component({
      selector: 'twain-scanning-demo',
      templateUrl: './twain-scanning-demo.component.html'
    })
    export class TwainScanningDemoComponent {
    
      ngOnInit() {
        // acquire images from TWAIN/SANE scanner
        this.__acquireImageFromTwainScanner();
      }
    
    
      /**
      * Acquires images from TWAIN/SANE scanner.
      */
      __acquireImageFromTwainScanner() {
        // 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
        let serviceUrl: string = 'https://localhost:25329/api/VintasoftTwainApi';
        // a Web API controller that allows to work with TWAIN and SANE devices
        let twainService: Vintasoft.Shared.WebServiceControllerJS = new Vintasoft.Shared.WebServiceControllerJS(serviceUrl);
    
        // TWAIN/SANE device manager
        let deviceManager: Vintasoft.Twain.WebTwainDeviceManagerJS = new Vintasoft.Twain.WebTwainDeviceManagerJS(twainService);
    
        // the default settings of device manager
        let deviceManagerInitSetting: Vintasoft.Twain.WebTwainDeviceManagerInitSettingsJS = new Vintasoft.Twain.WebTwainDeviceManagerInitSettingsJS();
    
        try {
          // open device manager
          deviceManager.open(deviceManagerInitSetting);
        }
        catch (ex) {
          alert(ex);
          return;
        }
    
        let device: Vintasoft.Twain.WebTwainDeviceJS = null;
        try {
          // get the default TWAIN/SANE device
          device = deviceManager.get_DefaultDevice();
    
          // open device without UI
          device.open(false);
    
          let acquireModalState: number;
          do {
            // do one step of modal image acquisition process
            let acquireModalResult: Vintasoft.Twain.WebTwainDeviceAcquireModalResultJS = device.acquireModalSync();
            // get state of image acquisition
            acquireModalState = acquireModalResult.get_AcquireModalState().valueOf() as number;
    
            switch (acquireModalState) {
              case 2:   // image is acquired
                // get acquired image
                let acquiredImage: Vintasoft.Twain.WebAcquiredImageJS = acquireModalResult.get_AcquiredImage();
                // get image as Base64 string
                let bitmapAsBase64String: string = acquiredImage.getAsBase64String();
                // update image preview
                let previewImageElement: HTMLImageElement = document.getElementById('previewImage') as HTMLImageElement;
                previewImageElement.src = bitmapAsBase64String;
                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();
        }
      }
    
    }