Отмена получения изображений от TWAIN/SANE сканера
В этом разделе
Для отмены получения изображения от TWAIN/SANE сканера, Вам следует выполнить следующие действия:
- Вызовите функцию WebTwainDeviceJS.cancelTransfer.
- Подождите когда функция WebTwainDeviceJS.acquireModalSync вернет WebAcquireModalStateEnumJS.ScanCanceled.
Некоторые высокоскоростные TWAIN сканеры имеют внутренний буфер, и TWAIN сканеры кэшируют полученные изображения в буфере, если TWAIN сканер работает быстрее, чем приложение, обрабатывающее полученные изображения. В случае отмены процесса получения изображений, изображения, кэшируемые во внутреннем буфере, будут потеряны.
Вот JavaScript код, который демонстрирует, как начать получение изображений с помощью TWAIN/SANE сканера и отменить получение изображений:
// TWAIN/SANE device manager
var _deviceManager;
// TWAIN/SANE device
var _device = null;
// start image acquisition from TWAIN/SANE scanner and cancel image acquisition
__startImageAcquisitionFromTwainScannerAndCancelImageAcquisition();
/**
* Starts image acquisition from TWAIN/SANE 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 and SANE devices
var twainService = new Vintasoft.Shared.WebServiceControllerJS(serviceUrl);
// TWAIN/SANE device manager
_deviceManager = new Vintasoft.Twain.WebTwainDeviceManagerJS(twainService);
// the default settings of device manager
var deviceManagerInitSetting = new Vintasoft.Twain.WebTwainDeviceManagerInitSettingsJS();
try {
// open device manager
_deviceManager.open(deviceManagerInitSetting);
// get the default TWAIN/SANE 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();
// get image as Base64 string
var bitmapAsBase64String = acquiredImage.getAsBase64String();
// update image preview
var previewImageElement = document.getElementById('previewImage');
previewImageElement.src = bitmapAsBase64String;
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/SANE 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/SANE сканера и отменить получение изображений:
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;
ngOnInit() {
_twainScanningDemoComponent = this;
// start image acquisition from TWAIN/SANE scanner and cancel image acquisition
this.__startImageAcquisitionFromTwainScannerAndCancelImageAcquisition();
}
/**
* Starts image acquisition from TWAIN/SANE 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 and SANE devices
let twainService: Vintasoft.Shared.WebServiceControllerJS = new Vintasoft.Shared.WebServiceControllerJS(serviceUrl);
// TWAIN/SANE device manager
this._deviceManager = new Vintasoft.Twain.WebTwainDeviceManagerJS(twainService);
// the default settings of TWAIN/SANE device manager
let deviceManagerInitSetting: Vintasoft.Twain.WebTwainDeviceManagerInitSettingsJS = new Vintasoft.Twain.WebTwainDeviceManagerInitSettingsJS();
try {
// open TWAIN/SANE device manager
this._deviceManager.open(deviceManagerInitSetting);
}
catch (ex) {
alert(ex);
return;
}
try {
// get the default TWAIN/SANE 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();
// 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;
// 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/SANE 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 scanner.
*/
__cancelTwainImageScanning() {
if (_twainScanningDemoComponent._device != null) {
// cancel image acquisition from TWAIN scanner
_twainScanningDemoComponent._device.cancelTransfer();
}
}
}