如何使Ocrad.js示例正常工作 [英] How to get Ocrad.js example to work

查看:394
本文介绍了如何使Ocrad.js示例正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作OCR(光学字符识别)Web应用程序,并且找到了JavaScript库 Ocrad.js ,这正是我一直在寻找的东西,但是我无法使它正常工作.有人可以帮我吗?

I am making an OCR (optical character recognition) web application, and I found the JavaScript library Ocrad.js, which is exactly what I was looking for, but I can't get it to work. Can somebody help me?

这是我的代码:

<!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>

推荐答案

您不能仅将<img>元素传递给OCRAD()函数.

You can't just pass an <img> element to the OCRAD() function.

来自 Ocrad.js文档:

此文件公开了单个全局函数OCRAD,该函数需要 图片作为参数,并以字符串形式返回识别的文本. [...] image参数可以是canvas元素,Context2D 实例或ImageData的实例.

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属性,才能使其正常工作.

But you may have to put width and height attributes on your <img> element in order for this to work.

如果尝试将<img>元素传递给OCRAD()函数,则会出现以下错误:

If you attempt to pass an <img> element to the OCRAD() function, you will get the following error:

未捕获的异常:5264272-异常捕获已禁用,这 无法捕获异常.用-s编译 要赶上DISABLE_EXCEPTION_CATCHING = 0或DISABLE_EXCEPTION_CATCHING = 2.

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()函数,则会出现以下错误:

If you attempt to pass a jQuery object to the OCRAD() function, you will get the following error:

错误:参数无效

Error: invalid arguments


注意:由于具有相同的来源策略,如果图像的URL与所在页面的域不同,则对context.getImageData()的调用将引发SecurityError.


Note: Because of the Same Origin Policy, the call to context.getImageData() will throw a SecurityError if the URL for the image does not have the same domain as the page it is on.

这篇关于如何使Ocrad.js示例正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆