图像或文档的上传和下载性能在很大程度上取决于网络质量、扫描图像的大小以及客户端机器的CPU和内存使用情况。然而,性能和安全性始终是衡量一个文档管理系统好坏的两个重要因素。本文将向展示如何通过嵌入ImageCapture Suite来提高图像上传/下载的效率。
Dynamsoft ImageCapture Suite是一个为Web应用程序优化的图像获取和处理SDK,它允许从扫描仪、网络摄像头和其他兼容TWAIN/UVC/WIA的设备捕获图像。如果对SDK感兴趣,可以从Dynamsoft网站下载30天免费试用版。
从扫描仪、网络摄像头和其他兼容TWAIN/WIA/UVC的成像设备捕获图像/文档。
与主流浏览器兼容,包括IE(32位和64位)、Firefox、Chrome、Safari和Opera。
编辑扫描图像:裁剪、更改图像大小、旋转、缩放、擦除等。
将图像上传到各种位置 - 本地文件夹、FTP站点、Web服务器、数据库、SharePoint库等。
增强的安全性:支持SSL和Windows/Forms/Basic身份验证。
支持的图像格式包括BMP、JPEG、PNG、TIFF(单页和多页)和PDF(单页和多页)。
ImageCapture Suite允许从扫描仪、网络摄像头和其他与TWAIN、WIA或UVC兼容的成像设备捕获图像。整个图像扫描过程可以通过硬编码扫描属性来定制,或者可以将IfShowUI设置为true以激活源的用户界面。
function AcquireImageInner() {
if (DW_DWTSourceContainerID == "") {
DWObject.SelectSource();
} else {
DWObject.SelectSourceByIndex(document.getElementById(DW_DWTSourceContainerID).selectedIndex);
}
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;
}
}
DWObject.Resolution = Resolution.value;
DWObject.IfFeederEnabled = document.getElementById("ADF").checked;
DWObject.IfDuplexEnabled = document.getElementById("Duplex").checked;
AppendMessage("Pixel Type: " + DWObject.PixelType + "Resolution: " + DWObject.Resolution + "");
DWObject.IfDisableSourceAfterAcquire = true;
DWObject.AcquireImage();
}
可以通过调用MaxInternetTransferThreads属性来设置通过HTTP(s) POST进行图像上传时使用的线程数量。启用多线程在目标图像大小相对较大时非常有用,例如> 1MB。为了参考,粘贴了下面的表格,反映了多线程如何帮助加快图像上传的性能。数据由Dynamsoft的测试团队提供。
测试文件名 | 文件大小 | 线程=5 | 线程=1 | 提升 |
---|---|---|---|---|
WebTK2.tif | 219 KB | 3550.67 ms | 3113.83 ms | -14.03% |
WebTK.tif | 537 KB | 7173.17 ms | 7249.00 ms | 1.05% |
DWTUp1M.tif | 1.57 MB | 14712.20 ms | 21581.00 ms | 31.83% |
Dynamic Web TWAIN 8.0.1 Trial.exe | 10.1 MB | 91461.20 ms | 128403.40 ms | 28.77% |
Install2.zip | 20.0 MB | 178861.20 ms | 258035.60 ms | 30.68% |
由于算法分配资源需要时间,如上表所示,使用多线程处理小于1MB的小图像并不是一个好主意。除了文件大小之外,当以下情况适用时,最好使用1个线程:
通过为应用程序启用异步,可以在图像上传/下载过程中执行其他操作,例如扫描更多文档。要激活该模式,可以将AsyncMode属性设置为true。
function btnUpload_onclick() {
if (!DW_CheckIfImagesInBuffer()) {
return;
}
var DW_ActionPage = "SaveToFile.aspx";
var i, strHTTPServer, strActionPage, strImageType;
if (document.getElementById("txt_ExtraInfo")) {
DWObject.ClearAllHTTPFormField();
DWObject.SetHTTPFormField("ExtraInfo", document.getElementById("txt_ExtraInfo").value);
}
txt_fileName.className = "";
DWObject.AsyncMode = true;
DWObject.MaxInternetTransferThreads = 5;
DWObject.AllowPluginAuthentication = false;
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;
}
}
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
);
} else {
DWObject.HTTPUploadThroughPostAsMultiPageTIFF(
strHTTPServer,
strActionPage,
uploadfilename
);
}
} 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
);
}
} else {
DWObject.HTTPUploadThroughPostEx(
strHTTPServer,
DWObject.CurrentImageIndexInBuffer,
strActionPage,
uploadfilename,
strImageType
);
}
DW_TempStr = DW_TempStr + "Upload: ";
if (DW_CheckErrorString()) {
}
}
操作页面用于接收并将图像数据传输到Web服务器。
using System;
using System.IO;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class SaveToFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String strExc = "";
try
{
HttpFileCollection files = HttpContext.Current.Request.Files;
HttpPostedFile uploadfile = files["RemoteFile"];
String ExtraInfo = HttpContext.Current.Request.Form["ExtraInfo"];
if (ExtraInfo != "")
{
String extraInfoPath = HttpContext.Current.Request.MapPath(".") + "/" + "extraInfo.txt";
StreamWriter sw = File.CreateText(extraInfoPath);
if (extraInfoPath != null)
{
sw.Write(ExtraInfo);
sw.Close();
}
}
String Path = System.Web.HttpContext.Current.Request.MapPath(".") + "/ImageScanned/";
if (!Directory.Exists(Path))
{
Directory.CreateDirectory(Path);
}
uploadfile.SaveAs(Path + uploadfile.FileName);
}
catch (Exception exc)
{
strExc = exc.ToString();
String strField1Path = HttpContext.Current.Request.MapPath(".") + "/" + "log.txt";
StreamWriter sw1 = File.CreateText(strField1Path);
if (strField1Path != null)
{
sw1.Write(strExc);
sw1.Close();
}
Response.Write(strExc);
}
}
}
完整的源代码可以从文章中下载。
要运行带有试用许可的示例应用程序或自定义Web应用程序并添加更多功能,例如图像编辑、条码识别和OCR,可以从Dynamsoft的网站下载ImageCapture Suite。