gpt4 book ai didi

actionscript-3 - As3 将 ByteArray 转换为 Bitmap

转载 作者:行者123 更新时间:2023-12-02 00:58:20 24 4
gpt4 key购买 nike

新手到 As3 位图的东西...

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

bmd.setPixels(bmd.rect,decodeValue);

错误消息是:错误:错误#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 转换为 Bitmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8797719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com