As package:
Package com. PAG
{
Import flash. display. Bitmap;
Import flash. display. bitmapdata;
Import flash. Geom. rectangle;
Import flash. utils. bytearray;
Import MX. Controls. Alert;
Public class bitmapbytes
{
Public Function bitmapbytes ()
{
Super ();
}
Public static function bitmapdatatobytearray (target: Bitmap): bytearray {
VaR imagewidth: uint = target. width;
VaR imageheigh: uint = target. height;
VaR srcbmp: bitmapdata = new bitmapdata (imagewidth, imageheigh );
Srcbmp. Draw (target );
VaR pixels: bytearray = srcbmp. getpixels (New rectangle (0, 0, imagewidth, imageheigh ));
Pixels. writeshort (imageheigh );
Pixels. writeshort (imagewidth );
Alert. Show ("" + pixels );
Return pixels;
}
Public static function bytearraytobitmap (byarr: bytearray): bitmap {
If (byarr = NULL)
Return NULL;
VaR BMI: bytearray = byarr;
>;
VaR imagewidth: Int = BMI. readshort ();
Bone mineral density. Position = Bone Mineral Density. Length-4;
VaR imageheight: Int = BMI. readshort ();
VaR copybmp: bitmapdata = new bitmapdata (imagewidth, imageheight, true );
BMI. Position = 0;
For (var I: uint = 0; I <imageheight; I ++ ){
For (var j: uint = 0; j <imagewidth; j ++ ){
Copybmp. setpixel (J, I, BMI. readunsignedint ());
}
}
VaR BMP: bitmap = new Bitmap (copybmp );
Return BMP;
}
}
}
Mxml file:
<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute" creationcomplete = "Init ();">
<Mx: SCRIPT>
<! [CDATA [
Import com. PAG. bitmapbytes;
Private Static Var BMP: bitmap;
Private var Loader: loader;
Private function Init (): void {
// Var img1: Image = new image ();
// Img1.source = "img/3.jpg ";
// Var byarr: bytearray = bitmapbytes. bitmapdatatobytearray (img1 );
// BMP = bitmapbytes. bytearraytobitmap (byarr );
Loader = new loader ();
VaR request: URLRequest = new URLRequest ("img/3.jpg ");
Loader. contentloaderinfo. addeventlistener (event. Complete, getbmp info );
Loader. Load (request );
}
Private function getbmp Info (Event: Event): void {
VaR bitmp: bitmap = (Bitmap) (loader. content );
VaR byarr: bytearray = bitmapbytes. bitmapdatatobytearray (bitmp );
BMP = bitmapbytes. bytearraytobitmap (byarr );
Trace (BMP );
// Img2.source = BMP;
// Img3.source = BMP;
}
Private function changto (): void {
Trace (BMP );
Img2.source = BMP;
}
Private function changto2 (): void {
Trace (BMP );
Img3.source = BMP;
}
]>
</MX: SCRIPT>
<Mx: Image id = "img2" x = "63"/>
<Mx: button click = "changto ();" x = "0" Y = "0" label = "button"/>
<Mx: button click = "changto2 ();" x = "471" Y = "10" label = "button"/>
<Mx: Image id = "img3" x = "535" Y = "2"/>
</MX: Application>