VintaSoft Twain .NET SDK 15.3: Документация для Веб разработчика
В этом разделе
    Синхронное получение изображений от TWAIN/WIA/SANE/eSCL сканера изображений
    В этом разделе
    VintaSoft Web Twain сервис позволяет синхронно (в модальном цикле) получать изображения от TWAIN/WIA/SANE/eSCL сканера изображений.

    Вот список шагов, которые необходимо выполнить для синхронного получения изображений от TWAIN/WIA/SANE/eSCL сканера изображений:
    1. Откройте менеджер устройств TWAIN/WIA/SANE/eSCL.
    2. Выберите TWAIN/WIA/SANE/eSCL устройство.
    3. Откройте TWAIN/WIA/SANE/eSCL устройство.
    4. Установите параметры сканирования для TWAIN/WIA/SANE/eSCL устройства.
    5. Запустите синхронное получение изображений от TWAIN/WIA/SANE/eSCL устройства вызвав функцию WebTwainDeviceJS.acquireModalSync.
    6. Получите отсканированное изображение, если функция WebTwainDeviceJS.acquireModalSync вернула значение WebTwainAcquireModalStateEnumJS.ImageAcquired.
    7. Прервите цикл, если функция WebTwainDeviceJS.acquireModalSync вернула значение WebTwainAcquireModalStateEnumJS.None.

    Вот JavaScript код, который демонстрирует как синхронно получить изображения от TWAIN/WIA/SANE/eSCL сканера изображений и сохранить полученные изображения в PDF документ:
    // synchronously acquire images from TWAIN/WIA/SANE/eSCL image scanner and saves acquired images to PDF file
    __synchronouslyAcquireImagesFromTwainScannerAndSaveToPdfFile();
    
    
    
    /**
     * Synchronously acquires images from TWAIN/WIA/SANE/eSCL image 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.ru/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/WIA/SANE/eSCL/eSCL devices
        var twainService = new Vintasoft.Shared.WebServiceControllerJS(serviceUrl);
    
        // TWAIN/WIA/SANE/eSCL 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/WIA/SANE/eSCL device
            device = deviceManager.get_DefaultDevice();
    
            // open device without UI
            device.open(false);
    
            // a collection that stores images, which are acquired from TWAIN/WIA/SANE/eSCL devices and stored in memory of VintaSoft Web TWAIN service
            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() ]);
    
                        // clear image collection (delete images from memory of VintaSoft Web TWAIN service) because image is not necessary anymore
                        acquiredImages.clear();
                        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();
        }
    }
    
    


    Вот TypeScript код, который демонстрирует как синхронно получить изображения от TWAIN/WIA/SANE/eSCL сканера изображений и сохранить полученные изображения в PDF документ:
    import { Component } from '@angular/core';
    
    @Component({
      selector: 'twain-scanning-demo',
      templateUrl: './twain-scanning-demo.component.html'
    })
    export class TwainScanningDemoComponent {
    
      ngOnInit() {
        // synchronously acquire images from TWAIN/WIA/SANE/eSCL image scanner and saves acquired images to PDF file
        this.__synchronouslyAcquireImagesFromTwainScannerAndSaveToPdfFile();
      }
    
    
      /**
       * Synchronously acquires images from TWAIN/WIA/SANE/eSCL image scanner and saves acquired images to PDF file.
       */
      __synchronouslyAcquireImagesFromTwainScannerAndSaveToPdfFile() {
        // register the evaluation version of VintaSoft Web TWAIN service
        // please read how to get evaluation license in documentation: https://www.vintasoft.ru/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/WIA/SANE/eSCL/eSCL devices
        let twainService: Vintasoft.Shared.WebServiceControllerJS = new Vintasoft.Shared.WebServiceControllerJS(serviceUrl);
    
        // TWAIN/WIA/SANE/eSCL 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 twainDevice: Vintasoft.Twain.WebTwainDeviceJS = null;
        try {
          // get the default TWAIN/WIA/SANE/eSCL device
          twainDevice = deviceManager.get_DefaultDevice();
    
          // open device without UI
          twainDevice.open(false);
    
          // a collection that stores images, which are acquired from TWAIN/WIA/SANE/eSCL devices and stored in memory of VintaSoft Web TWAIN service
          let acquiredImages: Vintasoft.Twain.WebAcquiredImageCollectionJS = new Vintasoft.Twain.WebAcquiredImageCollectionJS(deviceManager);
    
          let acquireModalState: number;
          do {
            // do one step of modal image acquisition process
            let acquireModalResult: Vintasoft.Twain.WebTwainDeviceAcquireModalResultJS = twainDevice.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();
                // 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()]);
    
                // clear image collection (delete images from memory of VintaSoft Web TWAIN service) because image is not necessary anymore
                acquiredImages.clear();
                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 (twainDevice != null) {
            // close the device
            twainDevice.close();
          }
          // close the device manager
          deviceManager.close();
        }
      }
    
    }