Jerome Wu
6 years ago
1 changed files with 56 additions and 93 deletions
@ -1,98 +1,61 @@
@@ -1,98 +1,61 @@
|
||||
var defaultOptions = { |
||||
const isURL = require('is-url'); |
||||
|
||||
const defaultOptions = { |
||||
// 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") { |
||||
console.debug('Using Development Configuration') |
||||
defaultOptions.workerPath = location.protocol + '//' + location.host + '/dist/worker.dev.js?nocache=' + Math.random().toString(36).slice(3) |
||||
}else{ |
||||
var version = require('../../package.json').version; |
||||
defaultOptions.workerPath = 'https://cdn.jsdelivr.net/gh/naptha/tesseract.js@' + version + '/dist/worker.js' |
||||
} |
||||
|
||||
exports.defaultOptions = defaultOptions; |
||||
|
||||
|
||||
exports.spawnWorker = function spawnWorker(instance, workerOptions){ |
||||
if(window.Blob && window.URL){ |
||||
var blob = new Blob(['importScripts("' + workerOptions.workerPath + '");']) |
||||
var worker = new Worker(window.URL.createObjectURL(blob)); |
||||
}else{ |
||||
var worker = new Worker(workerOptions.workerPath) |
||||
} |
||||
|
||||
worker.onmessage = function(e){ |
||||
var packet = e.data; |
||||
instance._recv(packet) |
||||
const loadImage = (imageURI) => { |
||||
if (isURL(imageURI)) { |
||||
return fetch(imageURI) |
||||
.then(resp => resp.buffer()); |
||||
} |
||||
return worker |
||||
} |
||||
|
||||
exports.terminateWorker = function(instance){ |
||||
instance.worker.terminate() |
||||
} |
||||
|
||||
exports.sendPacket = function sendPacket(instance, packet){ |
||||
loadImage(packet.payload.image, function(img){ |
||||
packet.payload.image = img |
||||
instance.worker.postMessage(packet) |
||||
}) |
||||
} |
||||
return new Promise(); |
||||
}; |
||||
|
||||
exports.defaultOptions = defaultOptions; |
||||
|
||||
function loadImage(image, cb){ |
||||
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) |
||||
exports.spawnWorker = (instance, workerOptions) => { |
||||
let worker; |
||||
if (window.Blob && window.URL) { |
||||
const blob = new Blob([`importScripts("${workerOptions.workerPath}");`]); |
||||
worker = new Worker(window.URL.createObjectURL(blob)); |
||||
} else { |
||||
worker = new Worker(workerOptions.workerPath); |
||||
} |
||||
} |
||||
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') |
||||
|
||||
} |
||||
worker.onmessage = ({ data }) => { |
||||
instance._recv(data); |
||||
}; |
||||
|
||||
return worker; |
||||
}; |
||||
|
||||
exports.terminateWorker = (instance) => { |
||||
instance.worker.terminate(); |
||||
}; |
||||
|
||||
exports.sendPacket = (instance, packet) => { |
||||
loadImage(packet.payload.image) |
||||
.then(buf => new Uint8Array(buf)) |
||||
.then((img) => { |
||||
instance.worker.postMessage({ |
||||
...packet, |
||||
payload: { |
||||
...packet.payload, |
||||
image: Array.from(img), |
||||
}, |
||||
}); |
||||
}); |
||||
}; |
||||
|
Loading…
Reference in new issue