Flex converts an image into a binary image and then converts it to BMP.

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.