问题描述
我正在制作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文档:
尝试一下:
$(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>
元素上放置width
和height
属性,才能使其正常工作.
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:
如果尝试将jQuery对象传递给OCRAD()
函数,则会出现以下错误:
If you attempt to pass a jQuery object to the OCRAD()
function, you will get the following error:
注意:由于具有相同的来源策略,如果图像的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示例正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!