分离扫描文档方法(1) —— Dynamic Web TWAIN:如何使用空白页作为扫描文档的分隔器

应用场景

您希望在网页中连接扫描仪,在网页中进行文档扫描。当您一次性扫描多个文档时,每个文档之间有一页空白页将它们分开,此时您希望在扫描完全部文档后单独保存每组文档。

解决方案

使用Dynamic Web TWAIN SDK,可以快速实现网页文档扫描的同时利用空白页进行文档分隔。

实现步骤

Step 1:下载并安装Dynamic Web TWAIN 30天免费试用版本

申请下载链接:https://www.damingsoft.com/products/dwt-register.aspx

Step 2:构建网页扫描页面

安装完成后可以在C:\Program Files (x86)\Dynamsoft\Dynamic Web TWAIN SDK {版本号} Trial\Samples\Getting Started中找到一个UseEvent.html的sample页面。双击打开它即可实现最简单的网页扫描。

分离扫描文档方法(1) —— Dynamic Web TWAIN:如何使用空白页作为扫描文档的分隔器_第1张图片

如果您想自己构建一个网页扫描页面,可参考文档:https://www.damingsoft.com/docs/dwt/Dev-Guide/Build-the-Hello-World-Scan-Page.html

Step 3:判断空白页并保存文档

为了做到这一点,我们将扫描所有文档,然后使用接口IsBlankImageExpress(index)来检测它是否为空白。 如果该页为空白,我们将从缓冲区中删除它并保存上一组文档。 为了完成此任务,我们将使用事件OnPostAllTransfers,它在所有扫描完成后触发,以检查是否有任何页面为空。

此时,我们可以打开UseEvent.html,将其代码稍加改动。function Dynamsoft_OnReady改动如下:

        function Dynamsoft_OnReady() {
            DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');    // Get the Dynamic Web TWAIN object that is embeded in the div with id 'dwtcontrolContainer'
            //DWObject.Width = 270;       // Set the width of the Dynamic Web TWAIN Object
            //DWObject.Height = 400;      // Set the height of the Dynamic Web TWAIN Object
            if (DWObject) {
                DWObject.RegisterEvent('OnPostAllTransfers', CheckBlankPage);      // Register OnPostAllTransfers event. This event fires when all pages have been scanned and transferred                    
            }
        }

添加function CheckBlankPage:

function CheckBlankPage() { //Function for checking a blank page, called when OnPostAllTransfers is triggered
    if (DWObject) {//Ensure there is a DWObject
        var startindex = 0;
        for (var i = 0; i < DWObject.HowManyImagesInBuffer; i++) {//Go through each image in the buffer.
            if (DWObject.IsBlankImageExpress(i)) {
                DWObject.RemoveImage(i);// remove the blank page from the buffer.
                if (i != 0) {
                    i--; //decrement i for the removed image
                    DWObject.SelectedImagesCount = (i - startindex + 1); // set how many images are selected
                    for (var j = 0; j < DWObject.SelectedImagesCount; j++) { //loop to select all images from previous blank to current
                        DWObject.SetSelectedImageIndex(j, j + startindex);
                    }
                    if (DWObject.SelectedImagesCount > 0) { //save images as long as there are some in the selection
                        DWObject.IfShowFileDialog = true;
                        var randomId = (Math.floor(Math.random() * 1000 + 1)).toString();
                        DWObject.SaveSelectedImagesAsMultiPagePDF(randomId + ".pdf");//PLEASE CHANGE THIS FILE PATH
                    }
                    startindex = i + 1; //set the start index for next search 1 higher than current page
                }
            }
            else if (i == DWObject.HowManyImagesInBuffer - 1) {//the last few images are not blank
                DWObject.SelectedImagesCount = (i - startindex + 1); // set how many images are selected
                for (var j = 0; j < DWObject.SelectedImagesCount; j++) { //loop to select all images from previous blank to current
                    DWObject.SetSelectedImageIndex(j, j + startindex);
                }
                if (DWObject.SelectedImagesCount > 0) { //save images as long as there are some in the selection
                    DWObject.IfShowFileDialog = true;
                    DWObject.SaveSelectedImagesAsMultiPagePDF("AllInOne.pdf");//PLEASE CHANGE THIS FILE PATH
                }
            }
        }
    }
}

如果您的应用程序很难检测到空白页面(空白页边缘周围有一些非空白点),或者您使用的是彩色页面,则可能需要使用页面标准偏差。 您可以改为使用以下代码段:

function CheckBlankPage(){ //Function for checking a blank page, called when OnPostAllTransfers is triggered 
    if(DWObject){//Ensure there is a DWObject 
        for(var i = 0; i < DWObject.HowManyImagesInBuffer;i++){//Go through each image in the buffer. 
            if(DWObject.IsBlankImageExpress(i)){}//Make Blank Image run, but do not use the result 
            if(DWObject.BlankImageCurrentStdDev < 15.0){//set a standard deviation for the program to use
                //...... same as above code
            } 
        } 
    } 
}

总结

通过以上方法,您可以轻松地实现网页文档扫描,并使用文档间空白页进行文档分隔。

当然,有的应用场景中分隔文档的标志不是空白页而是文档中的条码或者二维码。这也是文档分离的方法之一,并可以通过Dynamic Web TWAIN及其附加组件Barcode Reader来实现。我们会在之后的文章中介绍。

你可能感兴趣的:(Dynamic,Web,TWAIN,半原创,文档翻译)