最佳答案
Adobe AIR对canvas API中使用的图像强制执行相同的原点.一旦您在画布中使用了来自其他域的内容,就无法从中获取像素数据.但是,您可以使用Loader类来获取像素数据,并将其转换为画布ImageData.
例如:
function getDataURL(url,callback) {
var loader = new air.Loader();
loader.contentLoaderInfo.addEventListener(air.Event.COMPLETE,function(event) {
var bitmapData = loader.content.bitmapData;
var canvas = document.createElement('canvas');
canvas.width = bitmapData.width;
canvas.height = bitmapData.height;
var context = canvas.getContext('2d');
var imageData = context.createImageData(canvas.width,canvas.height);
var dst = imageData.data;
var src = bitmapData.getPixels(bitmapData.rect);
src.position = 0;
var i = 0;
while (i < dst.length) {
var alpha = src.readUnsignedByte();
dst[i++] = src.readUnsignedByte();
dst[i++] = src.readUnsignedByte();
dst[i++] = src.readUnsignedByte();
dst[i++] = alpha;
}
context.putImageData(imageData,0);
callback(canvas.toDataURL());
});
loader.load(new air.URLRequest(url));
}
window.addEventListener('load',function() {
getDataURL('http://www.google.com/images/logo.gif',function(dataURL) {
air.trace(dataURL);
});
},false);