致力于图形处理和移动开发。
分类: C#/.net
2013-09-17 10:17:04
在“大数据时代”的今天,越来越多的组织使用ECM企业内容管理系统或者RMS记录管理系统来维护和管理文档。数字化文档并将其储存在中央数据库已经成为多数企业文档管理流程中的重要组成部分。为了实现企业文档管理,我们需要创建一个应用程序从扫描仪或者摄像头来捕获文档图像。本文,我们将通过使用.NET 图像开发包Dynamic .NET TWAIN 来加快应用程序的开发和部署,并达到扫描文档并保存图像至数据库的目的。
并安装.NET 图像开发包Dynamic .NET TWAIN后,在安装目录中,找到"DynamicDotNetTWAIN.dll" under \Bin\v2.0 and \Bin\v4.0,选择适合自己.Net 框架的相应版本。
通过Dynamic .NET TWAIN,仅需简短的几行代码,你便可从TWAIN 设备或者USB/WIA摄像头捕获图像。代码如下:
Public frmCustomizeScan() { InitializeComponent(); dynamicDotNetTwain.SupportedDeviceType = Dynamsoft.DotNet.TWAIN.Enums.EnumSupportedDeviceType.SDT_ALL; // enable capturing images from both scanners and webcams int lngNum; dynamicDotNetTwain.OpenSourceManager(); for (lngNum = 0; lngNum < dynamicDotNetTwain.SourceCount; lngNum++) { cmbSource.Items.Add(dynamicDotNetTwain.SourceNameItems(Convert.ToInt16(lngNum))); // display the available imaging devices } if (lngNum > 0) cmbSource.SelectedIndex = 0; } private void cmdScan_Click(object sender, EventArgs e) { dynamicDotNetTwain.IfAppendImage = true; AcquireImage(); // acquire images } private void AcquireImage() { dynamicDotNetTwain.SelectSourceByIndex(Convert.ToInt16(cmbSource.SelectedIndex)); dynamicDotNetTwain.IfShowUI = chkIfShowUI.Checked; dynamicDotNetTwain.OpenSource(); dynamicDotNetTwain.IfDisableSourceAfterAcquire = true; try { dynamicDotNetTwain.AcquireImage(); } catch (Exception exp) { MessageBox.Show(exp.Message); } }
Dynamic .NET TWAIN提供了HTTP Upload方法,通过该方法,你可以将扫描或捕获的图像上传至Web服务器和数据库。它所支持的文件上传格式包括PDF、TIF、 JPG、 PNG、BMP、多页TIF和PDF等。上传图像时,你可以添加额外的参数并将它们与图像记录一起储存在数据库中。代码如下:
private void BtnUpload_Click(object sender, EventArgs e) { string strActionPage = "Upload.aspx"; // for receiving the uploaded image data on the server side string strFileName = textBox1.Text; string strFileType = textBox2.Text; string strHTTPServer = "localhost"; // the name or the IP of your HTTP Server dynamicDotNetTwain.HTTPPort = 8066; //the port number of the HTTP Server dynamicDotNetTwain.HTTPUserName = "chloe"; //user name for logging into HTTP Server dynamicDotNetTwain.HTTPPassword = "c"; dynamicDotNetTwain.SetHTTPFormField("FileType", strFileType); // pass extra text parameters when uploading image dynamicDotNetTwain.HTTPUploadAllThroughPostAsPDF(strHTTPServer,strActionPage, strFileName+".pdf"); // save the captured images as a multi-page PDF file if (dynamicDotNetTwain.ErrorCode !=ErrorCode.Succeed) { MessageBox.Show(dynamicDotNetTwain.HTTPPostResponseString); } else { MessageBox.Show("PDF saved successfully."); } }
HTTPUploadAllThroughPostAsPDF方法中所涉及到的Upload.aspx 动作页用于接收服务器端的图像数据。因此,一定要在你的Web 服务器中部署该文件,具体做法如下:
<%@ Page Language="c#" AutoEventWireup="false" Debug="True"%> <% try { int iFileLength; HttpFileCollection files = HttpContext.Current.Request.Files; HttpPostedFile uploadfile = files["RemoteFile"]; String strImageName = uploadfile.FileName; String strFileType = System.Web.HttpContext.Current.Request.Form["FileType"]; iFileLength = uploadfile.ContentLength; Byte[] inputBuffer = new Byte[iFileLength]; System.IO.Stream inputStream; inputStream = uploadfile.InputStream; inputStream.Read(inputBuffer,0,iFileLength); String strConnString = "Data Source=192.168.8.211;Initial Catalog=WebTwain;User ID=sa;Pwd=sa"; System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(strConnString); String SqlCmdText = "INSERT INTO tblImage (strImageName,imgImageData, strFileType) VALUES (@ImageName,@Image, @ImageType)"; System.Data.SqlClient.SqlCommand sqlCmdObj = new System.Data.SqlClient.SqlCommand(SqlCmdText, sqlConnection); sqlCmdObj.Parameters.Add("@Image",System.Data.SqlDbType.Binary,iFileLength).Value = inputBuffer; sqlCmdObj.Parameters.Add("@ImageName",System.Data.SqlDbType.VarChar,255).Value = strImageName; sqlCmdObj.Parameters.Add("@ImageType", System.Data.SqlDbType.VarChar, 255).Value = strFileType; sqlConnection.Open(); sqlCmdObj.ExecuteNonQuery(); sqlConnection.Close(); } catch(Exception e) { Response.Write(e.Message); throw; } %>
为了方便起见,你可以下载完整源代码。若运行示例时,出现许可证错误,你可以下载Dynamic .NET TWAIN的有效的。
在运行代码前,确保已在Web服务器上部署了Upload.aspx,并且更新了strHTTPServer, HTTPPort, HTTPUserName 和BtnUpload_Click()中的HTTPPassword。