dynamic web twain 完整示例
<!DOCTYPE html> <html> <head> <title>ScanAll</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <script type="text/javascript" src="Resources/dynamsoft.webtwain.initiate.js"></script> <script type="text/javascript" src="Resources/dynamsoft.webtwain.config.js"></script> </head> <body> <div style="display:none;"> <div id="dwtcontrolContainer"></div> </div> <select id="scanners"></select> <input type="button" value="开始扫描" onclick="AcquireImage()" /> <span id="status"></span> <span id="error" style="color:red;"></span> <div id="result"> <div style="margin-top:20px;"> <div style="border:1px solid gray; float:left;"> <img id="image1" alt="正面" style="max-width: 500px;width:auto;height:auto;" /> </div> <div style="border:1px solid gray; float:left;clear:right; margin-left: 10px;"> <img id="image2" alt="反面" style="max-width: 500px;width:auto;height:auto;" /> </div> </div> </div> <script type="text/javascript"> var DWObject; var images = new Array(); Dynamsoft.WebTwainEnv.RegisterEvent('OnWebTwainReady', Dynamsoft_OnReady); function Dynamsoft_OnReady() { DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer'); DWObject.OpenSourceManager(); var scanners = document.getElementById("scanners"); for (var i = 0; i < DWObject.SourceCount; i++) { var sourceName = DWObject.SourceNameItems(i); var scanner = document.createElement("option"); scanner.value = i; scanner.text = sourceName; scanners.add(scanner); } DWObject.CloseSourceManager(); } function generateUUID() { return 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'.replace(/[x]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); var result = v.toString(16); return result; }); } function getACapability(capability) {// Specify a Capability with capability var result = ""; STR_CapValueType = [ 'TWTY_INT8', 'TWTY_INT16', 'TWTY_INT32', 'TWTY_UINT8', 'TWTY_UINT16', 'TWTY_int', 'TWTY_BOOL', 'TWTY_FIX32', 'TWTY_FRAME', 'TWTY_STR32', 'TWTY_STR64', 'TWTY_STR128', 'TWTY_STR255']; DWObject.Capability = capability; // Specify a Capability DWObject.CapGet(); var i, nCapType = DWObject.CapType; switch (nCapType) { case EnumDWT_CapType.TWON_ARRAY/*3*/: result += ('Available Values:'); for (i = 0; i < DWObject.CapNumItems; i++) { if (DWObject.CapValueType > 8) /* >8 is string*/ /*STR*/result += "\n" + (DWObject.GetCapItemsString(i)); else /*NUM*/result += "\n" + (DWObject.GetCapItems(i)); } break; case EnumDWT_CapType.TWON_ENUMERATION/*4*/: result += "\n" + ('Available Values:'); for (i = 0; i < DWObject.CapNumItems; i++) { if (DWObject.CapValueType > 8) /*STR*/result += "\n" + (DWObject.GetCapItemsString(i)); else /*NUM*/result += "\n" + (DWObject.GetCapItems(i)); } if (DWObject.CapValueType > 8) { result += "\n" + ('Current Index = ' + DWObject.CapCurrentIndex + ' (Value: ' + DWObject.GetCapItemsString( DWObject.CapCurrentIndex) + ')'); result += "\n" + ('Default Index = ' + DWObject.CapDefaultIndex + ' (Value: ' + DWObject.GetCapItemsString( DWObject.CapDefaultIndex) + ')'); } else { result += "\n" + ('Current Index = ' + DWObject.CapCurrentIndex + ' (Value: ' + DWObject.GetCapItems(DWObject.CapCurrentIndex) + ')'); result += "\n" + ('Default Index = ' + DWObject.CapDefaultIndex + ' (Value: ' + DWObject.GetCapItems(DWObject.CapDefaultIndex) + ')'); } break; case EnumDWT_CapType.TWON_ONEVALUE/*5*/: var tempValue = ''; if (DWObject.CapValueType > 8) /*STR*/tempValue = DWObject.CapValueString; else /*NUM*/tempValue = DWObject.CapValue; /* * Special for BOOL */ if (DWObject.CapValueType == EnumDWT_CapValueType.TWTY_BOOL) { if (tempValue == 0) tempValue = 'FALSE'; else tempValue = 'TRUE'; } result += "\n" + ('ItemType = ' + STR_CapValueType[DWObject.CapValueType]); result += "\n" + ('Value = ' + tempValue); break; case EnumDWT_CapType.TWON_RANGE/*6*/: result += "\n" + ('ItemType = ' + STR_CapValueType[DWObject.CapValueType]); result += "\n" + ('Min = ' + DWObject.CapMinValue); result += "\n" + ('Max = ' + DWObject.CapMaxValue); result += "\n" + ('StepSize = ' + DWObject.CapStepSize); result += "\n" + ('Default = ' + DWObject.CapDefaultValue); result += "\n" + ('Current = ' + DWObject.CapCurrentValue); break; default: result += "\n" + ('This Capability is not supported'); } var supportLevel = []; if (DWObject.CapIfSupported(EnumDWT_MessageType.TWQC_GET)) supportLevel.push('GET');/*TWQC_GET*/ if (DWObject.CapIfSupported(EnumDWT_MessageType.TWQC_SET)) supportLevel.push('SET');/*TWQC_SET*/ if (DWObject.CapIfSupported(EnumDWT_MessageType.TWQC_RESET)) supportLevel.push('RESET');/*TWQC_RESET*/ if (supportLevel.length > 0) { result += "\n" + ('Supported operations: '); result += "\n" + (supportLevel.join(' / ')); } return result; } //Set A Capability function setACapability(capability, valueToSet, indexToSet) { var tempValue = '', i, valueToShow; DWObject.Capability = capability; DWObject.CapGet(); nCapType = DWObject.CapType; switch (nCapType) { case EnumDWT_CapType.TWON_ARRAY/*3*/: console.log('Setting an Array is not implemented'); break; case EnumDWT_CapType.TWON_ENUMERATION/*4*/: DWObject.CapValue = valueToSet; DWObject.CapCurrentIndex = indexToSet; DWObject.CapSet(); console.log('CapSet: ' + DWObject.ErrorString); DWObject.CapGet(); console.log('After Setting:'); if (DWObject.CapValueType > 8) { console.log('Current Index = ' + DWObject.CapCurrentIndex + ' (Value: ' + DWObject.GetCapItemsString( DWObject.CapCurrentIndex) + ')'); console.log('Default Index = ' + DWObject.CapDefaultIndex + ' (Value: ' + DWObject.GetCapItemsString( DWObject.CapDefaultIndex) + ')'); } else { console.log('Current Index = ' + DWObject.CapCurrentIndex + ' (Value: ' + DWObject.GetCapItems(DWObject.CapCurrentIndex) + ')'); console.log('Default Index = ' + DWObject.CapDefaultIndex + ' (Value: ' + DWObject.GetCapItems(DWObject.CapDefaultIndex) + ')'); } break; case EnumDWT_CapType.TWON_ONEVALUE/*5*/: DWObject.CapValue = valueToSet; DWObject.CapSet(); console.log('CapSet: ' + DWObject.ErrorString); DWObject.CapGet(); console.log('Value after setting: ' + DWObject.CapValue); break; case EnumDWT_CapType.TWON_RANGE/*6*/: DWObject.CapCurrentValue = valueToSet; DWObject.CapSet(); console.log('CapSet: ' + DWObject.ErrorString); DWObject.CapGet(); console.log('Value after setting: ' + DWObject.CapCurrentValue); break; default: console.log('This Capability is not supported'); } } function AcquireImage() { var scanners = document.getElementById("scanners"); if (scanners.options.length <= 0) { alert("扫描仪列表为空,如果已连接扫描仪请刷新页面重试!"); return; } if (scanners.selectedIndex < 0) { alert("请选择一个扫描仪!"); return; } DWObject.CloseSource(); document.getElementById("image1").src = ""; document.getElementById("image2").src = ""; document.getElementById("status").innerText = ""; document.getElementById("error").innerText = ""; var lastPaperNum = 1; var maxScanPageNum = 0; var scanCompleted = false; var paperId = generateUUID(); var scanedPageCount = 0; var uploadedPageCount = 0; var failedUploadPageCount = 0; DWObject.SetOpenSourceTimeout(3000); DWObject.SelectSourceByIndex(scanners.selectedIndex); DWObject.OpenSource(); setACapability(EnumDWT_Cap.CAP_XFERCOUNT, -1, 0); setACapability(EnumDWT_Cap.ICAP_AUTOMATICROTATE, 0, 0); DWObject.IfShowUI = false; DWObject.IfDisableSourceAfterAcquire = true; DWObject.PixelType = EnumDWT_PixelType.TWPT_GRAY; DWObject.PageSize = EnumDWT_CapSupportedSizes.TWSS_A4; DWObject.Resolution = 200; DWObject.IfAutoFeed = true; DWObject.IfDuplexEnabled = true; DWObject.RemoveAllImages(); document.getElementById("status").innerText = "扫描中..."; DWObject.RegisterEvent('OnPostTransfer', function () { scanedPageCount++; //console.log("OnPostTransfer" + scanedPageCount); var pageNumber = scanedPageCount; DWObject.ConvertToBase64([pageNumber - 1], EnumDWT_ImageType.IT_PNG, function (base64Result, newIndices, enumImageType, _arguments) { var length = base64Result.getLength(); //console.log("base64 calllback" + pageNumber + ":" + length); var imageId = "image" + (pageNumber % 2 + 1); var src = "data:image/png;base64," + base64Result.getData(0, length); document.getElementById(imageId).src = src; //console.log(imageId + " updated!") }, function (errorCode, errorString, newIndices, enumImageType, _arguments) { document.getElementById("error").innerText = "转换图片为base64时发生错误:" + errorString; }); if (pageNumber % 2 == 1) paperId = generateUUID(); var uploadFilename = paperId + "_" + (((pageNumber - 1) % 2) + 1) + '.png'; //上传图片 DWObject.HTTPUpload('SaveToFile.aspx', [pageNumber - 1], EnumDWT_ImageType.IT_PNG, EnumDWT_UploadDataFormat.Binary, uploadFilename, OnHttpUploadSuccess, OnHttpUploadFailure); function httpUploadProcess() { if (scanCompleted) { if (uploadedPageCount >= DWObject.HowManyImagesInBuffer) { if (failedUploadPageCount <= 0) document.getElementById("status").innerText = "扫描完成,图片上传完成!"; else document.getElementById("status").innerText = "扫描完成,图片上传完成,但有" + failedUploadPageCount + "张图片上传失败!"; } } } function OnHttpUploadSuccess(httpResponse, newIndices, _arguments) { uploadedPageCount++; console.log("file " + uploadFilename + " uploaded!") httpUploadProcess(); } function OnHttpUploadFailure(errorCode, errorString, httpResponse, newIndices, _arguments) { uploadedPageCount++; failedUploadPageCount++; document.getElementById("error").innerText = "ErrorCode: " + errorCode + "ErrorString: " + errorString + "HTTPResponseString: " + httpResponse; httpUploadProcess(); } }); DWObject.RegisterEvent('OnPostAllTransfers', function () { scanCompleted = true; DWObject.CloseSource(); document.getElementById("status").innerText = "扫描完成,正在上传图片..."; }); DWObject.AcquireImage(function () { DWObject.DisableSource(); }, function () { document.getElementById("status").innerText = "扫描被取消或扫描时发生错误!"; DWObject.DisableSource(); } ); } </script> </body> </html>
桂棹兮兰桨,击空明兮溯流光。