2014-01-07 119 views
4

我提出的OCR(光學字符識別)的Web應用程序,而且我發現JavaScript庫Ocrad.js,這正是我一直在尋找,但我不能得到它工作。有人能幫助我嗎?如何獲得Ocrad.js例如工作

這裏是我的代碼:

<!doctype html> 
<html> 
    <head> 
     <script src="ocrad.js"></script> 
     <script src="http://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js"></script> 
     <script src="http://code.jquery.com/jquery-1.10.2.js"></script> 
     <script> 
      $(document).ready(function() { 
       var image = document.getElementById('image'); 
       var string = OCRAD(image); 
       alert(string); 
      }); 
     </script> 
    </head> 
    <body> 
     <img src="img.jpg" id="image"> 
    </body> 
</html> 
+0

請具體得多。 「無法實現它」的意思是什麼? – isherwood

+0

我會使用的圖像的路徑來定義「形象」 – pathfinder

回答

4

你不能只傳遞一個<img>元素到OCRAD()功能。

Ocrad.js documentation

This file exposes a single global function, OCRAD which takes an image as an argument and returns the recognized text as a string. [...] The image argument can be a canvas element, a Context2D instance, or an instance of ImageData.

試試這個:

$(document).ready(function() { 
    var $img = $('#image'); 

    var context = document.createElement('canvas').getContext('2d'); 
    context.drawImage($img[0], 0, 0); 

    var imageData = context.getImageData(0, 0, $img.width(), $img.height()); 

    var string = OCRAD(imageData); 
    alert(string); 
}); 

但是你可能不得不把你的<img>元素widthheight屬性爲了這個工作。


如果您嘗試將<img>元素傳遞給OCRAD()功能,您將收到以下錯誤:

uncaught exception: 5264272 - Exception catching is disabled, this exception cannot be caught. Compile with -s DISABLE_EXCEPTION_CATCHING=0 or DISABLE_EXCEPTION_CATCHING=2 to catch.

如果你試圖傳遞一個jQuery對象到OCRAD()功能,您將獲得以下錯誤:

Error: invalid arguments


注:由於同源策略的,調用context.getImageData()將拋出一個SecurityError如果圖片的網址不具有相同的域名,因爲它是在頁面上。

+0

嗨@約翰S,這不是幫我ü可以PLZ檢查我的代碼以'函數imageLoaded(EV){ 元=的document.getElementById(「康康舞」) ; c = element.getContext(「2d」); im = ev.target; width = element.width; height = element.height; c.drawImage(im,0,0); \t VAR DATA1 = OCRAD(C); \t console.log(data1); } IM =新的圖像(); im.src =「message.png」; im.onload = imageLoaded;'我得到** Uncaught SecurityError:未能在CanvasRenderingContext2D上執行'getImageData':畫布已被交叉源數據污染** –

+0

@NithinCVpoyyil - 圖片的URL必須與它所在的頁面具有相同的域,否則調用'context.getImageData()'將會拋出一個'SecurityError'。 –