Node.js+Web TWAIN,實現Web文檔掃描和映像上傳

來源:互聯網
上載者:User

標籤:style   http   io   ar   os   sp   for   strong   on   

通過Dynamic Web TWAIN SDK和Node.js的組合,只需要幾行代碼就可以實現在瀏覽器中控制掃描器,擷取映像後上傳到遠程伺服器。

原文:Document Imaging and Uploading With Dynamic Web TWAIN and Node.js

下載安裝
  • Dynamic Web TWAIN download

  • Node.js download

通過Node.js建立server

建立工程目錄,開啟cmd.exe進入到工程目錄,安裝下面兩個Node.js模組:

npm install [email protected]npm install express

建立server.js,初始化:

var formidable = require(‘formidable‘);var util = require(‘util‘);var express = require(‘express‘);var fs = require(‘fs‘);var app = express();

把靜態資源,比片,css等,都載入進來:

app.use(express.static(__dirname, ‘/public‘));

要實現跨域訪問,需要在header裡添加許可權,如果不添加,只能local訪問:

app.use(function(req, res, next) {      res.header("Access-Control-Allow-Origin", "*");       res.header("Access-Control-Allow-Methods","PUT, POST, GET, DELETE, OPTIONS");      res.header("Access-Control-Allow-Headers","X-Requested-With, content-type");      res.header("Access-Control-Allow-Credentials", true);      next();  });

在POST請求中通過formidable解析資料:

app.post(‘/upload‘, function(req, res) {    var form = new formidable.IncomingForm();    form.parse(req, function(err, fields, files) {        // console.log(util.inspect({        //     fields: fields,        //     files: files        // }));         fs.readFile(files.RemoteFile.path, function(err, data) {            // save file from temp dir to new dir            var newPath = __dirname + "/uploads/" + files.RemoteFile.name;            fs.writeFile(newPath, data, function(err) {                if (err) throw err;                console.log(‘file saved‘);                  res.end();            });        });    });})

設定好IP和連接埠:

var server = app.listen(2014, function() {    var host = server.address().address;    var port = server.address().port;    console.log(‘listening at http://%s:%s‘, host, port);})
通過Dynamic Web TWAIN建立client

建立一個網頁,包含一個div和兩個button:

<html>    <head>        <title>Document Imaging & Uploading</title>        <script src="/Resources/dynamsoft.webtwain.initiate.js"></script>        <script src="/Resources/dynamsoft.webtwain.config.js"></script>    </head>    <body>        <div id="dwtcontrolContainer"></div>        <input type="button" value="Acquire" onclick="AcquireImage();" />        <input id="btnUpload" type="button" value="Upload Image" onclick="btnUpload_onclick()">    </body></html>

這裡需要把Web TWAIN SDK安裝目錄下的Resources目錄拷貝過來。

加幾行代碼就可以掃描了:

function AcquireImage(){     DWObject.IfShowUI = false;     DWObject.SelectSource();     DWObject.OpenSource();     DWObject.AcquireImage();}

到這裡可以先測試下掃描能否正常工作。接下來實現遠程上傳:

function btnUpload_onclick() {       DWObject.HTTPPort = 2014;       var CurrentPathName = unescape(location.pathname); // get current PathName in plain ASCII       var CurrentPath = CurrentPathName.substring(0, CurrentPathName.lastIndexOf("/") + 1);       var strActionPage = CurrentPath + "upload";       var strHostIP = "localhost"; // modify the IP for cross-domain access       var sFun = function(){           alert(‘successful‘);       }, fFun = function(){           alert(‘failed‘);       };       DWObject.HTTPUploadThroughPostEx(       strHostIP,       DWObject.CurrentImageIndexInBuffer,       strActionPage,       "test.jpg",       1,// JPEG       sFun, fFun       );}

測試下。命令列啟動server:

node server.js

開啟http://localhost:2014就可以玩了。

源碼

https://github.com/DynamsoftRD/nodejs-dwt

git clone https://github.com/DynamsoftRD/nodejs-dwt.git


Node.js+Web TWAIN,實現Web文檔掃描和映像上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.