Jerome Wu
6 years ago
1 changed files with 56 additions and 93 deletions
@ -1,98 +1,61 @@ |
|||||||
var defaultOptions = { |
const isURL = require('is-url'); |
||||||
// workerPath: 'https://cdn.jsdelivr.net/gh/naptha/tesseract.js@0.2.0/dist/worker.js',
|
|
||||||
corePath: 'https://cdn.jsdelivr.net/gh/naptha/tesseract.js-core@0.1.0/index.js', |
const defaultOptions = { |
||||||
langPath: 'https://cdn.jsdelivr.net/gh/naptha/tessdata@gh-pages/3.02/', |
// workerPath: 'https://cdn.jsdelivr.net/gh/naptha/tesseract.js@0.2.0/dist/worker.js',
|
||||||
|
corePath: 'https://cdn.jsdelivr.net/gh/naptha/tesseract.js-core@0.1.0/index.js', |
||||||
|
langPath: 'https://cdn.jsdelivr.net/gh/naptha/tessdata@gh-pages/3.02/', |
||||||
|
}; |
||||||
|
|
||||||
|
if (process.env.NODE_ENV === 'development') { |
||||||
|
console.debug('Using Development Configuration'); |
||||||
|
// eslint-disable-next-line
|
||||||
|
const { protocol, host } = location; |
||||||
|
defaultOptions.workerPath = `${protocol}://${host}/dist/worker.dev.js?nocache=${Math.random().toString(36).slice(3)}`; |
||||||
|
} else { |
||||||
|
const { version } = require('../../package.json'); |
||||||
|
defaultOptions.workerPath = `https://cdn.jsdelivr.net/gh/naptha/tesseract.js@${version}/dist/worker.js`; |
||||||
} |
} |
||||||
|
|
||||||
if (process.env.NODE_ENV === "development") { |
const loadImage = (imageURI) => { |
||||||
console.debug('Using Development Configuration') |
if (isURL(imageURI)) { |
||||||
defaultOptions.workerPath = location.protocol + '//' + location.host + '/dist/worker.dev.js?nocache=' + Math.random().toString(36).slice(3) |
return fetch(imageURI) |
||||||
}else{ |
.then(resp => resp.buffer()); |
||||||
var version = require('../../package.json').version; |
} |
||||||
defaultOptions.workerPath = 'https://cdn.jsdelivr.net/gh/naptha/tesseract.js@' + version + '/dist/worker.js' |
return new Promise(); |
||||||
} |
}; |
||||||
|
|
||||||
exports.defaultOptions = defaultOptions; |
exports.defaultOptions = defaultOptions; |
||||||
|
|
||||||
|
exports.spawnWorker = (instance, workerOptions) => { |
||||||
exports.spawnWorker = function spawnWorker(instance, workerOptions){ |
let worker; |
||||||
if(window.Blob && window.URL){ |
if (window.Blob && window.URL) { |
||||||
var blob = new Blob(['importScripts("' + workerOptions.workerPath + '");']) |
const blob = new Blob([`importScripts("${workerOptions.workerPath}");`]); |
||||||
var worker = new Worker(window.URL.createObjectURL(blob)); |
worker = new Worker(window.URL.createObjectURL(blob)); |
||||||
}else{ |
} else { |
||||||
var worker = new Worker(workerOptions.workerPath) |
worker = new Worker(workerOptions.workerPath); |
||||||
} |
} |
||||||
|
|
||||||
worker.onmessage = function(e){ |
worker.onmessage = ({ data }) => { |
||||||
var packet = e.data; |
instance._recv(data); |
||||||
instance._recv(packet) |
}; |
||||||
} |
|
||||||
return worker |
return worker; |
||||||
} |
}; |
||||||
|
|
||||||
exports.terminateWorker = function(instance){ |
exports.terminateWorker = (instance) => { |
||||||
instance.worker.terminate() |
instance.worker.terminate(); |
||||||
} |
}; |
||||||
|
|
||||||
exports.sendPacket = function sendPacket(instance, packet){ |
exports.sendPacket = (instance, packet) => { |
||||||
loadImage(packet.payload.image, function(img){ |
loadImage(packet.payload.image) |
||||||
packet.payload.image = img |
.then(buf => new Uint8Array(buf)) |
||||||
instance.worker.postMessage(packet) |
.then((img) => { |
||||||
}) |
instance.worker.postMessage({ |
||||||
} |
...packet, |
||||||
|
payload: { |
||||||
|
...packet.payload, |
||||||
function loadImage(image, cb){ |
image: Array.from(img), |
||||||
if(typeof image === 'string'){ |
}, |
||||||
if(/^\#/.test(image)){ |
}); |
||||||
// element css selector
|
}); |
||||||
return loadImage(document.querySelector(image), cb) |
}; |
||||||
}else if(/(blob|data)\:/.test(image)){ |
|
||||||
// data url
|
|
||||||
var im = new Image |
|
||||||
im.src = image; |
|
||||||
im.onload = e => loadImage(im, cb); |
|
||||||
return |
|
||||||
}else{ |
|
||||||
var xhr = new XMLHttpRequest(); |
|
||||||
xhr.open('GET', image, true) |
|
||||||
xhr.responseType = "blob"; |
|
||||||
xhr.onload = e => loadImage(xhr.response, cb); |
|
||||||
xhr.onerror = function(e){ |
|
||||||
if(/^https?:\/\//.test(image) && !/^https:\/\/crossorigin.me/.test(image)){ |
|
||||||
console.debug('Attempting to load image with CORS proxy') |
|
||||||
loadImage('https://crossorigin.me/' + image, cb) |
|
||||||
} |
|
||||||
} |
|
||||||
xhr.send(null) |
|
||||||
return |
|
||||||
} |
|
||||||
}else if(image instanceof File){ |
|
||||||
// files
|
|
||||||
var fr = new FileReader() |
|
||||||
fr.onload = e => loadImage(fr.result, cb); |
|
||||||
fr.readAsDataURL(image) |
|
||||||
return |
|
||||||
}else if(image instanceof Blob){ |
|
||||||
return loadImage(URL.createObjectURL(image), cb) |
|
||||||
}else if(image.getContext){ |
|
||||||
// canvas element
|
|
||||||
return loadImage(image.getContext('2d'), cb) |
|
||||||
}else if(image.tagName == "IMG" || image.tagName == "VIDEO"){ |
|
||||||
// image element or video element
|
|
||||||
var c = document.createElement('canvas'); |
|
||||||
c.width = image.naturalWidth || image.videoWidth; |
|
||||||
c.height = image.naturalHeight || image.videoHeight; |
|
||||||
var ctx = c.getContext('2d'); |
|
||||||
ctx.drawImage(image, 0, 0); |
|
||||||
return loadImage(ctx, cb) |
|
||||||
}else if(image.getImageData){ |
|
||||||
// canvas context
|
|
||||||
var data = image.getImageData(0, 0, image.canvas.width, image.canvas.height); |
|
||||||
return loadImage(data, cb) |
|
||||||
}else{ |
|
||||||
return cb(image) |
|
||||||
} |
|
||||||
throw new Error('Missing return in loadImage cascade') |
|
||||||
|
|
||||||
} |
|
||||||
|
Loading…
Reference in new issue