用于画布 html5 的 Javascript getImageData
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4121142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript getImageData for canvas html5
提问by hugh Hymanson
I've tearing my hair out! I got this working, thought 'i can afford not to save a version of this', then i .. broke the 'build'.
我已经把头发撕了!我得到了这个工作,认为“我负担不起不保存这个版本”,然后我..打破了“构建”。
The line myImageData = context.getImageData(0, 0, canvas.width, canvas.height);seems to breaking this, as an alert will work before, but not after it.
这条线myImageData = context.getImageData(0, 0, canvas.width, canvas.height);似乎打破了这一点,因为警报将在此之前起作用,但在它之后不起作用。
The image itself is loading.
图像本身正在加载。
Any and all suggestions welcomed ^_^ I'm at tether's end, and going to get RSI from kicking myself soon.
欢迎任何和所有建议 ^_^ 我在系绳的尽头,很快就会让 RSI 踢自己。
var myImageData;
var image_var = new Image();
image_var.onload = function () {
canvas.width = image_var.width;
canvas.height = image_var.height;
context.drawImage(image_var, 0, 0, image_var.width, image_var.height);
myImageData = context.getImageData(0, 0, canvas.width, canvas.height);
alert('');
}
image_var.src = "example1.jpeg";
回答by
Add below piece of code to your actual code, firefoxallows you to work locally
将以下代码添加到您的实际代码中,firefox允许您在本地工作
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
what actually it does ?
它实际上是做什么的?
When the script calls this function, if the signature is valid or codebase principal are enabled, expanded privileges can be granted. If a user has not accessed this principal before, a dialog asks the user if he wants to accept the signed code. Unlike Communicator 4.x, Mozilla does not display a detailed Java grant dialog, rather a simple dialog asking if the principal can be trusted. The user can accept or deny and allow their choice to be remembered by the browser. As shown in the second example, two privileges may be asked for at once, so only one dialog appears.
当脚本调用此函数时,如果签名有效或启用了代码库主体,则可以授予扩展权限。如果用户之前未访问过此主体,则会出现一个对话框询问用户是否要接受签名代码。与 Communicator 4.x 不同,Mozilla 不显示详细的 Java 授权对话框,而是一个询问主体是否可信的简单对话框。用户可以接受或拒绝并允许浏览器记住他们的选择。如第二个示例所示,可能一次请求两个权限,因此只出现一个对话框。
Privileges are granted only in the scope of the requesting function. This scope includes any functions called by the requesting function. When the script leaves the requesting function, privileges no longer apply.
权限仅在请求功能的范围内授予。此范围包括请求函数调用的任何函数。当脚本离开请求函数时,特权不再适用。
You can read more about it here
Here's a demo 
这是一个演示 
回答by hugh Hymanson
I found asolution - the problem was that I was attempting to read local files(ie, on my computer), which don't have a domain name associated with them (apparently local ip doesn't count in this instance). To protect the security of interests of people with online content, the W3C + browsers have made it so that getImageData() doesn't work on files stored outside of the server that's home to the javascript.
我找到了一个解决方案 - 问题是我试图读取本地文件(即,在我的计算机上),这些文件没有与之关联的域名(在这种情况下显然本地 ip 不计算在内)。为了保护拥有在线内容的人们的利益安全,W3C + 浏览器已经做出了这样的规定,即 getImageData() 不能处理存储在 javascript 所在的服务器之外的文件。
Unfortunately, it treats local files as domainless, and so the security restrictions apply, apparently. I'm looking around for ways to overcome this problem (looking at appCache and websql in HTML5 at the moment, may or may not bear fruit). I'd prefer for the user of my web app not to have to upload the material they're working with (so they can work offline, and for security reasons). Any suggestions welcome!
不幸的是,它将本地文件视为无域文件,因此显然适用安全限制。我正在寻找解决这个问题的方法(目前在 HTML5 中查看 appCache 和 websql,可能会也可能不会结果)。我希望我的网络应用程序的用户不必上传他们正在使用的材料(这样他们就可以脱机工作,并且出于安全原因)。欢迎任何建议!

