var formidable = require('formidable'); var util = require('util'); var express = require('express'); var fs = require('fs'); var app = express(); var path = require('path'); var dbr = require('./build/Release/dbr'); var http = require('http'); fs.readFile('./license.txt', 'utf8', function(err, data) { app.use(express.static(__dirname)); 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(); }); var server = app.listen(2016, function() { var host = server.address().address; var port = server.address().port; console.log('listening at %s:%s', host, port); }); });
使用 Formidable 从表单中提取图像数据:
app.post('/upload', function(req, res) { var form = new formidable.IncomingForm(); form.parse(req, function(err, fields, files) { var dir = 'uploads'; fs.mkdir(dir, function(err) { var flag = fields.uploadFlag; var barcodeType = parseInt(fields.barcodetype); console.log('flag: ' + flag); if (flag === '1') { // read barcode image file fs.readFile(files.fileToUpload.path, function(err, data) { // save file from temp dir to new dir var fileName = path.join(__dirname, dir, files.fileToUpload.name); console.log(fileName); fs.writeFile(fileName, data, function(err) { if (err) throw err; }); }); } else { // read barcode image url var tmpFileName = path.join(__dirname, dir, 'tmp.jpg'); var tmp = fs.createWriteStream(tmpFileName); var url = fields.fileToDownload; console.log('url: ' + url); http.get(url, function(response) { response.pipe(tmp); tmp.on('finish', function() { tmp.close(function() { }); }); }); } }); }); });
导入条形码模块用来解析图像文件:
decodeBarcode(res, license, tmpFileName, barcodeType);
运行应用:
node server.js
访问 :2016/index.htm:
以上所述是小编给大家介绍的Node.js 条形码识别程序构建思路详解,希望对大家有所帮助。
您可能感兴趣的文章: