ITPub博客

首页 > 大数据 > 数据挖掘 > 使用Dynamsoft存储和检索扫描图像

使用Dynamsoft存储和检索扫描图像

数据挖掘 作者:chnovsky 时间:2013-11-20 11:18:00 0 删除 编辑

存储和检索数字化文档是任何文档管理工作流过程的一个重要特征。本文我们将介绍如何使用ASP.NET Web程序中的文档扫描功能将扫描文件保存为PDF文档,并保存至SQL Server数据库中。

工具/原料

  • Dynamic Web TWAIN

方法/步骤

  1. 1

    存储和检索数字化文档是任何文档管理工作流过程的一个重要特征。本文我们将介绍如何使用ASP.NET Web程序中的文档扫描功能将扫描文件保存为PDF文档,并保存至SQL Server数据库中。

    在本文中我们将使用Dynamic Web TWAIN来加快文档扫描,上传和显示功能的开发进程。

  2. 2

    Dynamic Web TWAIN主要功能:

    1、Windows或Mac上兼容Firefox,Chrome,Opera和Safari等主流浏览器

    2、支持从扫描仪或者其他TWAIN兼容设备中扫描图像

    3、BMP,JPEG,PNG,单/多页 PDF和单/多页TIFF

    4、支持HTTPS上传

    5、支持cookie和会话的集成

  3. 3

    文件扫描

    由于Dynamic Web TWAIN是一个客户端SDK,我们将使用JavaScript来调用它的方法/属性。有了扫描识别工具Dynamic Web TWAIN,你可以自定义扫描设置,如分辨率,像素类型,亮度,对比度,页面大小等。本文,我们将重点介绍如何从SQL Server中储存和检索图像。为此,我们将只包括一个简单的扫描过程。

    function acquireImage() {

    if (_divDWTSourceContainerID == "")

    DWObject.SelectSource();

    else

    DWObject.SelectSourceByIndex(document.getElementById(_divDWTSourceContainerID).selectedIndex); //select a TWAIN scanner

    DWObject.CloseSource(); //make sure the source is closed before using it

    DWObject.OpenSource();

    DWObject.IfShowUI = document.getElementById("ShowUI").checked; //show or hide the user interface of the TWAIN scanner

     

    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 acquired images, B/W, gray or color

    DWObject.Resolution = document.getElementById("Resolution").value; //set the resolution

    DWObject.IfFeederEnabled = document.getElementById("ADF").checked; //scan images from auto feeder

    DWObject.IfDuplexEnabled = document.getElementById("Duplex").checked; //enable duplex scanning

    appendMessage("Pixel Type: " + DWObject.PixelType + "
    Resolution: " + DWObject.Resolution + "
    ");

     

    DWObject.IfDisableSourceAfterAcquire = true;

    DWObject.AcquireImage(); //start document scanning

    }

  4. 4

    将扫描图像作为个多页PDF保存至SQL Server

    扫描完成后,你可以将图像保存为多种格式:BMP、PNG、JPG、TIF、PDF、多页PDF或多页TIF。在这个例子中,我们将扫描图像保存为一个多页PDF文件。代码如下所示:

    function btnUpload_onclick() {

    if (!checkIfImagesInBuffer()) {

    return;

    }

    var i, strHTTPServer, strActionPage, strImageType;

    _txtFileName.className = "";

    if (!strre.test(_txtFileName.value)) {

    _txtFileName.className += " invalid";

    _txtFileName.focus();

    appendMessage("Please input file name.
    Currently only English names are allowed.
    ");

    return;

    }

    //DWObject.MaxInternetTransferThreads = 5;

    strHTTPServer = _strServerName;

    DWObject.HTTPPort = _strPort;

    var CurrentPathName = unescape(location.pathname); // get current PathName in plain ASCII

    var CurrentPath = CurrentPathName.substring(0, CurrentPathName.lastIndexOf("/") + 1);

    strActionPage = CurrentPath + _strActionPage; // the aspx page for receiving image data on the server side

    var redirectURLifOK = CurrentPath + "online_demo_list.aspx";

    var uploadfilename = _txtFileName.value + "." + document.getElementsByName("ImageType").item(i).value;

     

    DWObject.HTTPUploadAllThroughPostAsPDF(

    strHTTPServer,

    strActionPage,

    uploadfilename

    ); //upload images as multi-page PDF file

     

    _strTempStr = _strTempStr + "Upload: ";

    if (checkErrorString()) {

    if (strActionPage.indexOf("SaveToFile") != -1)

    alert(DWObject.ErrorString)//if save to file.

    else

    window.location = redirectURLifOK;

    }

    }

  5. 5

    Action Page- SaveToDB.aspx

    动作页面用于接收来自扫描图像的图像数据。

    <%@ Page Language="C#"%>

    <%

    try

    {

    String strImageName;

    int iFileLength;

    HttpFileCollection files = HttpContext.Current.Request.Files;

    HttpPostedFile uploadfile = files["RemoteFile"];

    strImageName = uploadfile.FileName;

    iFileLength = uploadfile.ContentLength;

     

    Byte[] inputBuffer = new Byte[iFileLength];

    System.IO.Stream inputStream;

     

    inputStream = uploadfile.InputStream;

    inputStream.Read(inputBuffer, 0, iFileLength);

     

    String strConnString;

     

    strConnString = Common.DW_ConnString;

     

    System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(strConnString);

     

    String SqlCmdText = "INSERT INTO " + Common.DW_SaveTable + " (strImageName,imgImageData) VALUES (@ImageName,@Image)";

    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;

     

    sqlConnection.Open();

    sqlCmdObj.ExecuteNonQuery();

    sqlConnection.Close();

    }

    catch

    {

    }

    %>

  6. 6

    从Web页面上显示数据库中检索PDF文件

    Online_demo_view.aspx用于显示PDF文档

    setTimeout(function () {

    var CurrentPathName = unescape(location.pathname); // get current PathName in plain ASCII

    var CurrentPath = CurrentPathName.substring(0, CurrentPathName.lastIndexOf("/") + 1);

    var strActionPage = CurrentPath + "online_demo_download.aspx"; //the ActionPage's file path

     

    strHTTPServer = location.hostname;

    DWObject.HTTPPort = location.port==""?80:location.port;

    var downloadsource = strActionPage +

    "?iImageIndex=<%=strImageID%>&ImageName=<%=strImageName%>&ImageExtName=<%=strImageExtName%>";

    DWObject.HTTPDownloadEx(strHTTPServer, downloadsource,<%=strImageFileType %>);

    }, 500);

  7. 7

    online_demo_download.aspx用于检索数据库中的图像

    <%@ Page Language="C#"%>

     

    <%

    String strExc = "";

    try

    {

    //Get the image data from the database

    HttpRequest request = HttpContext.Current.Request;

     

    String strImageName;

    String strImageExtName;

    String strImageID;

     

    strImageName = request["ImageName"];

    strImageExtName = request["ImageExtName"];

    strImageID = request["iImageIndex"];

     

    String strConnString;

     

    strConnString = Common.DW_ConnString;

     

    System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(strConnString);

    System.Data.SqlClient.SqlCommand sqlCmdObj = new System.Data.SqlClient.SqlCommand("SELECT imgImageData FROM " +

    Common.DW_SaveTable + " WHERE iImageID= " + strImageID, sqlConnection);

     

    sqlConnection.Open();

     

    System.Data.SqlClient.SqlDataReader sdrRecordset = sqlCmdObj.ExecuteReader();

     

    sdrRecordset.Read();

     

    long iByteLength;

    iByteLength = sdrRecordset.GetBytes(0, 0, null, 0, int.MaxValue);

     

    byte[] byFileData = new byte[iByteLength];

     

    sdrRecordset.GetBytes(0, 0, byFileData, 0, Convert.ToInt32(iByteLength));

     

    sdrRecordset.Close();

    sqlConnection.Close();

     

    sdrRecordset = null;

    sqlConnection = null;

     

    Response.Clear();

    Response.Buffer = true;

     

    if (strImageExtName == "bmp")

    {

    Response.ContentType = "image/bmp";

    }

    else if (strImageExtName == "jpg")

    {

    Response.ContentType = "image/jpg";

    }

    else if (strImageExtName == "tif")

    {

    Response.ContentType = "image/tiff";

    }

    else if (strImageExtName == "png")

    {

    Response.ContentType = "image/png";

    }

    else if (strImageExtName == "pdf")

    {

    Response.ContentType = "application/pdf";

    }

     

    try

    {

    String fileNameEncode;

    fileNameEncode = HttpUtility.UrlEncode(strImageName, System.Text.Encoding.UTF8);

    fileNameEncode = fileNameEncode.Replace("+", "%20");

    String appendedheader = "attachment;filename=" + fileNameEncode;

    Response.AppendHeader("Content-Disposition", appendedheader);

     

    Response.OutputStream.Write(byFileData, 0, byFileData.Length);

    }

    catch (Exception exc)

    {

    strExc = exc.ToString();

    DateTime d1 = DateTime.Now;

    string logfilename = d1.Year.ToString() + d1.Month.ToString() + d1.Day.ToString() +

    d1.Hour.ToString() + d1.Minute.ToString() + d1.Second.ToString() + "log.txt";

    String strField1Path = HttpContext.Current.Request.MapPath(".") + "/" + logfilename;

    if (strField1Path != null)

    {

    System.IO.StreamWriter sw1 = System.IO.File.CreateText(strField1Path);

    sw1.Write(strExc);

    sw1.Close();

    }

    Response.Flush();

    Response.Close();

    }

    }

    catch (Exception ex)

    {

    strExc = ex.ToString();

    DateTime d1 = DateTime.Now;

    string logfilename = d1.Year.ToString() + d1.Month.ToString() + d1.Day.ToString() +

    d1.Hour.ToString() + d1.Minute.ToString() + d1.Second.ToString() + "log.txt";

    String strField1Path = HttpContext.Current.Request.MapPath(".") + "/" + logfilename;

    if (strField1Path != null)

    {

    System.IO.StreamWriter sw1 = System.IO.File.CreateText(strField1Path);

    sw1.Write(strExc);

    sw1.Close();

    }

    Response.Write(strExc);

    }

    %>

    END

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9856513/viewspace-1110672/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论