From 1e38efd764ff65260852e86d504f0331ddf1e522 Mon Sep 17 00:00:00 2001 From: andyvuong Date: Mon, 4 Apr 2016 19:13:44 -0500 Subject: [PATCH 1/2] Add stream processing function and test --- lib/tesseract.js | 38 ++++++++++++++++++++++++++++++++++++++ test/tesseract.js | 17 +++++++++++++++-- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/lib/tesseract.js b/lib/tesseract.js index db9729a..9c9f719 100644 --- a/lib/tesseract.js +++ b/lib/tesseract.js @@ -10,6 +10,8 @@ var tmpdir = require('os').tmpdir(); // let the os take care of removing zombie var uuid = require('node-uuid'); var path = require('path'); var glob = require("glob"); +var spawn = require('child_process').spawn; + var Tesseract = { @@ -32,6 +34,41 @@ var Tesseract = { */ outputEncoding: 'UTF-8', + /** + * Runs Tesseract binary with options + * + * @param {String} stream + * @param {Object} options to pass to Tesseract binary + * @param {Function} callback + */ + processStream: function(stream, options, callback) { + if (typeof options === 'function') { + callback = options; + options = null; + } + + options = utils.merge(Tesseract.options, options); + + var tesseract = spawn('tesseract', ['stdin', 'stdout', '-l', options.l, '-psm', options.psm]) + stream.pipe(tesseract.stdin); + + var data = ''; + tesseract.stdout.on('data', function(chunk) { + data += chunk; + }); + tesseract.stderr.on('data', function(chunk) { + data += chunk; + }); + tesseract.on('exit', function(chunk) { + callback(null, data); + }); + tesseract.on('error', function(chunk) { + if (tesseract) { + tesseract.kill(); + } + }); + }, + /** * Runs Tesseract binary with options * @@ -138,3 +175,4 @@ process.addListener('exit', function _exit(code) { * Module exports. */ module.exports.process = Tesseract.process; +module.exports.processStream = Tesseract.processStream; \ No newline at end of file diff --git a/test/tesseract.js b/test/tesseract.js index bd64c53..208d240 100644 --- a/test/tesseract.js +++ b/test/tesseract.js @@ -2,6 +2,7 @@ var tesseract = require('../lib/tesseract'); var should = require('should'); +var fs = require('fs'); describe('process', function(){ @@ -14,6 +15,18 @@ describe('process', function(){ done(); }); - }) -}) + }); + + it('should return the string "node-tesseract"', function(done){ + + var testImage = __dirname + '/test.png'; + + tesseract.processStream(fs.createReadStream(testImage), function(err, text) { + text.trim().should.equal('node-tesseract'); + done(); + }); + + }); + +}); From 3e6658d3b73922725cfd42cdbd6ccc82ee2d0097 Mon Sep 17 00:00:00 2001 From: andyvuong Date: Tue, 5 Apr 2016 19:50:10 -0500 Subject: [PATCH 2/2] Add missing callback --- lib/tesseract.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/tesseract.js b/lib/tesseract.js index 9c9f719..6a2e58e 100644 --- a/lib/tesseract.js +++ b/lib/tesseract.js @@ -66,6 +66,7 @@ var Tesseract = { if (tesseract) { tesseract.kill(); } + callback('An error has occured', null); }); },