<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.graphics.codec.PNGEncoder;
import mx.graphics.codec.JPEGEncoder;
import mx.utils.Base64Encoder;
private var encoder:Base64Encoder;
private var encodeTarget:ByteArray;
private function loadImage():void
{
indentityImg.load(imageSrc.text);
}
private function encodeImage():void
{
var bmp:Bitmap = indentityImg.content as Bitmap;
var bmpData:BitmapData = bmp.bitmapData;
switch("jpg"){
case "bmp":
encodeTarget = bmpData.getPixels(bmpData.rect);
break;
case "jpg":
var jpg:JPEGEncoder = new JPEGEncoder(100);
encodeTarget = jpg.encode(bmpData);
break;
case "png":
var png:PNGEncoder = new PNGEncoder();
encodeTarget = png.encode(bmpData);
break;
}
encodeTarget.position = 0;
encoder = new Base64Encoder();
encoder.insertNewLines = false;
encoder.reset();
encoder.encodeBytes(encodeTarget,0,encodeTarget.length);
resultTxt.text = encoder.flush();
}
]]>
</mx:Script>
<mx:Image id="indentityImg" x="10" y="64" width="187" height="112" source="image2.jpg"/>
<mx:Button x="458" y="5" label="fetch" click="loadImage()"/>
<mx:Button x="260" y="169" label="encode" click="encodeImage()"/>
<mx:TextArea x="10" y="210" width="317" height="158" id="resultTxt" wordWrap="true" editable="false"/>
<mx:Label x="10" y="184" text="result" width="115"/>
<mx:TextInput id="imageSrc" x="10" y="5" width="425" editable="true">
<mx:text>http://avatar.profile.csdn.net/7/B/4/1_remote_roamer.jpg</mx:text>
</mx:TextInput>
</mx:Application>