从Noob到As3位图的东西...

当我尝试执行以下代码时,它将失败

bmd.setPixels(bmd.rect,解码值);

错误消息是:
错误:错误#2030:遇到文件结尾。

情况是我已经通过转换byteArray将图像作为二进制存储到数据库中,现在我想检索它并转换回图像。

只是为了清除这种情况,需要将ByteArray放入位图,然后可以将其添加到影片剪辑中了吗?

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 :)

最佳答案

试试这个代码:

   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而不是String?
您还可以将bitmapData编码为JPG或PNG,然后将字节发送到php。

关于actionscript-3 - As3将ByteArray转换为位图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8797719/

10-16 09:55