BTA Flexing

My passion RIAs…………..Shardul Singh Bartwal

Byte Array to BitmapData and BitmapData to Byte array in Flex 3.

Posted by Shardul Singh Bartwal on October 14, 2010

In the code below I am initially getting the byte array of my canvas with the increased size. Converting it into Bitmap data.And again converting that Bitmap data into ByteArray. So that you can use the particular step which one you require.

</pre>
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:Script>
<![CDATA[
import mx.controls.Alert;

import mx.graphics.ImageSnapshot;
import mx.graphics.codec.JPEGEncoder;

private var imageSnapshot:ImageSnapshot
private function inLargeImage():void
{
var jPEGEncoder:JPEGEncoder = new JPEGEncoder(90);
imageSnapshot = ImageSnapshot.captureImage(imgCanvas,500, jPEGEncoder,true);

//imageSnapshot.data is ByteArray.
byteArrayToBitmapData(imageSnapshot.data);
}

private var loader : Loader = new Loader();
private function byteArrayToBitmapData(byteArray : ByteArray):void
{
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, getBitmapData);
loader.loadBytes(byteArray);
}
private var bitmapData:BitmapData
private function getBitmapData(e:Event):void {
var content:* = loader.content;
bitmapData = new BitmapData(content.width,content.height,true,0x00000000);

var matrix:Matrix = new Matrix();
bitmapData.draw(content, matrix,null,null,null,true);
targetImage.source = new Bitmap( bitmapData );

}

private function saveInLargeImage() : void
{
if(bitmapData == null)
{
Alert.show("InLarge Image first,and then try again.")
return;
}

var encoder:JPEGEncoder = new JPEGEncoder(90);
var byteArray:ByteArray = encoder.encode(bitmapData);
var fr : FileReference= new FileReference();
fr.save(byteArray,'abcd.jpg');
}

]]>
</mx:Script>

<mx:VBox horizontalAlign="center" verticalAlign="middle" width="100%" height="100%">
<mx:VBox id="imgCanvas" width="600" height="450" horizontalAlign="center"
verticalAlign="middle" backgroundColor="0x0000FF">
<mx:Image source="image1.jpg"/>
</mx:VBox>

<mx:Image id="targetImage"/>

<mx:Button click="inLargeImage()" label="Inlarge Image"/>
<mx:Button label="Save To Your Computer" click="saveInLargeImage()"/>
</mx:VBox>

</mx:Application>
<pre>

Hope you will enjoy this……..

Advertisements

2 Responses to “Byte Array to BitmapData and BitmapData to Byte array in Flex 3.”

  1. Ashish said

    Nice example 🙂

  2. PaulS said

    Wonderful exaple. Quite handy, thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: