Web掃描識別控制元件Dynamic Web TWAIN掃描和上傳的例項
阿新 • • 發佈:2019-02-20
Dynamic Web TWAIN是一個專為Web應用程式設計的TWAIN掃描識別控制元件,允許使用者從掃描器掃描文件或從數碼相機/採集卡中獲取影象,並支援上傳和處理本地影象。本文將介紹如何通過TWAIN掃描識別工具Dynamic Web TWAIN實現在Web應用程式中掃描和上傳檔案。
下載並安裝Dynamic Web TWAIN後, 在 "\Dynamsoft\Dynamic Web TWAIN 9.0 Trial\Resources."目錄下找到下列檔案:
- DynamicWebTWAIN.cab/ DynamicWebTWAINx64.cab – the ActiveX control edition for Internet Explorer (IE) 32 bit and 64 bit
- DynamicWebTWAINPlugIn.msi - the Plugin Edition for Chrome, Firefox, Safari on Windows
- DynamicWebTWAINMacEditionTrial.pkg – the Mac Edition for Chrome, Firefox, Safari on Mac OS X
將上述提到4個Dynamic Web TWAIN 外掛檔案複製並貼上到MVC應用程式的Content資料夾中,從而將TWAIN掃描識別控制元件Dynamic Web TWAIN 嵌入到MVC應用程式。
Dynamic Web TWAIN文件掃描檢視
如下面的截圖所示,索引頁上已添加了一個"Scan" 選項卡。點選 "Scan",使用者將看到使用了Dynamic Web TWAIN外掛的Scan.aspx頁面。
啟動Dynamic Web TWAIN
function DW_CreateControl(bNeebBack) { var objString = ""; var DWTContainer; // For IE, render the ActiveX Object if (DW_InIE) { objString = "<object id='" + DW_ObjectName + "' style='width:" + DW_Width + "px;height:" + DW_Height + "px'"; if (DW_InWindowsX86) objString += "codebase='" + DW_CABX86Path + "#version=" + DW_VersionCode + "' "; //load 32 bit CAB file for 32 bit Windows else objString += "codebase='" + DW_CABX64Path + "#version=" + DW_VersionCode + "' "; //load 64 bit CAB file for 64 bit Windows var temp = DW_IsTrial ? DW_TRAILCLASSID : DW_FULLCLASSID; objString += " classid='clsid:" + temp + "' viewastext>"; objString += " <param name='Manufacturer' value='DynamSoft Corporation' />"; objString += " <param name='ProductFamily' value='" + DW_ProductName + "' />"; objString += " <param name='ProductName' value='" + DW_ProductName + "' />"; //objString += " <param name='wmode' value='transparent'/> "; objString += " </object>"; } // For non-IE browsers, render the embed object else { objString = " <embed id='" + DW_ObjectName + "'style='display: inline; width:" + DW_Width + "px;height:" + DW_Height + "px' id='" + DW_ObjectName + "' type='" + DW_MIMETYPE + "'"; objString += " OnPostTransfer='Dynamsoft_OnPostTransfer' OnPostAllTransfers='Dynamsoft_OnPostAllTransfers'"; objString += " OnMouseClick='Dynamsoft_OnMouseClick' OnPostLoad='Dynamsoft_OnPostLoadfunction'"; objString += " OnImageAreaSelected = 'Dynamsoft_OnImageAreaSelected'"; objString += " OnImageAreaDeSelected = 'Dynamsoft_OnImageAreaDeselected'"; objString += " OnMouseDoubleClick = 'Dynamsoft_OnMouseDoubleClick'"; objString += " OnMouseRightClick = 'Dynamsoft_OnMouseRightClick'"; objString += " OnTopImageInTheViewChanged = 'Dynamsoft_OnTopImageInTheViewChanged'"; if (DW_InWindows) objString += " pluginspage='" + else objString += " pluginspage='" + DW_PKGPath + "'></embed>"; //load the Mac edition for Chrome, Firefox, Safari on Mac } DWTContainer = document.getElementById(DW_DWTContainerID); DWTContainer.innerHTML = objString; DWObject = document.getElementById(DW_ObjectName); }
掃描文件
通過TWAIN掃描識別控制元件可以從掃描器中輕鬆捕獲影象。下列JavaScript程式碼設定了畫素型別,解析度,掃描型別等。
function AcquireImageInner() {
if (DW_DWTSourceContainerID == "")
DWObject.SelectSource();
else
DWObject.SelectSourceByIndex(document.getElementById(DW_DWTSourceContainerID).selectedIndex); //select from the available TWAIN devices
DWObject.CloseSource();
DWObject.OpenSource();
DWObject.IfShowUI = document.getElementById("ShowUI").checked;
var i;
for (i = 0; i < 3; i++) {
if (document.getElementsByName("PixelType").item(i).checked == true)
DWObject.PixelType = i;
} //set the pixel type of the image
DWObject.Resolution = Resolution.value;
DWObject.IfFeederEnabled = document.getElementById("ADF").checked; //whether scan from ADF or flatbed
DWObject.IfDuplexEnabled = document.getElementById("Duplex").checked; //whether do duplex scan
DWObject.IfDisableSourceAfterAcquire = true;
DWObject.AcquireImage(); //acquire images from the scanner
}
上傳文件
將影象掃描至Dynamic Web TWAIN後,你可以通過HTTP Post方法,將掃描影象上傳到Web伺服器。
var DW_ActionPage = "SaveToFile"; //call SaveToFile controller
function btnUpload_onclick(){
var i, strHTTPServer, strActionPage, strImageType;
strHTTPServer = DW_ServerName;
DWObject.HTTPPort = DW_strPort;
var CurrentPathName = unescape(location.pathname); // get current PathName in plain ASCII
var CurrentPath = CurrentPathName.substring(0, CurrentPathName.lastIndexOf("/") + 1);
strActionPage = CurrentPath + DW_ActionPage; //the ActionPage's file path
for(i=0;i<4;i++){
if(document.getElementsByName("ImageType").item(i).checked == true){
strImageType = i + 1;
break;
}
} //choose the image type, JPEG, TIFF, PNG or PDF
var uploadfilename = txt_fileName.value + "." + document.getElementsByName("ImageType").item(i).value;
if (strImageType == 2 && document.getElementById("MultiPageTIFF").checked) {
if ((DWObject.SelectedImagesCount == 1) || (DWObject.SelectedImagesCount == DWObject.HowManyImagesInBuffer)) {
DWObject.HTTPUploadAllThroughPostAsMultiPageTIFF(
strHTTPServer,
strActionPage,
uploadfilename
);
{
DWObject.HTTPUploadThroughPostAsMultiPageTIFF(
strHTTPServer,
strActionPage,
uploadfilename
);
}
} //whether to upload the images as a multi-page TIFF file
else if (strImageType == 4 && document.getElementById("MultiPagePDF").checked) {
if ((DWObject.SelectedImagesCount == 1) || (DWObject.SelectedImagesCount == DWObject.HowManyImagesInBuffer)) {
DWObject.HTTPUploadAllThroughPostAsPDF(
strHTTPServer,
strActionPage,
uploadfilename
);
}
else {
DWObject.HTTPUploadThroughPostAsMultiPagePDF(
strHTTPServer,
strActionPage,
uploadfilename
);
}
} //whether to upload the images as a multi-page PDF file
else {
DWObject.HTTPUploadThroughPostEx(
strHTTPServer,
DWObject.CurrentImageIndexInBuffer,
strActionPage,
uploadfilename,
strImageType
);
}
Controller.cs
public String SaveToFile()
{
String strImageName;
HttpFileCollectionBase files = Request.Files;
HttpPostedFileBase uploadfile = files["RemoteFile"];
strImageName = uploadfile.FileName;
uploadfile.SaveAs(Server.MapPath("~") + "\\UploadedImages\\" + strImageName);
return "";
}