本文介绍了如何使用canvas.toDataURL()来获取图像的base64在Adobe AIR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用 canvas.toDataURL()
在Adobe AIR?
当我尝试,我得到了以下错误:
解决方案
Adobe AIR的实施同样出身画布API中使用的图像。一旦你使用的东西从你的画布在另一个领域,你不能得到象素数据备份出来。但是,您可以使用Loader类来获取像素数据,并将其转换成一个画布的ImageData。
例如:
函数getDataURL(URL,回调){
VAR装载机=新air.Loader();
loader.contentLoaderInfo.addEventListener(air.Event.COMPLETE,函数(事件){
VAR的位图数据= loader.content.bitmapData;
VAR帆布= document.createElement方法('画布');
canvas.width = bitmapData.width;
canvas.height = bitmapData.height;
VAR上下文= canvas.getContext(2D);
VAR为imageData = context.createImageData(canvas.width,canvas.height);
VAR DST = imageData.data;
变种SRC = bitmapData.getPixels(bitmapData.rect);
src.position = 0;
变种I = 0;
而(I< dst.length){
VAR的α= src.readUnsignedByte();
DST [我+ +] = src.readUnsignedByte();
DST [我+ +] = src.readUnsignedByte();
DST [我+ +] = src.readUnsignedByte();
DST [我+ +] =阿尔法;
}
context.putImageData(为imageData,0,0);
回调(canvas.toDataURL());
});
loader.load(新air.URLRequest(URL));
}
window.addEventListener('负荷',函数(){
getDataURL('http://www.google.com/images/logo.gif',函数(dataURL){
由于air.trace(dataURL);
});
}, 假);
Is it possible to use canvas.toDataURL()
in Adobe AIR?
When I try I get the following error:
解决方案
Adobe AIR enforces same origin for images used in the canvas API. Once you've used something from another domain in your canvas, you can't get pixel data back out of it. However, you can make use of the Loader class to get the pixel data, and convert it into a canvas ImageData.
For example:
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, 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);
这篇关于如何使用canvas.toDataURL()来获取图像的base64在Adobe AIR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!