dynamic web twain 15.3 使用备忘

1.注册ready事件

Dynamsoft.WebTwainEnv.RegisterEvent('OnWebTwainReady', Dynamsoft_OnReady);
        var DWObject;
        function Dynamsoft_OnReady() {
            alert("ready")
            DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');
            DWObject.RegisterEvent("OnPostTransfer", function () {
                /* Your code goes here*/
            });
        }

2.最小扫描代码

var DWObject;
        function Dynamsoft_OnReady() {
            DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');
        }
        function AcquireImage() {
            if (DWObject) {
                DWObject.SelectSource(function () {
                    DWObject.OpenSource();
                    DWObject.AcquireImage();
                },
                    function () { console.log("SelectSource failed!"); });
            }
        }

3.隐藏界面自动扫描

var DWObject;
        function Dynamsoft_OnReady() {
            DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');
        }
        function AcquireImage() {
            if (DWObject) {
                DWObject.SelectSourceByIndex(0);
                DWObject.OpenSource();
                DWObject.IfShowUI = false;
                DWObject.AcquireImage();
            }
        }

4.枚举出所有设备并自动选择设置扫描

var DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');
            if (DWObject) {
                DWObject.OpenSourceManager();
                var sourceFound = false;
                for(var i= 0;i < DWObject.SourceCount;i++)
                {
                    var sourceName = DWObject.SourceNameItems(i);
                    if(sourceName == "HUAGOSCAN G300 Linux TWAIN")
                    {
                        DWObject.SelectSourceByIndex(i);
                        sourceFound = true;
                        break;
                    }
                }

                if(!sourceFound)
                {
                    alert("source not found!");
                    return;   
                }

                DWObject.IfShowUI = false;
                DWObject.AcquireImage();

5.怎么设置只扫描一张

var DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');
            DWObject.IfDisableSourceAfterAcquire = true;

            DWObject.startScan({
                scanner: "HUAGOSCAN G300 Linux TWAIN",
                ui: {
                    bShowUI: false
                },
                settings: {
                    bFeeder: true,
                    bDuplex: false,
                    pixelType: EnumDWT_PixelType.TWPT_GRAY,
                    resolution: 200
                },
                moreSettings:{
                    nXferCount: 1,  //设置只扫描一张
                }
            });

6.隐藏扫描计数器界面

var DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');
            DWObject.IfDisableSourceAfterAcquire = true;

            DWObject.startScan({
                scanner: "HUAGOSCAN G300 Linux TWAIN",
                ui: {
                    bShowUI: false,
                    bShowIndicator: false //隐藏扫描计数器界面
                },
                settings: {
                    bFeeder: true,
                    bDuplex: false,
                    pixelType: EnumDWT_PixelType.TWPT_GRAY,
                    resolution: 200
                },
                moreSettings:{
                    nXferCount: 2,
                }
            });

7.如何停止正在进行的扫描,貌似不太稳定,这样操作会导致下次使用一直提示设备被占用

DWObject.CloseWorkingProcess();
DWObject.CloseSource();
DWObject.DeleteAllImages();

8.如何让我的界面在扫描进度条之上?

扫描进度条的z-index是500001, 设置比这个大就行

9.如何获取图片信息

var DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');
            if (DWObject) {
                DWObject.RegisterEvent('OnPostTransferAsync', function (outputInfo) {
                    //console.log("an image scanned")
                    // 获取扫描仪返回的图像数据

                    var imageId = outputInfo.imageId;
                    var imageInfo = outputInfo.imageInfo;
                    var extendedImageInfo = outputInfo.extendedImageInfo;
                    debugger
                    // 处理图像数据,例如将其发送到服务器保存
                    console.log(imageInfo);
                });
                DWObject.SelectSourceByIndex(0);
                var OnAcquireImageSuccess = OnAcquireImageFailure = function () {
                    DWObject.CloseSource();
                };
                DWObject.OpenSource();
                DWObject.IfShowUI = false;
                DWObject.TransferMode = EnumDWT_TransferMode.TWSX_NATIVE;
                DWObject.AcquireImage();
            }

 一个完整的扫描 demo:

<!DOCTYPE html>
<html>

<head>
    <title>ScanAll</title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <script type="text/javascript" src="Resources/dynamsoft.webtwain.initiate.js"></script>
    <script type="text/javascript" src="Resources/dynamsoft.webtwain.config.js"></script>
</head>

<body>
    <div style="display:none;">
        <div id="dwtcontrolContainer"></div>
    </div>
    <div id="result">
        <ul id="images" style="list-style-position: inside;"></ul>
    </div>
    <input type="button" value="开始扫描" onclick="AcquireImage()" />
    <script type="text/javascript">
        var DWObject;
        Dynamsoft.WebTwainEnv.RegisterEvent('OnWebTwainReady', Dynamsoft_OnReady);
        function Dynamsoft_OnReady() {
            DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');
        }
        var images = new Array();
        function AcquireImage() {
            DWObject.startScan({
                transferMode: EnumDWT_TransferMode.TWSX_NATIVE,
                scanner: "HUAGOSCAN G300 Linux TWAIN",
                ui: {
                    bShowUI: false,
                    bShowIndicator: false, //隐藏扫描计数器界面
                    bShowProgressBar: false
                },
                settings: {
                    bFeeder: true,
                    bDuplex: false,
                    pixelType: EnumDWT_PixelType.TWPT_GRAY,
                    resolution: 200,
                    bDuplex: true, //whether to enable duplex
                },
                moreSettings: {
                    nXferCount: 6, //扫描张数,双面扫描须设置为偶数
                    pageSize: EnumDWT_CapSupportedSizes.TWSS_A4,
                },
                funcScanStatus: function (status) {
                    if (status.bScanCompleted) {
                        alert("扫描完成!");
                        return;
                    }
                    if (status.event == "postTransfer") {
                        var pageNum = status.result.currentPageNum;

                        //获取并在页面显示图片
                        DWObject.ConvertToBase64([pageNum - 1], EnumDWT_ImageType.IT_PNG,
                            function (base64Result, newIndices, enumImageType, _arguments) {
                                var length = base64Result.getLength();
                                var src = "data:image/png;base64," + base64Result.getData(0, length);
                                const img = document.createElement("img");
                                img.style = "border:10px solid black;"
                                img.src = src;
                                const li = document.createElement("li");
                                li.style = "vertical-align: top;";
                                li.innerHTML = "<h2>第" + (parseInt(newIndices) + 1) + "页</h2>" + img.outerHTML;
                                document.getElementById("images").appendChild(li);
                            },
                            function (errorCode, errorString, newIndices, enumImageType, _arguments) {
                                alert("转换图片为base64时发生错误:" + errorString);
                            });

                        //上传图片
                        DWObject.HTTPUpload('SaveToFile.aspx', [pageNum - 1], EnumDWT_ImageType.IT_PNG, EnumDWT_UploadDataFormat.Binary,
                            (pageNum - 1) + '.png', OnHttpUploadSuccess, OnHttpUploadFailure);
                        function OnHttpUploadSuccess(httpResponse, newIndices, _arguments) {
                            console.log("HTTPResponseString: " + httpResponse);
                        }
                        function OnHttpUploadFailure(errorCode, errorString, httpResponse, newIndices, _arguments) {
                            alert("ErrorCode: " + errorCode + "ErrorString: " +
                                errorString + "HTTPResponseString: " + httpResponse);
                        }
                    }
                }
            });
            DWObject.CloseSource();
        }
    </script>
</body>

</html>
 
SaveToFile.aspx 是文件接收端C#代码,其它语言照样写就可以
<%@ Page Language="C#" %>
<%
    try
    {
        String strImageName;
        HttpFileCollection files = HttpContext.Current.Request.Files;
        HttpPostedFile uploadfile = files["RemoteFile"];
        strImageName = uploadfile.FileName;

        uploadfile.SaveAs(Server.MapPath(".") + "\\UploadedImages\\" + strImageName);

    }
    catch
    {
    }
%>

 

posted on 2023-03-07 10:16  空明流光  阅读(176)  评论(0编辑  收藏  举报

导航