VintaSoft Twain .NET SDK 15.4: Документация для Веб разработчика
В этом разделе
Отмена получения изображений от TWAIN/WIA/SANE/eSCL сканера изображений
В этом разделе
Для отмены получения изображения от TWAIN/WIA/SANE/eSCL сканера изображений, Вам следует выполнить следующие действия:
  1. Вызовите функцию WebTwainDeviceJS.cancelTransfer.
  2. Подождите когда функция WebTwainDeviceJS.acquireModalSync вернет WebTwainAcquireModalStateEnumJS.ScanCanceled.
Некоторые высокоскоростные TWAIN сканеры имеют внутренний буфер, и TWAIN сканеры кэшируют полученные изображения в буфере, если TWAIN сканер работает быстрее, чем приложение, обрабатывающее полученные изображения. В случае отмены процесса получения изображений, изображения, кэшируемые во внутреннем буфере, будут потеряны.


Вот JavaScript код, который демонстрирует, как начать получение изображений от TWAIN/WIA/SANE/eSCL сканера изображений и отменить получение изображений:
// TWAIN/WIA/SANE/eSCL device manager
var _deviceManager;
// TWAIN/WIA/SANE/eSCL device
var _device = null;
// 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;


// start image acquisition from TWAIN/WIA/SANE/eSCL image scanner and cancel image acquisition
__startImageAcquisitionFromTwainScannerAndCancelImageAcquisition();


/**
 * Starts image acquisition from TWAIN/WIA/SANE/eSCL image scanner and cancels image acquisition.
 */
function __startImageAcquisitionFromTwainScannerAndCancelImageAcquisition() {
    // 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 devices
    var twainService = new Vintasoft.Shared.WebServiceControllerJS(serviceUrl);

    // TWAIN/WIA/SANE/eSCL device manager
    _deviceManager = new Vintasoft.Twain.WebTwainDeviceManagerJS(twainService);
    // create a collection that stores images, which are acquired from TWAIN/WIA/SANE/eSCL devices and stored in memory of VintaSoft Web TWAIN service
    _acquiredImages = new Vintasoft.Twain.WebAcquiredImageCollectionJS(_deviceManager);

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

    try {
        // open device manager
        _deviceManager.open(deviceManagerInitSetting);

        // get the default TWAIN/WIA/SANE/eSCL device
        _device = _deviceManager.get_DefaultDevice();

        // open device
        _device.open();

        // specify that image scanning must be canceled after 2 seconds
        setTimeout(__cancelTwainImageScanning, 2000);

        // do one step of modal image acquisition process
        _device.acquireModalAsync(__acquireModal_success, __acquireModal_error);
    }
    catch (ex) {
        alert(ex);
    }
}

/**
 One image scanning iteration is executed successfully.
 @param {object} twainDevice An instance of WebTwainDeviceJS class.
 @param {object} acquireModalResult An instance of WebTwainDeviceAcquireModalResultJS class.
*/
function __acquireModal_success(twainDevice, acquireModalResult) {
    // get the state of TWAIN image scanning
    var acquireModalStateValue = acquireModalResult.get_AcquireModalState().valueOf();
    switch (acquireModalStateValue) {
        // if image is acquired
        case 2:
            // get acquired image
            var acquiredImage = acquireModalResult.get_AcquiredImage();
            // add acquired image to the image collection
            _acquiredImages.add(acquiredImage);

            // get image as Base64 string
            var bitmapAsBase64String = acquiredImage.getAsBase64String();
            // update image preview
            var previewImageElement = document.getElementById('previewImage');
            previewImageElement.src = bitmapAsBase64String;

            // clear image collection (delete images from memory of VintaSoft Web TWAIN service) because image is not necessary anymore
            _acquiredImages.clear();
            break;

        // if image scanning is failed
        case 4:
            alert(acquireModalResult.get_ErrorMessage());
            break;

       // if image scanning is canceled
        case 5:
            alert('Scan is canceled.');
            break;

        // if image scanning is finished
        case 9:
            break;
    }

    // if modal image acquisition process is not finished and must be continued
    if (acquireModalStateValue !== 0) {
        // send asynchronous request for doing one step of modal image acquisition process
        _device.acquireModalAsync(__acquireModal_success, __acquireModal_error);
    }
    // if image scanning is finished
    else {
        // close device manager
        __closeDeviceManager();
    }
}

/**
 One image scanning iteration is failed.
 @param {object} data Information about error.
*/
function __acquireModal_error(data) {
    alert('Acquire modal error: ' + data);
}

/**
 Closes TWAIN/WIA/SANE/eSCL device manager.
*/
function __closeDeviceManager() {
    if (_device != null) {
        // close the device
        _device.close();
    }
    // close the device manager
    _deviceManager.close();
}

function __cancelTwainImageScanning() {
    if (_device != null) {
        // cancel image acquisition from image scanner
        _device.cancelTransfer();
    }
}



Вот TypeScript код, который демонстрирует, как начать получение изображений от TWAIN/WIA/SANE/eSCL сканера изображений и отменить получение изображений:
import { Component } from '@angular/core';


var _twainScanningDemoComponent: TwainScanningDemoComponent;


@Component({
  selector: 'twain-scanning-demo',
  templateUrl: './twain-scanning-demo.component.html'
})
export class TwainScanningDemoComponent {

  // TWAIN device manager
  _deviceManager: Vintasoft.Twain.WebTwainDeviceManagerJS;
  // TWAIN device
  _device: Vintasoft.Twain.WebTwainDeviceJS;
  // a collection that stores images, which are acquired from TWAIN/WIA/SANE/eSCL devices and stored in memory of VintaSoft Web TWAIN service
  _acquiredImages: Vintasoft.Twain.WebAcquiredImageCollectionJS;


  ngOnInit() {
    _twainScanningDemoComponent = this;

    // start image acquisition from TWAIN/WIA/SANE/eSCL image scanner and cancel image acquisition
    this.__startImageAcquisitionFromTwainScannerAndCancelImageAcquisition();
  }


  /**
   * Starts image acquisition from TWAIN/WIA/SANE/eSCL image scanner and cancels image acquisition.
   */
  __startImageAcquisitionFromTwainScannerAndCancelImageAcquisition() {
    // 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 devices
    let twainService: Vintasoft.Shared.WebServiceControllerJS = new Vintasoft.Shared.WebServiceControllerJS(serviceUrl);

    // TWAIN/WIA/SANE/eSCL device manager
    this._deviceManager = new Vintasoft.Twain.WebTwainDeviceManagerJS(twainService);
    // create a collection that stores images, which are acquired from TWAIN/WIA/SANE/eSCL devices and stored in memory of VintaSoft Web TWAIN service
    this._acquiredImages = new Vintasoft.Twain.WebAcquiredImageCollectionJS(this._twainDeviceManager);

    // the default settings of TWAIN/WIA/SANE/eSCL device manager
    let deviceManagerInitSetting: Vintasoft.Twain.WebTwainDeviceManagerInitSettingsJS = new Vintasoft.Twain.WebTwainDeviceManagerInitSettingsJS();

    try {
      // open TWAIN/WIA/SANE/eSCL device manager
      this._deviceManager.open(deviceManagerInitSetting);
    }
    catch (ex) {
      alert(ex);
      return;
    }

    try {
      // get the default TWAIN/WIA/SANE/eSCL device
      _device = _deviceManager.get_DefaultDevice();

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

      // specify that image scanning must be canceled after 2 seconds
      setTimeout(this.__cancelTwainImageScanning, 2000);

      // do one step of modal image acquisition process
      this._device.acquireModalAsync(this.__acquireModal_success, this.__acquireModal_error);
    }
    catch (ex) {
      alert(ex);
    }
  }

  /**
   * One image scanning iteration is executed successfully.
   * @param twainDevice An instance of WebTwainDeviceJS class.
   * @param acquireModalResult An instance of WebTwainDeviceAcquireModalResultJS class.
   */
  __acquireModal_success(twainDevice, acquireModalResult) {
    // get the state of TWAIN image scanning
    var acquireModalStateValue = acquireModalResult.get_AcquireModalState().valueOf();
    switch (acquireModalStateValue) {
      // if image is acquired
      case 2:
        // get acquired image
        let acquiredImage: Vintasoft.Twain.WebAcquiredImageJS = acquireModalResult.get_AcquiredImage();
        // add acquired image to the image collection
        this._acquiredImages.add(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;

        // clear image collection (delete images from memory of VintaSoft Web TWAIN service) because image is not necessary anymore
        this._acquiredImages.clear();
        break;

      // if image scanning is failed
      case 4:
        alert(acquireModalResult.get_ErrorMessage());
        break;

      // if image scanning is canceled
      case 5:
        alert('Scan is canceled.');
        break;

      // if image scanning is finished
      case 9:
        break;
    }

    // if modal image acquisition process is not finished and must be continued
    if (acquireModalStateValue !== 0) {
      // send asynchronous request for doing one step of modal image acquisition process
      _twainScanningDemoComponent._device.acquireModalAsync(_twainScanningDemoComponent.__acquireModal_success, _twainScanningDemoComponent.__acquireModal_error);
    }
    // if image scanning is finished
    else {
      // close device manager
      _twainScanningDemoComponent.__closeTwainDeviceManager();
    }
  }

  /**
   One image scanning iteration is failed.
   @param {object} data Information about error.
  */
  __acquireModal_error(data) {
    alert('Acquire modal error: ' + data);
  }

  /**
   * Closes TWAIN/WIA/SANE/eSCL device manager.
   */
  __closeTwainDeviceManager() {
    if (_twainScanningDemoComponent._device != null) {
      // close the device
      _twainScanningDemoComponent._device.close();
    }
    // close the device manager
    _twainScanningDemoComponent._deviceManager.close();
  }

  /**
   * Cancels image acquisition from TWAIN image scanner.
   */
  __cancelTwainImageScanning() {
    if (_twainScanningDemoComponent._device != null) {
      // cancel image acquisition from TWAIN image scanner
      _twainScanningDemoComponent._device.cancelTransfer();
    }
  }

}