Jerome Wu
5 years ago
8 changed files with 140 additions and 136 deletions
@ -0,0 +1,79 @@ |
|||||||
|
const axios = require('axios'); |
||||||
|
const resolveURL = require('resolve-url'); |
||||||
|
|
||||||
|
/** |
||||||
|
* readFromBlobOrFile |
||||||
|
* |
||||||
|
* @name readFromBlobOrFile |
||||||
|
* @function |
||||||
|
* @access private |
||||||
|
* @param {object} blob A blob or file objec to read |
||||||
|
* @param {function} res callback function after reading completes |
||||||
|
*/ |
||||||
|
const readFromBlobOrFile = blob => ( |
||||||
|
new Promise((resolve, reject) => { |
||||||
|
const fileReader = new FileReader(); |
||||||
|
fileReader.onload = () => { |
||||||
|
resolve(fileReader.result); |
||||||
|
}; |
||||||
|
fileReader.onerror = ({ target: { error: { code } } }) => { |
||||||
|
reject(Error(`File could not be read! Code=${code}`)); |
||||||
|
}; |
||||||
|
fileReader.readAsArrayBuffer(blob); |
||||||
|
}) |
||||||
|
); |
||||||
|
|
||||||
|
/** |
||||||
|
* loadImage |
||||||
|
* |
||||||
|
* @name loadImage |
||||||
|
* @function load image from different source |
||||||
|
* @access private |
||||||
|
* @param {string, object} image - image source, supported formats: |
||||||
|
* string: URL string, can be relative path |
||||||
|
* string: base64 image |
||||||
|
* img HTMLElement: extract image source from src attribute |
||||||
|
* video HTMLElement: extract image source from poster attribute |
||||||
|
* canvas HTMLElement: extract image data by converting to Blob |
||||||
|
* File instance: data from <input type="file" /> |
||||||
|
* @returns {array} binary image in array format |
||||||
|
*/ |
||||||
|
const loadImage = async (image) => { |
||||||
|
let data = image; |
||||||
|
if (typeof image === 'undefined') { |
||||||
|
return 'undefined'; |
||||||
|
} |
||||||
|
|
||||||
|
if (typeof image === 'string') { |
||||||
|
// Base64 Image
|
||||||
|
if (/data:image\/([a-zA-Z]*);base64,([^"]*)/.test(image)) { |
||||||
|
data = atob(image.split(',')[1]) |
||||||
|
.split('') |
||||||
|
.map(c => c.charCodeAt(0)); |
||||||
|
} else { |
||||||
|
const { data: _data } = await axios.get(resolveURL(image), { responseType: 'arraybuffer' }); |
||||||
|
data = _data; |
||||||
|
} |
||||||
|
} else if (image instanceof HTMLElement) { |
||||||
|
if (image.tagName === 'IMG') { |
||||||
|
data = loadImage(image.src); |
||||||
|
} |
||||||
|
if (image.tagName === 'VIDEO') { |
||||||
|
data = loadImage(image.poster); |
||||||
|
} |
||||||
|
if (image.tagName === 'CANVAS') { |
||||||
|
await new Promise((resolve) => { |
||||||
|
image.toBlob(async (blob) => { |
||||||
|
data = await readFromBlobOrFile(blob); |
||||||
|
resolve(); |
||||||
|
}); |
||||||
|
}); |
||||||
|
} |
||||||
|
} else if (image instanceof File || image instanceof Blob) { |
||||||
|
data = await readFromBlobOrFile(image); |
||||||
|
} |
||||||
|
|
||||||
|
return new Uint8Array(data); |
||||||
|
}; |
||||||
|
|
||||||
|
module.exports = loadImage; |
@ -1,92 +1,12 @@ |
|||||||
const axios = require('axios'); |
|
||||||
const resolveURL = require('resolve-url'); |
|
||||||
|
|
||||||
/** |
/** |
||||||
* readFromBlobOrFile |
* send |
||||||
* |
* |
||||||
* @name readFromBlobOrFile |
* @name send |
||||||
* @function |
|
||||||
* @access private |
|
||||||
* @param {object} blob A blob or file objec to read |
|
||||||
* @param {function} res callback function after reading completes |
|
||||||
*/ |
|
||||||
const readFromBlobOrFile = blob => ( |
|
||||||
new Promise((resolve, reject) => { |
|
||||||
const fileReader = new FileReader(); |
|
||||||
fileReader.onload = () => { |
|
||||||
resolve(fileReader.result); |
|
||||||
}; |
|
||||||
fileReader.onerror = ({ target: { error: { code } } }) => { |
|
||||||
reject(Error(`File could not be read! Code=${code}`)); |
|
||||||
}; |
|
||||||
fileReader.readAsArrayBuffer(blob); |
|
||||||
}) |
|
||||||
); |
|
||||||
|
|
||||||
/** |
|
||||||
* loadImage |
|
||||||
* |
|
||||||
* @name loadImage |
|
||||||
* @function load image from different source |
|
||||||
* @access private |
|
||||||
* @param {string, object} image - image source, supported formats: |
|
||||||
* string: URL string, can be relative path |
|
||||||
* string: base64 image |
|
||||||
* img HTMLElement: extract image source from src attribute |
|
||||||
* video HTMLElement: extract image source from poster attribute |
|
||||||
* canvas HTMLElement: extract image data by converting to Blob |
|
||||||
* File instance: data from <input type="file" /> |
|
||||||
* @returns {array} binary image in array format |
|
||||||
*/ |
|
||||||
const loadImage = async (image) => { |
|
||||||
let data = image; |
|
||||||
if (typeof image === 'undefined') { |
|
||||||
return 'undefined'; |
|
||||||
} |
|
||||||
|
|
||||||
if (typeof image === 'string') { |
|
||||||
// Base64 Image
|
|
||||||
if (/data:image\/([a-zA-Z]*);base64,([^"]*)/.test(image)) { |
|
||||||
data = atob(image.split(',')[1]) |
|
||||||
.split('') |
|
||||||
.map(c => c.charCodeAt(0)); |
|
||||||
} else { |
|
||||||
const { data: _data } = await axios.get(resolveURL(image), { responseType: 'arraybuffer' }); |
|
||||||
data = _data; |
|
||||||
} |
|
||||||
} else if (image instanceof HTMLElement) { |
|
||||||
if (image.tagName === 'IMG') { |
|
||||||
data = loadImage(image.src); |
|
||||||
} |
|
||||||
if (image.tagName === 'VIDEO') { |
|
||||||
data = loadImage(image.poster); |
|
||||||
} |
|
||||||
if (image.tagName === 'CANVAS') { |
|
||||||
await new Promise((resolve) => { |
|
||||||
image.toBlob(async (blob) => { |
|
||||||
data = await readFromBlobOrFile(blob); |
|
||||||
resolve(); |
|
||||||
}); |
|
||||||
}); |
|
||||||
} |
|
||||||
} else if (image instanceof File || image instanceof Blob) { |
|
||||||
data = await readFromBlobOrFile(image); |
|
||||||
} |
|
||||||
|
|
||||||
return new Uint8Array(data); |
|
||||||
}; |
|
||||||
|
|
||||||
/** |
|
||||||
* sendPacket |
|
||||||
* |
|
||||||
* @name sendPacket |
|
||||||
* @function send packet to worker and create a job |
* @function send packet to worker and create a job |
||||||
* @access public |
* @access public |
||||||
* @param {object} instance TesseractWorker instance |
* @param {object} instance TesseractWorker instance |
||||||
* @param {object} iPacket data for worker |
* @param {object} iPacket data for worker |
||||||
*/ |
*/ |
||||||
module.exports = async (worker, _packet) => { |
module.exports = async (worker, packet) => { |
||||||
const packet = { ..._packet }; |
|
||||||
packet.payload.image = await loadImage(packet.payload.image); |
|
||||||
worker.postMessage(packet); |
worker.postMessage(packet); |
||||||
}; |
}; |
||||||
|
@ -0,0 +1,40 @@ |
|||||||
|
const util = require('util'); |
||||||
|
const fs = require('fs'); |
||||||
|
const axios = require('axios'); |
||||||
|
const isURL = require('is-url'); |
||||||
|
|
||||||
|
const readFile = util.promisify(fs.readFile); |
||||||
|
|
||||||
|
/** |
||||||
|
* loadImage |
||||||
|
* |
||||||
|
* @name loadImage |
||||||
|
* @function load image from different source |
||||||
|
* @access public |
||||||
|
* @param {string} image - image source, supported formats: |
||||||
|
* string: URL string or file path |
||||||
|
* string: base64 image |
||||||
|
* buffer: image buffer |
||||||
|
* @returns {array} binary image in array format |
||||||
|
*/ |
||||||
|
module.exports = async (image) => { |
||||||
|
let data = image; |
||||||
|
if (typeof image === 'undefined') { |
||||||
|
return image; |
||||||
|
} |
||||||
|
|
||||||
|
if (typeof image === 'string') { |
||||||
|
if (isURL(image) || image.startsWith('chrome-extension://') || image.startsWith('file://')) { |
||||||
|
const { data: _data } = await axios.get(image, { responseType: 'arraybuffer' }); |
||||||
|
data = _data; |
||||||
|
} else if (/data:image\/([a-zA-Z]*);base64,([^"]*)/.test(image)) { |
||||||
|
data = Buffer.from(image.split(',')[1], 'base64'); |
||||||
|
} else { |
||||||
|
data = await readFile(image); |
||||||
|
} |
||||||
|
} else if (Buffer.isBuffer(image)) { |
||||||
|
data = image; |
||||||
|
} |
||||||
|
|
||||||
|
return new Uint8Array(data); |
||||||
|
}; |
Loading…
Reference in new issue