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属性中,字符串为&#34; data:image / jpeg; base64,&#34;之前。但是我的浏览器图标已经破了。我做错了什么以及如何使用我的cam中的base64编码数据创建图像?
谢谢你的帮助!答案 0 :(得分:2)
我认为问题在于您最初从ByteArray
创建BitmapData
的方式。 Flash有内置编码,我建议你使用它:
public static function encodeByteArray(data:BitmapData):String{
if(data == null)throw new Error("data parameter can not be empty!");
var bytes:ByteArray = data.encode(data.rect, new JPEGEncoderOptions(100)); //100 is the JPG quality
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('');
}
答案 1 :(得分:1)
你可以尝试这样做......
首先,您需要抓住Base64 SWC here(右键单击并保存红色文本,然后返回此处)。如果您需要帮助来安装SWC,可以查看我的其他answer here。 (如果您还没有安装AS3 JPEG编码器,那么您还需要AS3 Corelib .AS3 Corelib负责从原始位图数据中编码真正的JPG或PNG图像)
在代码中:您需要 public 变量来存储图像数据字节&amp;字符串,并在快照函数和base64编码函数等之间更新它们。所以在这个例子中代码 actual_IMG 是准备好base64的实际图像数据(用jpeg标题完成)或只是保存为常规.jpg到磁盘使用fileReference
。
public var actual_IMG:ByteArray = new ByteArray;
public var image_B64_str:String = new String;
private function snap():void
{
var bitmapData:BitmapData = new BitmapData(myVideo.width, myVideo.height);
bitmapData.draw(myVideo);
var jpg_Quality:int= 100;
var jpgEncoder:JPGEncoder = new JPGEncoder(jpg_Quality);
actual_IMG = jpgEncoder.encode(bitmapData);
encode_Image(); //do base64 encode function
}
private function encode_Image():void
{
image_B64_str = Base64.encode(actual_IMG); //update string with b64 data
doSomeFunction(); //do whatever is needed with.. image_B64_str (JPG string)
}
private function doSomeFunction():void
{
//example: send "image_B64_str" to Javascript or save to disk etc etc...
}
希望有所帮助......
使用直接链接进行更新: