VintaSoft Twain .NET SDK 15.1: Документация для Веб разработчика
В этом разделе
    Сохранение отсканированных изображений
    В этом разделе
    SDK позволяет получить изображения от TWAIN/SANE сканера и сохранить полученные изображения в локальный файл. Также отсканированные изображения могут быть получены в виде Base64-строки. Изображение может быть сохранено как новый BMP, JPEG, PNG, TIFF файл или PDF файл (версия 1.4). Также изображение может быть добавлено в существующий файл TIFF или PDF (версии от 1.0 до 1.4).

    Вот JavaScript код, который демонстрирует, как получить изображения от TWAIN/SANE сканера и сохранить полученные изображения в файлы JPEG:
    // acquire images from TWAIN/SANE scanner and saves acquired images to JPEG files
    __acquireImagesFromTwainScannerAndSaveToJpegFiles();
    
    
    
    /**
     * Acquires images from TWAIN/SANE scanner and saves acquired images to JPEG files.
     */
    function __acquireImagesFromTwainScannerAndSaveToJpegFiles() {
        // 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 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;
            var savedImageCount = 0;
            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.jpg" file
                        acquiredImages.saveImages("d:\\result" + savedImageCount + ".jpg", false, [acquiredImage.get_Id()]);
                        // increase count of saved images
                        savedImageCount = savedImageCount + 1;
                        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/SANE сканера и сохранить полученные изображения в файлы JPEG:
    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 and saves acquired images to JPEG files
        this.__acquireImagesFromTwainScannerAndSaveToJpegFiles();
      }
    
    
      /**
       * Acquires images from TWAIN/SANE scanner and saves acquired images to JPEG files.
       */
      __acquireImagesFromTwainScannerAndSaveToJpegFiles() {
        // 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 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 twainDevice: Vintasoft.Twain.WebTwainDeviceJS = null;
        try {
          // get the default TWAIN/SANE device
          twainDevice = deviceManager.get_DefaultDevice();
    
          // open device without UI
          device.open(false);
    
          // create collection for images acquired from device
          let acquiredImages: Vintasoft.Twain.WebAcquiredImageCollectionJS = new Vintasoft.Twain.WebAcquiredImageCollectionJS(deviceManager);
    
          let acquireModalState: number;
          let imageCount: number = 0;
          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.jpg" file
                acquiredImages.saveImages("d:\\result" + imageCount + ".jpg", false, [acquiredImage.get_Id()]);
                // increase count of saved images
                imageCount = imageCount + 1;
                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();
        }
      }
    
    }
    
    


    Вот JavaScript код, который демонстрирует, как получить изображения от TWAIN/SANE сканера и сохранить полученные изображения в мультистраничный файл TIFF:
    // acquire images from TWAIN/SANE scanner and saves acquired images to TIFF file
    __acquireImagesFromTwainScannerAndSaveToTiffFile();
    
    
    
    /**
     * Acquires images from TWAIN/SANE scanner and saves acquired images to TIFF file.
     */
    function __acquireImagesFromTwainScannerAndSaveToTiffFile() {
        // 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 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 twainDevice = null;
        try {
            // open device manager
            deviceManager.open(deviceManagerInitSetting);
    
            // get the default TWAIN/SANE device
            twainDevice = 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 = twainDevice.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);
    
                        // get TIFF encoder settings
                        var tiffEncoderSettings = acquiredImages.get_TiffEncoderSettings();
                        // specify that TIFF encoder must create TIFF images with strips (not tiles)
                        tiffEncoderSettings.set_UseTiles(false);
    
                        // save acquired image to "result.tif" file
                        acquiredImages.saveImages("d:\\result.tif", 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 (twainDevice != null) {
                // close the device
                twainDevice.close();
            }
            // close the device manager
            deviceManager.close();
        }
    }
    
    


    Вот TypeScript код, который демонстрирует, как получить изображения от TWAIN/SANE сканера и сохранить полученные изображения в мультистраничный файл TIFF:
    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 and saves acquired images to TIFF file
        this.__acquireImagesFromTwainScannerAndSaveToTiffFile();
      }
    
    
      /**
       * Acquires images from TWAIN/SANE scanner and saves acquired images to TIFF file.
       */
      __acquireImagesFromTwainScannerAndSaveToTiffFile() {
        // 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 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 twainDevice: Vintasoft.Twain.WebTwainDeviceJS = null;
        try {
          // get the default TWAIN/SANE device
          twainDevice = deviceManager.get_DefaultDevice();
    
          // open device without UI
          device.open(false);
    
          // create collection for images acquired from device
          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);
    
                // get TIFF encoder settings
                let tiffEncoderSettings: Vintasoft.Twain.WebTwainTiffEncoderSettingsJS = acquiredImages.get_TiffEncoderSettings();
                // specify that TIFF encoder must create TIFF images with strips (not tiles)
                tiffEncoderSettings.set_UseTiles(false);
    
                // save acquired image to "result.tif" file
                acquiredImages.saveImages("d:\\result.tif", false, [acquiredImage.get_Id()]);
                break;
              case 4:   // 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();
        }
      }
    
    }
    
    


    Вот JavaScript код, который демонстрирует, как получить изображения от TWAIN/SANE сканера и сохранить полученные изображения в мультистраничный файл PDF:
    // acquire images from TWAIN/SANE scanner and saves acquired images to PDF file
    __acquireImagesFromTwainScannerAndSaveToPdfFile();
    
    
    
    /**
     * Acquires images from TWAIN/SANE scanner and saves acquired images to PDF file.
     */
    function __acquireImagesFromTwainScannerAndSaveToPdfFile() {
        // 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 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 twainDevice = null;
        try {
            // open device manager
            deviceManager.open(deviceManagerInitSetting);
    
            // get the default TWAIN/SANE device
            twainDevice = 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 = twainDevice.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 (twainDevice != null) {
                // close the device
                twainDevice.close();
            }
            // close the device manager
            deviceManager.close();
        }
    }
    
    


    Вот TypeScript код, который демонстрирует, как получить изображения от TWAIN/SANE сканера и сохранить полученные изображения в мультистраничный файл PDF:
    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 and saves acquired images to PDF file
        this.__acquireImagesFromTwainScannerAndSaveToPdfFile();
      }
    
    
      /**
       * Acquires images from TWAIN/SANE scanner and saves acquired images to PDF file.
       */
      __acquireImagesFromTwainScannerAndSaveToPdfFile() {
        // 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 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 twainDevice: Vintasoft.Twain.WebTwainDeviceJS = null;
        try {
          // get the default TWAIN/SANE device
          twainDevice = deviceManager.get_DefaultDevice();
    
          // open device without UI
          device.open(false);
    
          // create collection for images acquired from device
          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()]);
                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();
        }
      }
    
    }