美化图片滤镜
效果:
转载请注明出处:http://www.cnblogs.com/TheViper/p/4031768.html
新浪微博,美图秀秀还有其他的那些都是把滤镜的swf编译进了他的flex里面,不好取出,只有qq空间的是按需加载,点击一个滤镜,加载一个滤镜swf,所以qq空间,对不起了。。。
1.获取滤镜的swf,去qq空间的美化图片,点击一个滤镜,用firebug之类的得到滤镜,最后得到的滤镜:
"abao.swf","colorfulmood.swf","enhance.swf","lordkelvin.swf","blackenhance.swf","sweetjelly.swf","lightred.swf","aladin.swf","mellow.swf","rainbow.swf","romanticstar.swf","smokedval.swf","sweetlight.swf","sweet.swf","nashville.swf","lomored.swf"
对应的滤镜名称:
"阿宝色","缤纷心情","自动增强","枫叶","黑白画映","琥珀","亮红","潘多拉","午茶","彩虹","浪漫星光","掠影","甜美光影","甜美可人","往昔","晚秋"
2.用Loader类加载滤镜swf,对swf的content var a:BitmapData =content.execute(bmpData);bmpData 是目标图片的bitmapdata..那个execute方法是接口暴露出来渲染滤镜效果的方法,,qq空间美化图片里面美化和磨皮的swf也是用的类似方法。
其他都是些小细节,很简单的。
1 package 2 { 3 import flash.display.*; 4 import flash.events.Event; 5 import flash.net.*; 6 import fl.controls.*; 7 import flash.events.*; 8 import flash.geom.*; 9 import flash.utils.getDefinitionByName; 10 import bloom.containers.*; 11 import bloom.core.Margin; 12 import bloom.themes.*; 13 import bloom.*; 14 import flash.text.*; 15 import com.adobe.images.JPGEncoder; 16 import flash.utils.ByteArray; 17 import flash.external.ExternalInterface; 18 19 public class Main extends MovieClip 20 { 21 var urlLoader:Loader=new Loader(); 22 var urlLoader1:Loader=new Loader(); 23 var image:DisplayObject; 24 private var _bmd:BitmapData; 25 private var scrollContainer:ScrollContainer; 26 private var filter_bm:Bitmap; 27 private var ori_height; 28 private var ori_width; 29 private var filter_name=["阿宝色","缤纷心情","自动增强","枫叶","黑白画映","琥珀","亮红","潘多拉","午茶","彩虹","浪漫星光","掠影","甜美光影", 30 "甜美可人","往昔","晚秋"]; 31 private var filter_file = ["abao.swf","colorfulmood.swf","enhance.swf","lordkelvin.swf","blackenhance.swf","sweetjelly.swf","lightred.swf", 32 "aladin.swf","mellow.swf","rainbow.swf","romanticstar.swf","smokedval.swf","sweetlight.swf","sweet.swf","nashville.swf","lomored.swf"]; 33 public function Main() 34 { 35 urlLoader1.load(new URLRequest("2.jpg")); 36 urlLoader1.contentLoaderInfo.addEventListener( Event.COMPLETE,load_complete); 37 ThemeBase.setTheme(new ColorTheme()); 38 scrollContainer = new ScrollContainer(null); 39 scrollContainer.content.addChild(filter_list); 40 scrollContainer.margin.reset(0, 0, 0, 0); 41 scrollContainer.direction = ScrollContainer.VERTICALLY; 42 scrollContainer.calculateContentSize(); 43 scrollContainer.setScrollBar(false, true); 44 scrollContainer.width = 185; 45 scrollContainer.height = 516; 46 stage.addChild(scrollContainer); 47 add_flag(); 48 filter_list.selected_filter.visible = false; 49 filter_list.wrap_small_img.visible = false; 50 filter_list.addEventListener(MouseEvent.MOUSE_OUT,filter_out); 51 back_btn.addEventListener(MouseEvent.CLICK,back_img); 52 ExternalInterface.addCallback("beautify_image_upload",upload_handler); 53 } 54 private function add_flag():void 55 { 56 for (var i=1; i<17; i++) 57 { 58 var cls:Class = getDefinitionByName("filter"+i) as Class; 59 var bmpdata:BitmapData = new cls(0,0); 60 var bmp:Bitmap = new Bitmap(bmpdata); 61 var item:Sprite=new Sprite(); 62 var s:Sprite=new Sprite(); 63 var tf:TextField=new TextField(); 64 s.x =(i%2==0?8:90); 65 s.y=Math.floor((i-1)/2)*102+8; 66 s.name = "filter" + i; 67 tf.text = filter_name[i - 1]; 68 tf.y = 78; 69 tf.x = 0; 70 tf.width = 82; 71 tf.autoSize = TextFieldAutoSize.CENTER; 72 tf.selectable = false; 73 s.addChild(bmp); 74 s.addChild(tf); 75 s.buttonMode = true; 76 s.addEventListener(MouseEvent.MOUSE_OVER,filter_hover); 77 s.addEventListener(MouseEvent.CLICK,filter_click); 78 filter_list.addChild(s); 79 } 80 } 81 private function back_img(event:MouseEvent):void 82 { 83 image.visible = true; 84 if (img_panel.numChildren > 2) 85 { 86 img_panel.removeChildAt(2); 87 } 88 } 89 private function filter_out(event:MouseEvent):void 90 { 91 filter_list.wrap_small_img.visible = false; 92 } 93 private function filter_click(event:MouseEvent):void 94 { 95 filter_list.selected_filter.x = event.target.x; 96 filter_list.selected_filter.y = event.target.y; 97 filter_list.selected_filter.visible = true; 98 var i = event.target.name.substring(6); 99 var urlRequest:URLRequest = new URLRequest("filter/"+filter_file[i-1]); 100 urlLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, apply_filter); 101 urlLoader.load( urlRequest ); 102 } 103 private function filter_hover(event:MouseEvent):void 104 { 105 if (event.target is Sprite) 106 { 107 filter_list.wrap_small_img.x = event.target.x; 108 filter_list.wrap_small_img.y = event.target.y; 109 filter_list.wrap_small_img.visible = true; 110 } 111 } 112 function load_complete( event:Event ):void 113 { 114 image = event.target.content; 115 ori_height = image.height; 116 ori_width = image.width; 117 scaleInBox(image,560,516); 118 do_center(image); 119 img_panel.addChild(image); 120 } 121 private function do_center(target:DisplayObject):void 122 { 123 image.x = (560 - target.width) * 0.5; 124 image.y = (516 - target.height) * 0.5; 125 //trace(img_panel.numChildren); 126 } 127 private function apply_filter(event:Event):void 128 { 129 urlLoader.contentLoaderInfo.removeEventListener( Event.COMPLETE, apply_filter); 130 var _content = event.target.content; 131 trace(image.height); 132 var scale:Number = Math.min((560 / ori_width),516 / ori_height); 133 var bmpData:BitmapData = new BitmapData(image.width,image.height); 134 var matrix:Matrix = new Matrix(); 135 matrix.scale(scale, scale); 136 bmpData.draw(image,matrix); 137 var a = _content.execute(bmpData); 138 var filter_bm:Bitmap=new Bitmap(); 139 filter_bm.bitmapData = a; 140 image.visible = false; 141 filter_bm.x = (560 - filter_bm.width) * 0.5; 142 filter_bm.y = (516 - filter_bm.height) * 0.5; 143 if (img_panel.numChildren > 2) 144 { 145 img_panel.removeChildAt(2); 146 } 147 img_panel.addChild(filter_bm); 148 } 149 private function upload_handler(event:MouseEvent):void 150 { 151 var bitmapData:BitmapData = filter_bm.bitmapData; 152 var _encoder:JPGEncoder = new JPGEncoder(100); 153 bitmapData.draw(bitmapData); 154 var Data:ByteArray = _encoder.encode(bitmapData); 155 var req:URLRequest = new URLRequest("http://localhost/youtube/util/upload.php"); 156 req.data = Data; 157 req.method = URLRequestMethod.POST; 158 req.contentType = "application/octet-stream"; 159 var loader:URLLoader = new URLLoader ; 160 loader.dataFormat = URLLoaderDataFormat.BINARY; 161 loader.load(req); 162 loader.addEventListener(Event.COMPLETE,upload_complete); 163 } 164 private function upload_complete(event:Event):void 165 { 166 ExternalInterface.call("upload_complete"); 167 } 168 private function scaleInBox(target:DisplayObject,boxWidth:Number,boxHeight:Number):void 169 { 170 var scale:Number = Math.min((boxWidth / target.width),boxHeight / target.height); 171 if (scale<1) 172 { 173 target.scaleX = scale; 174 target.scaleY = scale; 175 } 176 177 } 178 } 179 }