Guillermo
10 years ago
commit
906ce3cadb
5 changed files with 2086 additions and 0 deletions
@ -0,0 +1,60 @@
@@ -0,0 +1,60 @@
|
||||
var Tesseract = {} |
||||
|
||||
Tesseract.recognize = function(image, options, callback){ |
||||
var lang = options.lang |
||||
if(typeof lang === "undefined"){ |
||||
lang = 'eng' |
||||
} |
||||
|
||||
if (typeof options === 'string') { |
||||
lang = options |
||||
options = {} |
||||
} |
||||
|
||||
if (typeof options === "function") { |
||||
callback = options |
||||
options = {} |
||||
} |
||||
|
||||
|
||||
if(image.getContext){ |
||||
image = image.getContext('2d'); |
||||
}else if(image.tagName == "IMG" || image.tagName == "VIDEO"){ |
||||
var c = document.createElement('canvas'); |
||||
if(image.tagName == "IMG"){ |
||||
c.width = image.naturalWidth; |
||||
c.height = image.naturalHeight; |
||||
}else if(image.tagName == "VIDEO"){ |
||||
c.width = image.videoWidth; |
||||
c.height = image.videoHeight; |
||||
} |
||||
var ctx = c.getContext('2d'); |
||||
ctx.drawImage(image, 0, 0); |
||||
image = ctx; |
||||
} |
||||
if(image.getImageData) image = image.getImageData(0, 0, image.canvas.width, image.canvas.height); |
||||
|
||||
var worker = new Worker('./worker.js') |
||||
|
||||
if(typeof callback === "function"){ |
||||
worker.onmessage = function(e){ |
||||
callback(e.data.err, e.data.result) |
||||
} |
||||
worker.postMessage({image: image, lang: lang}) |
||||
console.log('callback') |
||||
} |
||||
else { |
||||
return new Promise(function(resolve, reject){ |
||||
worker.onmessage = function(e){ |
||||
if(e.data.err){ |
||||
reject(e.data.err) |
||||
} |
||||
else { |
||||
resolve(e.data.result) |
||||
} |
||||
} |
||||
worker.postMessage({image: image, lang: lang, options: options}) |
||||
console.log('promise') |
||||
}) |
||||
} |
||||
} |
@ -0,0 +1,17 @@
@@ -0,0 +1,17 @@
|
||||
<canvas id="c"></canvas> |
||||
<script type="text/javascript" src="./Tesseract.js"></script> |
||||
<script type="text/javascript"> |
||||
var canvas = document.getElementById('c') |
||||
canvas.width = 400 |
||||
canvas.height = 400 |
||||
var ctx = canvas.getContext('2d'); |
||||
ctx.font = '30px "Arial Black"' |
||||
ctx.fillText('Hell0 World', 100, 40) |
||||
// ctx.fillText("囚犯離奇掙脫囚犯離奇掙脫", 100, 40) |
||||
ctx.font = '30px "Times New Roman"' |
||||
ctx.fillText('from beyond', 100, 80) |
||||
// ctx.fillText('2小時可換乘2次2小時可換乘2次', 100, 80) |
||||
ctx.font = '30px sans-serif' |
||||
ctx.fillText('the Cosmic Void', 100, 120) |
||||
Tesseract.recognize(canvas,{tessedit_char_blacklist:'e'}).then( function(d){ console.log(d) } ) |
||||
</script> |
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue