ActionScript-byteArray转Bitmap

ActionScript-byteArray转Bitmap

清晨说ぺ晚安 发布于 2017-01-30 字数 78 浏览 1110 回复 3

在AS3中,我把一张png图片的byteArray数据,请问如何把它转成Bitmap对象啊?

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

偏爱自由 2017-10-05 3 楼

分享一个网上的,ByteArray和BitMap的相互转换,以及扣图

package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.utils.ByteArray;

public class ByteMapArray extends Sprite
{
public var url:String=new String("up/15.png");
public var loader:Loader=new Loader();
public var bitmapdata:BitmapData
public var bytearr:ByteArray=new ByteArray();
public var bytemap:Bitmap=new Bitmap();
public var newmap:Bitmap=new Bitmap();
public function ByteMapArray()
{
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandle);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorHandle);
loader.load(new URLRequest(url));
//this.addChild(loader);

}
public function completeHandle(evt:Event):void
{
trace("sucess");
bitmapdata=new BitmapData(80,90);
bitmapdata.draw(loader);
var temp:ByteArray=bitmapdata.getPixels(bitmapdata.rect);
bytearr.writeBytes(temp);

var bitmapdata2:BitmapData=new BitmapData(loader.width,loader.height);
bytearr.position=0;
bitmapdata2.setPixels(bitmapdata.rect,bytearr);
bytemap.bitmapData=bitmapdata2;
this.addChild(bytemap);

var picRect:Rectangle=new Rectangle(35,50,20,20);//从哪里开始扣,扣多少?
var point:Point=new Point(20,20);//将抠图放在newBitmapdata的那个位置
var newBitmapdata:BitmapData=new BitmapData(100,100);
newBitmapdata.copyPixels(bitmapdata2,picRect,point);
newmap.bitmapData=newBitmapdata;
newmap.x=200;
newmap.y=200;
this.addChild(newmap);
}
public function errorHandle(evt:IOErrorEvent):void
{
trace("error");
}

}
}

偏爱自由 2017-05-06 2 楼

既然你有Bytes了 用loader.loadBytes就可以 然后取loader.content 应该是个Bitmap

浮生未歇 2017-05-05 1 楼

看到过一个类似的问题:AS3/Air: PNG > File > FileStream > ByteArray > BitmapData
希望对你有帮助。
in the package flash.display, use Loader::loadBytes ... that'll give you a Bitmap, and the BitmapData can then be simply accessed through Bitmap::bitmapData ... this makes the whole operation asynchronous, of course ... the only thing you could do, is write a decoder yourself ...

now there is a PNG encoder in AS3, in the as3corelib and i guess there are even others, but probably most people considered it pointless to write a decoder, since flash does this in its own, and also encoding is easier than decoding, because decoding means, you have to implement the whole format ... still, you can give it a shot of course ...