Flex 3 Cookbook: Chapter 8, Images, Bitmaps, Videos, Sounds
Pages: 1, 2, 3, 4, 5, 6, 7, 8
Its parameters are as follows:
sourceBitmapData:BitmapDataBitmapData object
or the current BitmapData
object.sourceRect:RectangledestPoint:PointBitmapData instance) that corresponds
to the upper-left corner of the source rectangle.redMultiplier:uintuint value by which to multiply the
red channel value.greenMultiplier:uintuint
value by which to multiply the green channel
value.blueMultiplier:uintuint value by which to multiply the
blue channel value.alphaMultiplier:uintuint value by which to multiply the
alpha transparency value.A complete code listing follows with modifiable controls to
alter the values of the ColorTransform:
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="500" height="550"
creationComplete="imgMod()">
<mx:Script>
<![CDATA[
import mx.core.BitmapAsset;
import mx.controls.Image;
[Embed(source="../assets/bigshakey.png")]
private var shakey:Class;
[Embed(source="../assets/mao.jpg")]
private var mao:Class;
//superimpose the two images together
//using the vslider data
private function imgMod():void
{
var maoData:BitmapData = new BitmapData(firstImg.width,
firstImg.height);
var shakeyData:BitmapData = new BitmapData(secondImg.width,
secondImg.height);
maoData.draw(firstImg);
shakeyData.draw(secondImg);
maoData.colorTransform(new Rectangle(0, 0, maoData.width,
maoData.height), new ColorTransform(redSlider.value/10, greenSlider.value/10,
blueSlider.value/10,alphaSlider.value/10));
var red:uint = (uint(redSlider.value.toString(16)) / 10) * 160;
var green:uint = (uint(greenSlider.value.toString(16)) / 10) * 160;
var blue:uint = (uint(blueSlider.value.toString(16)) / 10) * 160;
var alpha:uint = (uint(alphaSlider.value.toString(16)) / 10) * 160;
shakeyData.merge(maoData, new Rectangle(0, 0, shakeyData.width,
shakeyData.height), new Point(0, 0), red, green, blue, alpha);
mainImg.source = new BitmapAsset(shakeyData);
}
]]>
</mx:Script>
<mx:HBox>
<mx:Image id="firstImg" source="{mao}" height="200" width="200"/>
<mx:Image id="secondImg" source="{shakey}" height="200" width="200"/>
</mx:HBox>
<mx:HBox>
<mx:Text text="Red"/>
<mx:VSlider height="100" id="redSlider" value="5.0" change="imgMod()"/>
<mx:Text text="Blue"/>
<mx:VSlider height="100" id="blueSlider" value="5.0" change="imgMod()"/>
<mx:Text text="Green"/>
<mx:VSlider height="100" id="greenSlider" value="5.0" change="imgMod()"/>
<mx:Text text="Alpha"/>
<mx:VSlider height="100" id="alphaSlider" value="5.0" change="imgMod()"/>
</mx:HBox>
<mx:Image id="mainImg"/>
</mx:VBox>
You want to allow users to alter the colors, contrast, or sharpness of an image.
Create an instance of a ConvolutionFilter and bind the properties of
the matrix within the ConvolutionFilter to text inputs that the
user can alter. Then push the filter onto the image's filters array to
apply the filter.
This excerpt is from Flex 3 Cookbook. This highly practical book contains more than 300 proven recipes for developing interactive Rich Internet Applications and Web 2.0 sites. You'll find everything from Flex basics and working with menus and controls, to methods for compiling, deploying, and configuring Flex applications. Each recipe features a discussion of how and why it works, and many of them offer sample code that you can put to use immediately.
ConvolutionFilter is one of
the most versatile and complex filters in the package. It can be
used to emboss, detect edges, sharpen, blur, and perform many other
effects. All the parameters are controlled by a Matrix object representing a three-by-three matrix that is passed to
the filter in its constructor. The ConvolutionFilter conceptually goes through
each pixel in the source image one by one and determines the final
color of that pixel by using the value of the pixel and its
surrounding pixels. A matrix, specified as an array of numeric values,
indicates to what degree the value of each particular neighboring
pixel affects the final resulting value. The constructor is shown
here:
ConvolutionFilter(matrixX:Number = 0, matrixY:Number = 0, matrix:Array = null, divisor:Number = 1.0, bias:Number = 0.0, preserveAlpha:Boolean = true, clamp:Boolean = true, co lor:uint = 0, alpha:Number = 0.0)
Its parameters are as follows:
matrixX:Number (default =
0)matrixY:Number (default =
0)matrix:Array (default =
null)matrixX * matrixY.divisor:Number (default =
1.0)bias:Number (default =
0.0)preserveAlpha:Boolean
(default = true)clamp:Boolean (default =
true)color:uint (default =
0)alpha:Number (default =
0.0)Some common effects for the ConvolutionFilter are as follows:
new ConvolutionFilter(3,3,new
Array(-5,0,1,1,-2,3,-1,2,1),1)new ConvolutionFilter(3,3,new
Array(0,20,0,20,-80,20,0,20,0),10)new ConvolutionFilter(5,5,new
Array(0,1,2,1,0,1,2,4,2,1,2,4,8,4,2,1,2,4,2,
1,0,1,2,1,0),50);new ConvolutionFilter(3,3,new
Array(-2,-1,0,-1,1,1,0,1,2),0);