Как получить информацию о возможностях/опциях TWAIN/SANE устройства?
В этом разделе
TWAIN стандарт определяет множество стандартных возможностей (XferCount, IPixelType, DoubleFeedDetection, IContrast, IRotation, ...), которые могут поддерживаться TWAIN устройством. Также TWAIN устройство может предоставлять пользовательские возможности, определяемые производителем сканера.
Если Вы хотите получить список возможностей, которые поддерживает TWAIN устройство, используйте функцию
WebTwainDeviceJS.getSupportedCapabilities.
Если Вы хотите получить текущее значение возможности TWAIN устройства, используйте функцию
WebTwainCapabilityInfoJS.get_CurrentValue.
Если Вы хотите получить значение по умолчанию возможности TWAIN устройства, используйте функцию
WebTwainCapabilityInfoJS.get_DefaultValue.
Если Вы хотите получить поддерживаемые значения возможности TWAIN устройства, используйте функцию
WebTwainCapabilityInfoJS.get_SupportedValues.
SANE API не определяет стандартные опции для SANE устройства, SANE API просто определяет правила, которые позволяют создавать опции, которые являются самодокументированными.
Если Вы хотите получить список опций, которые поддерживаются SANE устройством, используйте функцию
WebTwainDeviceJS.getSupportedCapabilities.
Если Вы хотите получить текущее значение опции SANE устройства, используйте функцию
WebTwainCapabilityInfoJS.get_CurrentValue.
Если Вы хотите получить поддерживаемые значения опции SANE устройства, используйте функцию
WebTwainCapabilityInfoJS.get_SupportedValues.
Вот JavaScript код, который демонстрирует, как получить информацию о поддерживаемых значениях возможностей/опций TWAIN/SANE устройства:
// get information about capability value from TWAIN/SANE scanner
__getCapValueFromTwainScanner();
/**
* Returns information about capability value from TWAIN/SANE scanner.
*/
function __getCapValueFromTwainScanner() {
// 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);
// if device is SANE device
if (device.get_DeviceName().substring(0, 4) == "Sane") {
// get "mode" option of SANE device
var scanModeOption = device.getCapability("mode", new Vintasoft.Twain.WebTwainDeviceCapabilityUsageModeEnumJS("Get"));
// if "mode" option is supported by SANE device
if (scanModeOption != null) {
// output information about current scan mode
console.log("Current scan mode: " + scanModeOption.get_CurrentValue());
// get supported scan modes
var supportedScanModes = scanModeOption.get_SupportedValues();
var supportedScanModesAsArray = supportedScanModess.get_SupportedValuesAsArray();
var supportedScanModesString = "Supported scan modes: ";
// for each supported scan mode
for (var j = 0; j < supportedScanModesAsArray.length; j++) {
supportedScanModesString += supportedScanModesAsArray[j].toString() + ' ';
}
// output information about supported scan modes
console.log(supportedScanModesString);
}
}
// if device is TWAIN device
else {
// get IPixelType capability of TWAIN device
var pixelTypeCap = device.getCapability(
new Vintasoft.Twain.WebDeviceCapabilityIdEnumJS("IPixelType"),
new Vintasoft.Twain.WebTwainDeviceCapabilityUsageModeEnumJS("Get"));
// if IPixelType capability is supported by TWAIN device
if (pixelTypeCap != null) {
// output information about current pixel type
console.log("Current pixel type: " + pixelTypeCap.get_CurrentValue());
// output information about default pixel type
console.log("Default pixel type: " + pixelTypeCap.get_DefaultValue());
// get supported pixel types
var supportedPixelTypes = pixelTypeCap.get_SupportedValues();
var supportedPixelTypesAsArray = supportedPixelTypes.get_SupportedValuesAsArray();
var supportedPixelTypesString = "Supported pixel types: ";
// for each supported pixel type
for (var j = 0; j < supportedPixelTypesAsArray.length; j++) {
supportedPixelTypesString += supportedPixelTypesAsArray[j].toString() + ' ';
}
// output information about supported pixel types
console.log(supportedPixelTypesString);
}
}
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();
// get image as Base64 string
var bitmapAsBase64String = acquiredImage.getAsBase64String();
// update image preview
var previewImageElement = document.getElementById('previewImage');
previewImageElement.src = bitmapAsBase64String;
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 устройства:
import { Component } from '@angular/core';
@Component({
selector: 'twain-scanning-demo',
templateUrl: './twain-scanning-demo.component.html'
})
export class TwainScanningDemoComponent {
ngOnInit() {
// get information about capability value from TWAIN/SANE device
this.__getCapValueFromTwainScanner();
}
/**
* Returns information about capability value from TWAIN/SANE device.
*/
__getCapValueFromTwainScanner() {
// 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 device: Vintasoft.Twain.WebTwainDeviceJS = null;
try {
// get the default TWAIN/SANE device
device = deviceManager.get_DefaultDevice();
// open device without UI
device.open(false);
// if device is SANE device
if (device.get_DeviceName().substring(0, 4) == "Sane") {
// get "mode" option of SANE device
let scanModeOption: Vintasoft.Twain.WebTwainCapabilityInfoJS = device.getCapability(
"mode",
new Vintasoft.Twain.WebTwainDeviceCapabilityUsageModeEnumJS("Get"));
// if "mode" option is supported by SANE device
if (scanModeOption != null) {
// output information about current scan mode
console.log("Current scan mode: " + scanModeOption.get_CurrentValue());
// get supported scan modes
let supportedScanModes: Vintasoft.Twain.WebTwainCapabilitySupportedValuesJS = scanModeOption.get_SupportedValues();
let supportedScanModesAsArray: object[] = supportedScanModes.get_SupportedValuesAsArray();
let supportedScanModesString: string = "Supported scan modes: ";
// for each supported scan mode
for (let j: number = 0; j < supportedScanModesAsArray.length; j++) {
supportedScanModesString += supportedScanModesAsArray[j].toString() + ' ';
}
// output information about supported scan modes
console.log(supportedScanModesString);
}
}
// if device is TWAIN device
else {
// get IPixelType capability of TWAIN device
let pixelTypeCap: Vintasoft.Twain.WebTwainCapabilityInfoJS = device.getCapability(
new Vintasoft.Twain.WebDeviceCapabilityIdEnumJS("IPixelType"),
new Vintasoft.Twain.WebTwainDeviceCapabilityUsageModeEnumJS("Get"));
// if IPixelType capability is supported by TWAIN scanner
if (pixelTypeCap != null) {
// output information about current pixel type
console.log("Current pixel type: " + pixelTypeCap.get_CurrentValue());
// output information about default pixel type
console.log("Default pixel type: " + pixelTypeCap.get_DefaultValue());
// get supported pixel types
let supportedPixelTypes: Vintasoft.Twain.WebTwainCapabilitySupportedValuesJS = pixelTypeCap.get_SupportedValues();
let supportedPixelTypesAsArray: object[] = supportedPixelTypes.get_SupportedValuesAsArray();
let supportedPixelTypesString: string = "Supported pixel types: ";
// for each supported pixel type
for (let j: number = 0; j < supportedPixelTypesAsArray.length; j++) {
supportedPixelTypesString += supportedPixelTypesAsArray[j].toString() + ' ';
}
// output information about supported pixel types
console.log(supportedPixelTypesString);
}
}
let acquireModalState: number;
do {
// do one step of modal image acquisition process
let acquireModalResult: Vintasoft.Twain.WebTwainDeviceAcquireModalResultJS = device.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();
// 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;
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();
}
}
}