-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBitmapEffects.as
166 lines (131 loc) · 4.8 KB
/
BitmapEffects.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// *************************
// * COPYRIGHT
// * DEVELOPER: MTEAM ( info@mteamapp.com )
// * ALL RIGHTS RESERVED FOR MTEAM
// * YOU CAN'T USE THIS CODE IN ANY OTHER SOFTWARE FOR ANY PURPOSE
// * YOU CAN'T SHARE THIS CODE
// *************************
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.JPEGEncoderOptions;
import flash.display.PNGEncoderOptions;
import flash.filters.BlurFilter;
import flash.filters.ColorMatrixFilter;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.utils.ByteArray;
import animation.Anim_Frame_Controller;
import flash.display.MovieClip;
import com.Base64;
public class BitmapEffects
{
public static var backGroundColor:uint = 0x00000000 ;
public static function setGrayScale( obj:BitmapData ,RedPercent:Number=1,GreenPercent:Number=1,BluePercent:Number=1) : BitmapData
{
var rLum : Number = 0.2225;
var gLum : Number = 0.7169;
var bLum : Number = 0.0606;
var matrix:Array =
[ rLum*RedPercent, gLum*RedPercent, bLum*RedPercent, 0, 0,
rLum*GreenPercent, gLum*GreenPercent, bLum*GreenPercent, 0, 0,
rLum*BluePercent, gLum*BluePercent, bLum*BluePercent, 0, 0,
0, 0, 0, 1, 0
];
var filter:ColorMatrixFilter = new ColorMatrixFilter( matrix );
obj.applyFilter( obj, new Rectangle( 0,0,obj.width,obj.height ), new Point(0,0), filter );
return obj;
}
//Imgage processing
public static function changeSize(bitmapData:BitmapData,newWidth:Number,newHeight:Number,keepRatio:Boolean=true,keepAllImageInFrame:Boolean=false,changePhotoDymention:Boolean=true):flash.display.BitmapData
{
var imageW:Number = bitmapData.width;
var imageH:Number = bitmapData.height;
var scaleX:Number = newWidth/imageW ;
var scaleY:Number = newHeight/imageH ;
var dx:Number = 0 ;
var dy:Number = 0 ;
if(keepRatio)
{
if(keepAllImageInFrame)
{
scaleX = scaleY = Math.min(scaleX,scaleY);
}
else
{
scaleX = scaleY = Math.max(scaleX,scaleY);
}
imageH = scaleX*imageH ;
imageW = scaleY*imageW ;
dx = (newWidth-imageW)/2;
dy = (newHeight-imageH)/2;
}
var newBitmapData:BitmapData;
if(changePhotoDymention)
{
newBitmapData = new BitmapData(newWidth,newHeight,true,backGroundColor);
newBitmapData.draw(bitmapData,new Matrix(scaleX,0,0,scaleY,dx,dy),null,null,null,true);
}
else
{
trace("Change image size to : "+scaleX);
newBitmapData = new BitmapData(bitmapData.width*scaleX,bitmapData.height*scaleY,true,backGroundColor);
newBitmapData.draw(bitmapData,new Matrix(scaleX,0,0,scaleY,0,0),null,null,null,true);
}
return newBitmapData;
}
/**This function make chane on the original bitma;*/
public static function blur(bitmapData:BitmapData,blurLevel:Number=4.0,quality:uint=1):void
{
bitmapData.applyFilter(bitmapData,bitmapData.rect,new Point(),new BlurFilter(blurLevel,blurLevel,quality));
}
public static function createJPG(bitmapData:BitmapData,quality:Number = 100 ):ByteArray
{
var jpeg:JPEGEncoderOptions = new JPEGEncoderOptions(quality);
return bitmapData.encode(bitmapData.rect,jpeg) ;
}
public static function createPNG(bitmapData:BitmapData):ByteArray
{
var png:PNGEncoderOptions = new PNGEncoderOptions(true);
return bitmapData.encode(bitmapData.rect,png);
}
/**This function will rotate current bitmap to 90 degrees*/
public static function rotateBitmapData(bitmapData:BitmapData,rad:Number=90):BitmapData
{
var r:Number = rad/180*Math.PI;
/*var W:Number = Math.abs(bitmapData.width*Math.cos(r)+bitmapData.height*Math.sin(r));
var H:Number = Math.abs(bitmapData.height*Math.cos(r)+bitmapData.width*Math.sin(r));*/
var sin:Number = Math.sin(r) ;
var cos:Number = Math.cos(r) ;
var W:Number = Math.abs(bitmapData.width*cos)+Math.abs(bitmapData.height*sin);
var H:Number = Math.abs(bitmapData.width*sin)+Math.abs(bitmapData.height*cos);
var rotatedBitmap:BitmapData = new BitmapData(W,H,bitmapData.transparent,0x00ffffff);
var matrix:Matrix = new Matrix(1,0,0,1,0);
matrix.rotate(r);
if(sin>0)
{
matrix.tx = sin*bitmapData.height;
}
else
{
matrix.ty = Math.abs(sin*bitmapData.width) ;
}
if(cos<0)
{
matrix.ty += Math.abs(cos*bitmapData.height);
matrix.tx += Math.abs(cos*bitmapData.width);
}
//matrix.rotate(rad);
rotatedBitmap.draw(bitmapData,matrix,null,null,null,true);
return rotatedBitmap ;
}
public static function createBase64(bitmapData:BitmapData,quality:int=100):String
{
var jpg:JPEGEncoderOptions = new JPEGEncoderOptions(quality);
bitmapData.encode(bitmapData.rect,jpg)
return Base64.EncodeByte(bitmapData.encode(bitmapData.rect,jpg)).split('\n').join('');
}
}
}