Noob to As3 Bitmap的东西......
当我尝试执行以下代码时失败
bmd.setPixels(bmd.rect,decodeValue);
,错误信息是: 错误:错误#2030:遇到文件结束。
情况是我通过转换byteArray将图像作为二进制文件存储到数据库中,现在我想检索它并转换回图像。
只是为了清除这个ByteArray需要放入Bitmap然后你可以添加到影片剪辑吗?
import flash.display.Loader;
import flash.net.URLRequest;
import flash.display.Bitmap;
import flash.display.BitmapData;
import com.dynamicflash.util.Base64;
var loader:Loader;
var req:URLRequest;
var orig_mc:MovieClip;
var copy_mc:MovieClip;
function loaderCompleteHandler(evt:Event) {
//swap data
var ldr:Loader = evt.currentTarget.loader as Loader;
var origImg:Bitmap = (ldr.content as Bitmap);
var origBmd:BitmapData = origImg.bitmapData;
trace(origImg.bitmapData);
trace(origImg.width);
trace(origImg.height);
//Convert image byteData into Base64 String
var byteArray:ByteArray = new ByteArray();
byteArray.writeObject(origBmd);
var encoded:String = Base64.encodeByteArray(byteArray);
trace("\nENCODED:\n" + encoded);
var decoded:ByteArray = Base64.decodeToByteArray(encoded);
trace("\nDECODED:\n" + decoded.toString());
// convert base64 string back into movieclip
var newBmd:BitmapData = new BitmapData(origImg.width,origImg.height,"true",0xFFFFFFFF);
newBmd.setPixels(origBmd.rect, decoded);**// THIS THROWS AN " Error #2030: End of file was encountered."**
var image:Bitmap = new Bitmap(newBmd, "auto", true);
copy_mc.addChild(image);
copy_mc.x = origImg.width;
}
loader = new Loader();
req = new URLRequest("C:\\Data\\cutcord.jpg");
loader.load(req);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
// movieclip to display original image
orig_mc = new MovieClip();
orig_mc.addChild(loader);
addChild(orig_mc);
// movieclip to display image copy
copy_mc = new MovieClip();
addChild(copy_mc);
Anyone can help would be very appreciated :)
答案 0 :(得分:6)
试试这段代码:
var rect:Rectangle = new Rectangle(0,0,img.widht,img.height);
var byteArray:ByteArray = bitmapData.getPixels(rect);
var encoded:String = Base64.encodeByteArray(byteArray);
var decoded:ByteArray = Base64.decodeToByteArray(encoded);
decoded.position = 0;
var newBmd:BitmapData = new BitmapData(rect.width,rect.height,true,0xFFFFFFFF);
newBmd.setPixels(rect, decoded);
var image:Bitmap = new Bitmap(newBmd, "auto", true);
addChild(image);
无论如何,你在服务器中存储字符串的想法不太好......为什么你不发送ByteArray,而不是字符串? 您还可以将bitmapData编码为JPG或PNG,然后将字节发送到php。
答案 1 :(得分:3)
因为它是一个图像,你把它放在一个BitmapData中,让我们说“myBmp”...然后使用下面的方法从BitmapData中提取所有数据:
var bytes:ByteArray = myBmp.getPixels(myBmp.rect);
以及以下内容:
myBmp.setPixels(myBmp.rect, bytes);
请注意,只有原始32位像素数据存储在ByteArray中,没有压缩,也没有原始图像的尺寸。
对于压缩,你应该参考corelib。