【发布时间】:2014-09-13 17:15:20
【问题描述】:
我正在尝试从我的摄像头获取快照。我有这部分代码(as3)用于快照
private static const encodeChars:Array = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'];
private function snap():String{
var bitmapData:BitmapData = new BitmapData(myVideo.width, myVideo.height);
bitmapData.draw(myVideo);
return encodeByteArray(bitmapData);
}
public static function encodeByteArray(data:BitmapData):String{
if(data == null)throw new Error("data parameter can not be empty!");
var bytes:ByteArray = data.getPixels(data.rect);
bytes.writeShort(data.width);
bytes.writeShort(data.height);
bytes.writeBoolean(data.transparent);
bytes.compress();
var out:Array = [];
var i:int = 0;
var j:int = 0;
var r:int = bytes.length % 3;
var len:int = bytes.length - r;
var c:int;
while (i < len) {
c = bytes[i++] << 16 | bytes[i++] << 8 | bytes[i++];
out[j++] = encodeChars[c >> 18] + encodeChars[c >> 12 & 0x3f] + encodeChars[c >> 6 & 0x3f] + encodeChars[c & 0x3f];
}
if (r == 1) {
c = bytes[i++];
out[j++] = encodeChars[c >> 2] + encodeChars[(c & 0x03) << 4] + "==";
}
else if (r == 2) {
c = bytes[i++] << 8 | bytes[i++];
out[j++] = encodeChars[c >> 10] + encodeChars[c >> 4 & 0x3f] + encodeChars[(c & 0x0f) << 2] + "=";
}
return out.join('');
}
在我从 js 调用它之后,我在我的 html 中创建了一个 img 标记,并将接收到的数据粘贴到 img 的 src 属性中,之前带有字符串“data:image/jpeg;base64”。但是我的浏览器图标坏了。我做错了什么以及如何使用我的 cam 中的 base64 编码数据创建图像?
感谢您的帮助!
【问题讨论】:
-
你能发布你的
encodeChars吗? -
是的,我刚刚把它们放在了问题中
标签: image actionscript-3 actionscript base64