diff --git a/lib/tesseract.js b/lib/tesseract.js index db9729a..6a2e58e 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,42 @@ 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(); + } + callback('An error has occured', null); + }); + }, + /** * Runs Tesseract binary with options * @@ -138,3 +176,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(); + }); + + }); + +});