I think this is a useful class for web application. just a simple example, you can encode a BitmapData object into Base64 string by the encodeBase64 method and save it into database, when you need to use the BitmapData, use the decodeBase64 method to decode Base64 string into BitmapData object.
package com.foxarc.images {
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.utils.ByteArray;
import com.foxarc.util.Base64;
public class BitmapEncoder {
public static function encodeByteArray(data:BitmapData):ByteArray{
if(data == null){
throw new Error("data parameter can not be empty!");
}
var bytes:ByteArray = data.getPixels(data.rect);
bytes.writeShort(data.width);
bytes.writeShort(data.height);
bytes.writeBoolean(data.transparent);
bytes.compress();
return bytes;
}
public static function encodeBase64(data:BitmapData):String{
return Base64.encodeByteArray(encodeByteArray(data));
}
public static function decodeByteArray(bytes:ByteArray):BitmapData{
if(bytes == null){
throw new Error("bytes parameter can not be empty!");
}
bytes.uncompress();
if(bytes.length < 6){
throw new Error("bytes parameter is a invalid value");
}
bytes.position = bytes.length - 1;
var transparent:Boolean = bytes.readBoolean();
bytes.position = bytes.length - 3;
var height:int = bytes.readShort();
bytes.position = bytes.length - 5;
var width:int = bytes.readShort();
bytes.position = 0;
var datas:ByteArray = new ByteArray();
bytes.readBytes(datas,0,bytes.length - 5);
var bmp:BitmapData = new BitmapData(width,height,transparent,0);
bmp.setPixels(new Rectangle(0,0,width,height),datas);
return bmp;
}
public static function decodeBase64(data:String):BitmapData{
return decodeByteArray(Base64.decodeToByteArray(data));
}
public function BitmapEncoder() {
throw new Error("BitmapEncoder is a static class!");
}
}
}
DropShadowFilter is a useful and lovely filter. you can add a drop shadow to anything with this class simple and quickly, The example code of adding drop shadow to an image is the following:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
creationComplete="transformImage()" verticalScrollPolicy="off" horizontalScrollPolicy="off">
<mx:Script>
<![CDATA[
[Bindable]
[Embed(source="images/demo.jpg")]
private var demoImage:Class;
private function transformImage():void
{
var filter : DropShadowFilter = new DropShadowFilter();
filter.blurX = 4;
filter.blurY = 4;
filter.quality = 2;
filter.alpha = 0.5;
filter.angle = 45;
filter.color = 0x202020;
filter.distance = 6;
filter.inner = false;
modifiedImage.filters = [ filter ];
}
]]>
</mx:Script>
<mx:HBox width="900" height="400">
<mx:VBox>
<mx:Label text="Original Image"/>
<mx:Image source="{demoImage}" width="400" height="300"/>
</mx:VBox>
<mx:VBox>
<mx:Label text="Modified Image"/>
<mx:Image id="modifiedImage" source="{demoImage}" width="400" height="300"/>
</mx:VBox>
</mx:HBox>
</mx:Application>
The ColorMatrixFilter class lets you apply a 4 x 5 matrix transformation on the RGBA color and alpha values of every pixel in the input image to produce a result with a new set of RGBA color and alpha values. You can apply the filter to any display object (that is, objects that inherit from the DisplayObject class), such as MovieClip, SimpleButton, TextField, and Video objects, as well as to BitmapData objects. Here is the example to adjusting brightness of a picture.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
creationComplete="transformImage()" verticalScrollPolicy="off" horizontalScrollPolicy="off">
<mx:Script>
<![CDATA[
[Bindable]
[Embed(source="images/demo.jpg")]
private var demoImage:Class;
private function transformImage():void
{
var matrix:Array = new Array();
matrix = matrix.concat([1, 0, 0, 0, 30]); // red
matrix = matrix.concat([0, 1, 0, 0, 30]); // green
matrix = matrix.concat([0, 0, 1, 0, 30]); // blue
matrix = matrix.concat([0, 0, 0, 1, 0]); // alpha
var cmf:ColorMatrixFilter = new ColorMatrixFilter(matrix);
var filtersArray:Array = new Array();
filtersArray.push(cmf);
modifiedImage.filters = filtersArray;
}
]]>
</mx:Script>
<mx:HBox width="420" height="200">
<mx:VBox>
<mx:Label text="Original Image"/>
<mx:Image source="{demoImage}" width="200" height="160"/>
</mx:VBox>
<mx:VBox>
<mx:Label text="Modified Image"/>
<mx:Image id="modifiedImage" source="{demoImage}" width="200" height="160"/>
</mx:VBox>
</mx:HBox>
</mx:Application>
Tags: Flex ColorMatrixFilter brightness
Sometimes we need replace the Flex default context menu, the code below can hide the default menu but can not replace it (with function "hideBuiltInItems").
var contextMenu : ContextMenu = new ContextMenu();
contextMenu.hideBuiltInItems();
The code below tells you how to implement this.
// add javascript function at Flex html file
<script>
function onNsRightClick(e){
if(e.which == 3){
FlexTest.openRightClick();
e.stopPropagation();
}
return false;
}
function onIeRightClick(e){
if(event.button > 1){
FlexTest.openRightClick();
parent.frames.location.replace( 'javascript: parent.falseframe' );
}
return false;
}
if(navigator.appName == "Netscape" ){
document.captureEvents(Event.MOUSEDOWN);
document.addEventListener( "mousedown" , onNsRightClick, true);
}
else{
document.onmousedown=onIeRightClick;
}
</script>
//modify Flex MXML file, add initialize method and MOUSEOVER event
Tags: relpace Flex context menu











