Browse Source

Add test cases for image Buffer

pull/305/head
Jerome Wu 6 years ago
parent
commit
b7b2148b71
  1. 1
      docs/image-format.md
  2. 3
      scripts/test-helper.js
  3. 9
      src/node/index.js
  4. 19
      tests/recognize.test.js

1
docs/image-format.md

@ -11,3 +11,4 @@ On a browser, an image can be:
In Node.js, an image can be In Node.js, an image can be
- a path to a local image - a path to a local image
- a Buffer storing binary image

3
scripts/test-helper.js

@ -1,3 +1,4 @@
global.expect = require('expect.js'); global.expect = require('expect.js');
global.fetch = require('node-fetch'); global.fs = require('fs');
global.path = require('path');
global.Tesseract = require('../src'); global.Tesseract = require('../src');

9
src/node/index.js

@ -25,6 +25,7 @@ const readFile = util.promisify(fs.readFile);
* @access public * @access public
* @param {string} image - image source, supported formats: * @param {string} image - image source, supported formats:
* string: URL string or file path * string: URL string or file path
* buffer: image buffer
* @returns {array} binary image in array format * @returns {array} binary image in array format
*/ */
const loadImage = (image) => { const loadImage = (image) => {
@ -34,8 +35,12 @@ const loadImage = (image) => {
}) })
.then(resp => resp.data); .then(resp => resp.data);
} }
if (Buffer.isBuffer( image) ) return new Promise(function(resolve, reject) { resolve(image); });
else return readFile(image); if (Buffer.isBuffer(image)) {
return Promise.resolve(image);
}
return readFile(image);
}; };
/* /*

19
tests/recognize.test.js

@ -39,7 +39,7 @@ describe('recognize()', () => {
}).timeout(30000) }).timeout(30000)
)); ));
}); });
describe('should read bmp, jpg, png and pbm format images', () => { describe('should read bmp, jpg, png and pbm format images', () => {
FORMATS.forEach(format => ( FORMATS.forEach(format => (
it(`support ${format} format`, (done) => { it(`support ${format} format`, (done) => {
@ -116,6 +116,21 @@ describe('recognize()', () => {
)); ));
}); });
(isBrowser ? describe.skip : describe)('should recognize image in Buffer (Node.js only)', () => {
FORMATS.forEach(format => (
it(`support ${format} format`, (done) => {
const worker = getWorker();
worker
.recognize(fs.readFileSync(path.join(__dirname, 'assets', 'images', `simple.${format}`)))
.then(({ text }) => {
expect(text).to.be(SIMPLE_TEXT);
worker.terminate();
done();
});
}).timeout(10000)
));
});
(isBrowser ? describe : describe.skip)('should read image from img DOM element (browser only)', () => { (isBrowser ? describe : describe.skip)('should read image from img DOM element (browser only)', () => {
FORMATS.forEach(format => ( FORMATS.forEach(format => (
it(`support ${format} format`, (done) => { it(`support ${format} format`, (done) => {
@ -133,7 +148,7 @@ describe('recognize()', () => {
}).timeout(10000) }).timeout(10000)
)); ));
}); });
(isBrowser ? describe : describe.skip)('should read image from video DOM element (browser only)', () => { (isBrowser ? describe : describe.skip)('should read image from video DOM element (browser only)', () => {
FORMATS.forEach(format => ( FORMATS.forEach(format => (
it(`support ${format} format`, (done) => { it(`support ${format} format`, (done) => {

Loading…
Cancel
Save