简体   繁体   English

getImageData跨源错误

[英]getImageData cross-origin error

Precursor: I know there have been a few questions already asked about this topic, but none of them seem to offer a straight JavaScript only solution. 前言:我知道已经有一些关于这个主题的问题,但是它们似乎都没有提供直接的JavaScript解决方案。

So I ran into this error where I am trying to get the pixel data from a canvas using something like context.getImageData() , my exact code can been seen here or below: 所以我遇到了这个错误,我试图从类似于context.getImageData()的画布中获取像素数据,我的确切代码可以在这里或下面看到:

<canvas id="imageCanvas" width="600" height="800"></canvas>
// Load Image
var canvas = document.getElementById('imageCanvas');

var image = new Image();
image.src = 'http://emoticons.pw/emoticons/emoticon-faces-002-medium.png';
image.onload = function() {

    width=image.width;
    height=image.height;

    var context = canvas.getContext('2d');
    context.fillStyle="#024359"; // canvas background color
    context.fillRect(0, 0, width, height);
    context.drawImage(this,0,0);

    imageData = context.getImageData(0,0,width, height); // PROBLEM HERE

    context.putImageData(imageData, 0, 0);
}

I get the following errors in Chrome: 我在Chrome中遇到以下错误:

Unable to get image data from canvas because the canvas has been tainted by cross-origin data. 无法从画布获取图像数据,因为画布已被跨源数据污染。

and then a security error as well. 然后是安全错误。 I don't want to make server changes or start chrome with a weird instruction thing. 我不想更改服务器或使用奇怪的指令启动chrome。 I feel like there has to be something I can do in JavaScript. 我觉得我必须在JavaScript中做些什么。

Using local images only is not a problem, but when trying that, I got the same error! 仅使用本地图像不是问题,但在尝试时,我得到了同样的错误!

I am trying to do this without a server, if I put this on my "default" godaddy web server, are all my problems solved? 我试图在没有服务器的情况下这样做,如果我把它放在我的“默认”godaddy网络服务器上,我的所有问题都解决了吗? I heard rumors dropbox could also simulate a server close enough? 我听说传言dropbox也可以模拟一个足够接近的服务器?

You can't use file:// if you're using that (Chrome allow you to override this but I won't recommend it). 你不能使用file://如果你正在使用它(Chrome允许你覆盖它,但我不会推荐它)。

For local testing use a light-weight server such as Mongoose which allow you use http://localhost as a domain to test your local pages. 对于本地测试,使用轻量级服务器(如Mongoose) ,允许您使用http://localhost作为域来测试本地页面。 This way you avoid problems with CORS. 这样可以避免CORS出现问题。

If you need to host images on a different domain you need to make sure they support cross-origin usage. 如果您需要在不同的域上托管图像,则需要确保它们支持跨域使用。

DropBox and ImgUrl (I recommend the latter for just images) both support CORS usage, but you need to request such usage by adding the crossOrigin property to your image before setting the source (or include it in the HTML tag if you're using that): DropBox和ImgUrl(我建议后者仅用于图像)都支持CORS用法,但是你需要在设置源代码之前将crossOrigin属性添加到图像中来请求这样的用法(如果你使用它,则将其包含在HTML标记中) ):

var img = new Image;

img.onload = myLoader;
img.crossOrigin = '';              ///=anonymous
img.src = 'http://imgur.com/....';

or in HTML: 或者在HTML中:

<img src="..." alt="" crossOrigin="anonymous" />

Make sure you put the img.setAttribute('crossOrigin', ''); 确保你把img.setAttribute('crossOrigin', ''); before you set the source of your image object. 在设置图像对象的源之前。 just like this: 像这样:

var img = document.createElement("img");
//fix crossorigin here...
img.setAttribute('crossOrigin', '');
//after that put your source
img.src = imageSrcURL;
document.body.appendChild(img);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM