Cancel the image acquisition from TWAIN/SANE/eSCL image scanner
In This Topic
You should do the following steps if you want to cancel the image acquisition from TWAIN/SANE/eSCL image scanner:
- Call the WebTwainDeviceJS.cancelTransfer function.
- Wait while the WebTwainDeviceJS.acquireModalSync function returns WebTwainAcquireModalStateEnumJS.ScanCanceled.
Some high speed TWAIN image scanners have an internal buffer and these TWAIN image scanners cache the acquired images into the buffer if the TWAIN image scanner is faster than application, which processes acquired images. Images, cached in the internal buffer of scanner, will be lost if you cancel the image acquisition process.
Here is JavaScript code that demonstrates how to start image acquisition from TWAIN/SANE/eSCL image scanner and cancel the image acquisition:
// TWAIN/SANE/eSCL device manager
var _deviceManager;
// TWAIN/SANE/eSCL device
var _device = null;
// a collection that stores images, which are acquired from TWAIN/SANE/eSCL devices and stored in memory of VintaSoft Web TWAIN service
var _acquiredImages;
// start image acquisition from TWAIN/SANE/eSCL image scanner and cancel image acquisition
__startImageAcquisitionFromTwainScannerAndCancelImageAcquisition();
/**
* Starts image acquisition from TWAIN/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.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/SANE/eSCL devices
var twainService = new Vintasoft.Shared.WebServiceControllerJS(serviceUrl);
// TWAIN/SANE/eSCL device manager
_deviceManager = new Vintasoft.Twain.WebTwainDeviceManagerJS(twainService);
// create a collection that stores images, which are acquired from TWAIN/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/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/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();
}
}
Here is TypeScript code that demonstrates how to start image acquisition from TWAIN/SANE/eSCL image scanner and cancel the image acquisition:
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/SANE/eSCL devices and stored in memory of VintaSoft Web TWAIN service
_acquiredImages: Vintasoft.Twain.WebAcquiredImageCollectionJS;
ngOnInit() {
_twainScanningDemoComponent = this;
// start image acquisition from TWAIN/SANE/eSCL image scanner and cancel image acquisition
this.__startImageAcquisitionFromTwainScannerAndCancelImageAcquisition();
}
/**
* Starts image acquisition from TWAIN/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.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/SANE/eSCL devices
let twainService: Vintasoft.Shared.WebServiceControllerJS = new Vintasoft.Shared.WebServiceControllerJS(serviceUrl);
// TWAIN/SANE/eSCL device manager
this._deviceManager = new Vintasoft.Twain.WebTwainDeviceManagerJS(twainService);
// create a collection that stores images, which are acquired from TWAIN/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/SANE/eSCL device manager
let deviceManagerInitSetting: Vintasoft.Twain.WebTwainDeviceManagerInitSettingsJS = new Vintasoft.Twain.WebTwainDeviceManagerInitSettingsJS();
try {
// open TWAIN/SANE/eSCL device manager
this._deviceManager.open(deviceManagerInitSetting);
}
catch (ex) {
alert(ex);
return;
}
try {
// get the default TWAIN/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/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();
}
}
}