65.9K
CodeProject 正在变化。 阅读更多。
Home

Web 应用中的图像扫描和上传

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2013年1月17日

CPOL

3分钟阅读

viewsIcon

46158

downloadIcon

1586

Web 应用中的图像扫描和上传

在您的 Web 应用程序中扫描完文档后,下一步是编辑和保存图像,可能还会附带一些其他额外信息,例如患者 ID 和评论。 在图像上传过程中,您可能需要解决以下问题

  • 如何处理图像格式;
  • 是否涉及多页;
  • 如果我需要与扫描的图像一起上传额外的文本怎么办;
  • 如何确保图像数据在网络上的安全性;
  • 在哪里存储图像/文档。

我已随本文上传了一个简单的示例。 将应用程序部署到您的 Web 服务器后,您可以自己试用图像扫描和上传的功能。 该示例基于 Dynamsoft 提供的第三方组件 – Dynamic Web TWAIN 进行开发。 您可以继续阅读以了解有关源代码的更多信息以及组件如何解决上述问题。

 

关于 Dynamic Web TWAIN

Dynamsoft 的 Dynamic Web TWAIN 是一款浏览器插件,可让您从扫描仪和其他符合 TWAIN 标准的设备扫描图像。 扫描后,您可以编辑并将图像上传到您的本地文件夹、Web 服务器、FTP 站点、SharePoint 和数据库,而无需离开您的浏览器。 您可以查看 在线演示,以了解图像采集 SDK 的大致情况。

主要特点

  • 与包括 IE、Firefox、Chrome、Safari 和 Opera 在内的主流浏览器兼容
  • 能够从扫描仪和其他 TWAIN 兼容设备扫描图像
  • 支持 BMP、JPEG、PNG、单页/多页 PDF 和单页/多页 TIFF
  • 支持 SSL
  • 支持 cookie 和会话
  • 支持与图像一起上传额外的文本

图像扫描

能够与主流浏览器一起使用并能够以整洁的方式从扫描仪扫描图像是 Dynamsoft 提供的出色功能。 由于我们在这篇文章中关注图像上传,因此我提供了一个简单的示例,它允许您一次扫描多张图像。

function DWT_AcquireImage() {
    if (DWT_DWTSourceContainerID == "")
        WebTWAIN.SelectSource();
    else
        WebTWAIN.SelectSourceByIndex(document.getElementById(DWT_DWTSourceContainerID).selectedIndex);
    WebTWAIN.CloseSource();
    WebTWAIN.OpenSource();

    WebTWAIN.IfFeederEnabled = true;
    WebTWAIN.IfShowUI = false;
    WebTWAIN.PixelType = 2;
    WebTWAIN.Resolution =200;
    WebTWAIN.AcquireImage();
}

IfFeederEnabled 设置为 true,文档将从自动文档进纸器 (ADF) 扫描。 除了示例中硬编码的像素类型和分辨率之外,还有更多属性,例如页面大小和亮度,可帮助您标准化(或自定义)图像特征。

上传扫描的图像

以下示例显示了如何将扫描的图像上传到您的 Web 服务器。 根据您的要求,您还可以将图像上传到数据库、本地文件夹,甚至通过电子邮件发送给某人。

是否通过 SSL 上传图像

IfSSL 设置为 true 时,图像将通过 SSL 上传以保护您的图像数据。

    strHTTPServer = location.hostname;
    if (window.location.protocol != "https:") {
        WebTWAIN.HTTPPort = location.port == "" ? 80 : location.port;
        WebTWAIN.IfSSL = false; // if 55302 is the port number of non-secure port
    }
    else {
        WebTWAIN.HTTPPort = 443;
        WebTWAIN.IfSSL = true; // if 443 is the port number of secure port
    }

上传额外信息

创建一个文本框“txt_Directory”。 使用以下代码行将额外信息与图像一起上传。

WebTWAIN.ClearAllHTTPFormField();
WebTWAIN.SetHTTPFormField("Directory",   document.getElementById("txt_Directory").value);

将扫描的图像上传到您的 Web 服务器

将所有扫描的图像或选定的图像上传到您的 Web 服务器,并以多页 PDF 格式保存它们。

DWT_ScanAndUpload.js

    if (!DWT_CheckIfImagesInBuffer()) {
        return;
    }
    var i, strHTTPServer, strActionPage, strImageType;
    var ID_txt_fileName = document.getElementById("txt_fileName");
    ID_txt_fileName.value = ID_txt_fileName.value.trim();
    ID_txt_fileName.className = "";
    if (!strre.test(ID_txt_fileName.value)) {
        ID_txt_fileName.className += " invalid";
        ID_txt_fileName.focus();
        AppendMessage("Please input file name.Currently only English names are allowed.");
        return;
    }

    var CurrentPathName = unescape(location.pathname); // get current PathName in plain ASCII	
    var CurrentPath = CurrentPathName.substring(0, CurrentPathName.lastIndexOf("/") + 1);
    strActionPage = CurrentPath + "SaveToFile.aspx"; //the ActionPage's file path

    var uploadfilename = ID_txt_fileName.value + ".pdf";

    if ((WebTWAIN.SelectedImagesCount == 1) || (WebTWAIN.SelectedImagesCount == WebTWAIN.HowManyImagesInBuffer)) {
        WebTWAIN.HTTPUploadAllThroughPostAsPDF(
            strHTTPServer,
            strActionPage,
            uploadfilename
        );
    }
    else {
        WebTWAIN.HTTPUploadThroughPostAsMultiPagePDF(
            strHTTPServer,
            strActionPage,
            uploadfilename
        );
    }
    DWT_CheckErrorString();
}

SaveToFile.aspx.cs

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 directoryPath = System.Web.HttpContext.Current.Request.MapPath(".") + "/ImageScanned/" + HttpContext.Current.Request.Form["Directory"] + "/";
            if (!System.IO.Directory.Exists(directoryPath)) System.IO.Directory.CreateDirectory(directoryPath);
            string filePath = directoryPath + uploadfile.FileName;
            uploadfile.SaveAs(filePath);
        }
        catch (Exception exc)
        {
            strExc = exc.ToString();
            String strField1Path = HttpContext.Current.Request.MapPath(".") + "/" + "log.txt";
            if (strField1Path != null)
            {
                StreamWriter sw1 = File.CreateText(strField1Path);
                sw1.Write(strExc);
                sw1.Close();
            }
            Response.Write(strExc);
        }
    }
}

支持的图像格式包括 BMP、JPEG、PNG、(多页) TIFF 和 (多页) PDF。

下载示例

可以在下面找到更多 Dynamic Web TWAIN 的示例
Dynamic Web TWAIN 示例

如果您想评估 Dynamic Web TWAIN,您可以在此处下载免费试用版
Dynamic Web TWAIN 30天免费试用

如果您有任何问题,可以联系我们的支持团队:twainsupport@dynamsoft.com

© . All rights reserved.