2014-01-07 120 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'。 –