Browse Source

Add base64 image support

pull/305/head
Jerome Wu 6 years ago
parent
commit
5dd382a67a
  1. 2
      docs/image-format.md
  2. 1
      src/browser/b64toU8Array.js
  3. 7
      src/browser/index.js
  4. 3
      src/browser/worker.js
  5. 1
      src/node/b64toU8Array.js
  6. 6
      src/node/index.js
  7. 3
      src/node/worker.js
  8. 22
      tests/recognize.test.js

2
docs/image-format.md

@ -8,7 +8,9 @@ On a browser, an image can be:
- an `img`, `video`, or `canvas` element - an `img`, `video`, or `canvas` element
- a `File` object (from a file `<input>`) - a `File` object (from a file `<input>`)
- a path or URL to an accessible image - a path or URL to an accessible image
- a base64 encoded image fits `data:image\/([a-zA-Z]*);base64,([^"]*)` regexp
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 - a Buffer storing binary image
- a base64 encoded image fits `data:image\/([a-zA-Z]*);base64,([^"]*)` regexp

1
src/browser/b64toU8Array.js

@ -0,0 +1 @@
module.exports = s => new Uint8Array(atob(s).split('').map(c => c.charCodeAt(0)));

7
src/browser/index.js

@ -10,6 +10,7 @@
const check = require('check-types'); const check = require('check-types');
const resolveURL = require('resolve-url'); const resolveURL = require('resolve-url');
const axios = require('axios'); const axios = require('axios');
const b64toU8Array = require('./b64toU8Array');
const { defaultOptions } = require('../common/options'); const { defaultOptions } = require('../common/options');
const { version } = require('../../package.json'); const { version } = require('../../package.json');
@ -38,6 +39,7 @@ const readFromBlobOrFile = (blob, res) => {
* @access private * @access private
* @param {string, object} image - image source, supported formats: * @param {string, object} image - image source, supported formats:
* string: URL string, can be relative path * string: URL string, can be relative path
* string: base64 image
* img HTMLElement: extract image source from src attribute * img HTMLElement: extract image source from src attribute
* video HTMLElement: extract image source from poster attribute * video HTMLElement: extract image source from poster attribute
* canvas HTMLElement: extract image data by converting to Blob * canvas HTMLElement: extract image data by converting to Blob
@ -46,6 +48,11 @@ const readFromBlobOrFile = (blob, res) => {
*/ */
const loadImage = (image) => { const loadImage = (image) => {
if (check.string(image)) { if (check.string(image)) {
// Base64 Image
if (/data:image\/([a-zA-Z]*);base64,([^"]*)/.test(image)) {
return Promise.resolve(b64toU8Array(image.split(',')[1]));
}
// Image URL
return axios.get(resolveURL(image), { return axios.get(resolveURL(image), {
responseType: 'arraybuffer', responseType: 'arraybuffer',
}) })

3
src/browser/worker.js

@ -10,6 +10,7 @@
const check = require('check-types'); const check = require('check-types');
const workerUtils = require('../common/workerUtils'); const workerUtils = require('../common/workerUtils');
const b64toU8Array = require('./b64toU8Array');
/* /*
* register message handler * register message handler
@ -42,7 +43,7 @@ workerUtils.setAdapter({
} }
return global.TesseractCore; return global.TesseractCore;
}, },
b64toU8Array: s => new Uint8Array(atob(s).split('').map(c => c.charCodeAt(0))), b64toU8Array,
writeFile: (path, data, type) => { writeFile: (path, data, type) => {
postMessage({ postMessage({
jobId: 'Download', jobId: 'Download',

1
src/node/b64toU8Array.js

@ -0,0 +1 @@
module.exports = s => Buffer.from(s, 'base64');

6
src/node/index.js

@ -13,6 +13,7 @@ const axios = require('axios');
const isURL = require('is-url'); const isURL = require('is-url');
const { fork } = require('child_process'); const { fork } = require('child_process');
const path = require('path'); const path = require('path');
const b64toU8Array = require('./b64toU8Array');
const { defaultOptions } = require('../common/options'); const { defaultOptions } = require('../common/options');
const readFile = util.promisify(fs.readFile); const readFile = util.promisify(fs.readFile);
@ -25,6 +26,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
* string: base64 image
* buffer: image buffer * buffer: image buffer
* @returns {array} binary image in array format * @returns {array} binary image in array format
*/ */
@ -36,6 +38,10 @@ const loadImage = (image) => {
.then(resp => resp.data); .then(resp => resp.data);
} }
if (/data:image\/([a-zA-Z]*);base64,([^"]*)/.test(image)) {
return Promise.resolve(b64toU8Array(image.split(',')[1]));
}
if (Buffer.isBuffer(image)) { if (Buffer.isBuffer(image)) {
return Promise.resolve(image); return Promise.resolve(image);
} }

3
src/node/worker.js

@ -10,6 +10,7 @@
const check = require('check-types'); const check = require('check-types');
const workerUtils = require('../common/workerUtils'); const workerUtils = require('../common/workerUtils');
const b64toU8Array = require('./b64toU8Array');
let TesseractCore = null; let TesseractCore = null;
@ -33,7 +34,7 @@ workerUtils.setAdapter({
} }
return TesseractCore; return TesseractCore;
}, },
b64toU8Array: s => Buffer.from(s, 'base64'), b64toU8Array,
writeFile: (path, data) => { writeFile: (path, data) => {
const fs = require('fs'); const fs = require('fs');
fs.writeFile(path, data, (err) => { fs.writeFile(path, data, (err) => {

22
tests/recognize.test.js

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save