Browse Source

Update README.md and add error handler in loadImage

support/1.x
Jerome Wu 6 years ago
parent
commit
1f497271b5
  1. 3
      README.md
  2. 27
      dist/tesseract.js
  3. 2
      dist/tesseract.min.js
  4. 2
      dist/tesseract.min.js.map
  5. 8
      src/browser/index.js

3
README.md

@ -24,6 +24,9 @@ Tesseract.js wraps an [emscripten](https://github.com/kripken/emscripten) [port]
# Installation # Installation
**Tesseract.js v2 alpha is now available!! Check [HERE](https://github.com/naptha/tesseract.js) for more information.**
Tesseract.js works with a `<script>` tag via local copy or CDN, with webpack and Browserify via `npm`, and on Node.js via `npm`. [Check out the docs](#docs) for a full treatment of the API. Tesseract.js works with a `<script>` tag via local copy or CDN, with webpack and Browserify via `npm`, and on Node.js via `npm`. [Check out the docs](#docs) for a full treatment of the API.
## &lt;script /> ## &lt;script />

27
dist/tesseract.js vendored

@ -253,9 +253,11 @@ if (process.env.TESS_ENV === "development") {
exports.defaultOptions = defaultOptions; exports.defaultOptions = defaultOptions;
exports.spawnWorker = function spawnWorker(instance, workerOptions) { exports.spawnWorker = function spawnWorker(instance, workerOptions) {
if (window.Blob && window.URL) { if (Blob && URL) {
var blob = new Blob(['importScripts("' + workerOptions.workerPath + '");']); var blob = new Blob(['importScripts("' + workerOptions.workerPath + '");'], {
var worker = new Worker(window.URL.createObjectURL(blob)); type: 'application/javascript'
});
var worker = new Worker(URL.createObjectURL(blob));
} else { } else {
var worker = new Worker(workerOptions.workerPath); var worker = new Worker(workerOptions.workerPath);
} }
@ -290,20 +292,26 @@ function loadImage(image, cb) {
im.onload = function (e) { im.onload = function (e) {
return loadImage(im, cb); return loadImage(im, cb);
}; };
im.onerror = function (e) {
throw e;
};
return; return;
} else { } else {
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.open('GET', image, true); xhr.open('GET', image, true);
xhr.responseType = "blob"; xhr.responseType = "blob";
xhr.onload = function (e) { xhr.onload = function (e) {
return loadImage(xhr.response, cb); if (xhr.status >= 400) {
throw new Error('Fail to get image as Blob');
} else {
loadImage(xhr.response, cb);
}
}; };
xhr.onerror = function (e) { xhr.onerror = function (e) {
if (/^https?:\/\//.test(image) && !/^https:\/\/crossorigin.me/.test(image)) { throw e;
console.debug('Attempting to load image with CORS proxy');
loadImage('https://crossorigin.me/' + image, cb);
}
}; };
xhr.send(null); xhr.send(null);
return; return;
} }
@ -313,6 +321,9 @@ function loadImage(image, cb) {
fr.onload = function (e) { fr.onload = function (e) {
return loadImage(fr.result, cb); return loadImage(fr.result, cb);
}; };
fr.onerror = function (e) {
throw e;
};
fr.readAsDataURL(image); fr.readAsDataURL(image);
return; return;
} else if (image instanceof Blob) { } else if (image instanceof Blob) {

2
dist/tesseract.min.js vendored

File diff suppressed because one or more lines are too long

2
dist/tesseract.min.js.map vendored

File diff suppressed because one or more lines are too long

8
src/browser/index.js

@ -54,7 +54,7 @@ function loadImage(image, cb){
var im = new Image var im = new Image
im.src = image; im.src = image;
im.onload = e => loadImage(im, cb); im.onload = e => loadImage(im, cb);
//im.onerror = e => ?; TODO handle error im.onerror = e => { throw e; };
return return
}else{ }else{
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
@ -63,12 +63,12 @@ function loadImage(image, cb){
xhr.onload = e => { xhr.onload = e => {
if (xhr.status >= 400){ if (xhr.status >= 400){
//TODO handle error throw new Error('Fail to get image as Blob');
}else{ }else{
loadImage(xhr.response, cb); loadImage(xhr.response, cb);
} }
}; };
//xhr.onerror = e => ?; TODO handle error xhr.onerror = e => { throw e; };
xhr.send(null) xhr.send(null)
return return
@ -77,7 +77,7 @@ function loadImage(image, cb){
// files // files
var fr = new FileReader() var fr = new FileReader()
fr.onload = e => loadImage(fr.result, cb); fr.onload = e => loadImage(fr.result, cb);
//fr.onerror = e => ?; TODO handle error fr.onerror = e => { throw e; };
fr.readAsDataURL(image) fr.readAsDataURL(image)
return return
}else if(image instanceof Blob){ }else if(image instanceof Blob){

Loading…
Cancel
Save