[AS3]as3压缩位图BitmapData.encode用法源代码示例(2)
AIR 的,其实,AIR的本来只想做一个处理的过程,做着做着就做多了。揉在一起了。文档类:JpegEncodeDemo.as package { importcom.vini123.tool.Button; importcom.vini123.tool.ScrollBar; importcom.vini123.utils
AIR 的,其实,AIR的本来只想做一个处理的过程,做着做着就做多了。揉在一起了。文档类:JpegEncodeDemo.as
- package
- {
- import com.vini123.tool.Button;
- import com.vini123.tool.ScrollBar;
- import com.vini123.utils.Box;
- import flash.display.Bitmap;
- import flash.display.BitmapData;
- import flash.display.JPEGEncoderOptions;
- import flash.display.Loader;
- import flash.display.LoaderInfo;
- import flash.display.PixelSnapping;
- import flash.display.Sprite;
- import flash.display.StageAlign;
- import flash.display.StageScaleMode;
- import flash.events.Event;
- import flash.events.FileListEvent;
- import flash.events.MouseEvent;
- import flash.filesystem.File;
- import flash.filesystem.FileMode;
- import flash.filesystem.FileStream;
- import flash.geom.Matrix;
- import flash.geom.Point;
- import flash.geom.Rectangle;
- import flash.net.FileFilter;
- import flash.net.URLRequest;
- import flash.utils.ByteArray;
- [SWF(width = "790", height = "612",frameRate = "30")]
- public class JpegEncodeDemo extends Sprite
- {
- private var addBtn:Button;
- private var creatBtn:Button;
- private var maskSp:Sprite;
- private var resultSp:Sprite;
- private var scrollBar:ScrollBar;
- private var bg:Bg;
- private var resultPos:Point;
- private var _file:File;
- private var _fs:FileStream
- private var index:int = 0;
- private var urlList:Vector.<String> = new Vector.<String>();
- private var boxList:Vector.<Box> = new Vector.<Box>();
- public function JpegEncodeDemo()
- {
- bg = new Bg();
- addChild(bg);
- stagestage.nativeWindow.maxSize = stage.nativeWindow.minSize = new Point(stage.stageWidth,stage.stageHeight);
- stage.nativeWindow.title = "用我三生烟火,换你一世迷离:图片生成器";
- stage.scaleMode = StageScaleMode.NO_SCALE;
- stage.align = StageAlign.TOP_LEFT;
- addBtn = new Button("添加图片");
- addChild(addBtn);
- creatBtn = new Button("生成图片");
- addChild(creatBtn);
- addBtn.x = 5;
- addBtn.y = 5;
- creatBtn.x = addBtn.x + addBtn.width + 5;
- creatBtn.y = 5;
- addBtn.buttonMode = true;
- addBtn.name = "addBtn";
- addBtn.addEventListener(MouseEvent.CLICK,clickHandler);
- creatBtn.buttonMode = true;
- creatBtn.name = "creatBtn";
- creatBtn.addEventListener(MouseEvent.CLICK,clickHandler);
- resultSp = new Sprite();
- resultSp.x = 5;
- resultSp.y = 35
- addChild(resultSp);
- resultSp.alpha = 1;
- resultPos = new Point(5,35);
- scrollBar = new ScrollBar();
- addChild(scrollBar);
- scrollBar.setSize(stage.stageHeight - 35 - 5,15,0x65A5FA,0xADD0FC,0x2D72B5,0,true);
- scrollBar.scrollTarget = resultSp;
- maskSp = new Sprite();
- maskSp.graphics.beginFill(0x00ffff,0.2);
- maskSp.graphics.drawRect(0,0,stage.stageWidth - scrollBar.width -5,scrollBar.height);
- maskSp.graphics.endFill();
- addChild(maskSp);
- maskSp.x = 5,
- maskSp.y = 35;
- resultSp.mask = maskSp;
- resizeHandler();
- stage.addEventListener(Event.RESIZE,resizeHandler);
- }
- private function clickHandler(e:MouseEvent):void
- {
- var tarName:String = e.currentTarget.name;
- switch(tarName)
- {
- case "addBtn":
- addImage();
- break;
- case "creatBtn":
- creatImage();
- break;
- }
- }
- private function addImage():void
- {
- _file = File.desktopDirectory;
- // _file.browseForDirectory("Please import a Directory that contains image");
- // _file.addEventListener(Event.SELECT,addSelectHandler);
- var fileFilter:FileFilter = new FileFilter("Images(*.jpg;*.gif;*.png;*.jpeg)","*.jpg;*.gif;*.png;*.jpeg");
- _file.browseForOpenMultiple("Please import image files",[fileFilter]);
- _file.addEventListener(FileListEvent.SELECT_MULTIPLE,selectEnd);
- }
- private function selectEnd(e:FileListEvent):void
- {
- e.target.removeEventListener(FileListEvent.SELECT_MULTIPLE,selectEnd);
- var len:int = e.files.length;
- index = urlList.length;
- for(var i:int =0 ; i < len ; i++)
- {
- trace(File(e.files[i]).size/(1024*1024) + "M");
- urlList.push(File(e.files[i]).nativePath);
- }
- load();
- }
- private function creatImage():void
- {
- _file = File.desktopDirectory;
- _file.browseForDirectory("Please select a Directory that place this image");
- _file.addEventListener(Event.SELECT,creatSelectHandler);
- }
- private function creatSelectHandler(e:Event):void
- {
- e.target.removeEventListener(Event.SELECT,creatSelectHandler);
- var path:String = (e.target as File).nativePath;
- var len:int = boxList.length;
- var jpegEncoder:JPEGEncoderOptions ;
- var ba:ByteArray;
- for(var i:int = 0 ; i< len; i++)
- {
- jpegEncoder = new JPEGEncoderOptions(80);
- var rec:Rectangle = boxList[i].bitmap.bitmapData.rect;
- ba = boxList[i].bitmap.bitmapData.encode(rec,jpegEncoder);
- _file_file = _file.resolvePath(path + "/" + i + ".jpg");
- _fs = new FileStream();
- _fs.open(_file,FileMode.WRITE);
- _fs.writeBytes(ba);
- _fs.addEventListener(Event.COMPLETE,creatFull);
- _fs.close();
- ba.clear();
- }
- }
- private function creatFull(e:Event):void
- {
- }
- private function addSelectHandler(e:Event):void
- {
- e.target.removeEventListener(Event.SELECT,creatSelectHandler);
- if(e.target.isDirectory)
- {
- index = urlList.length;
- var tempArr:Array = e.target.getDirectoryListing();
- for(var i:int = 0 ; i< tempArr.length; i++)
- {
- var str:String = (tempArr[i] as File).nativePath;
- var type:String = (str.slice(str.lastIndexOf(".")+1)).toLowerCase();
- if(type == "jpg" || type == "png" || type == "gif" || type == "jpeg")
- {
- urlList.push(str);
- }
- }
- }
- load();
- }
- private function load():void
- {
- if(index >= urlList.length)
- {
- return;
- }
- var loader:Loader = new Loader();
- var request:URLRequest = new URLRequest();
- request.url = urlList[index];
- loader.load(request);
- loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
- }
- private function completeHandler(e:Event):void
- {
- e.target.removeEventListener(Event.COMPLETE,completeHandler);
- var box:Box = new Box();
- var bm:Bitmap = e.target.content as Bitmap;
- bm.smoothing = true;
- bm.pixelSnapping = PixelSnapping.AUTO;
- var poinSize:Point = setWh(bm,1280,720)
- var bd:BitmapData = new BitmapData(poinSize.x,poinSize.y,false);
- bd.draw(bm,new Matrix(poinSize.x/bm.width,0,0,poinSize.y/bm.height,0,0));
- bm = new Bitmap(bd,PixelSnapping.AUTO,true);
- box.bitmap = bm;
- resultSp.addChild(box);
- boxList.push(box);
- box.x = (index % 3) * box.width;
- box.y = Math.floor((index / 3)) * box.height;
- box.index = index;
- box.deleBtn.addEventListener(MouseEvent.CLICK,boxClickHandler)
- //(e.target as LoaderInfo).loader.unloadAndStop();
- index ++;
- scrollBar.scrollTarget = resultSp;
- load();
- }
- private function setWh(bm:Bitmap,_w,_h):Point
- {
- var po:Point = new Point();
- if(_w/_h > bm.width/bm.height)
- {
- po.x = _w;
- po.y = bm.height * _w/bm.width;
- }
- else
- {
- po.x = bm.width * _h/bm.height;
- po.y = _h;
- }
- return po;
- }
- private function boxClickHandler(e:MouseEvent):void
- {
- if(e.currentTarget.name == "deleBtn")
- {
- var tempIndex:int = e.currentTarget.parent.index;
- for(var i:int = 0 ; i< boxList.length ;i++)
- {
- if(boxList[i].index == tempIndex)
- {
- urlList.splice(i,1);
- boxList.splice(i,1);
- (e.currentTarget.parent as Box).dispose();
- e.currentTarget.removeEventListener(MouseEvent.CLICK,boxClickHandler);
- index = urlList.length;
- }
- }
- }
- resultSp.removeChildren();
- for(i = 0 ; i< boxList.length ;i++)
- {
- var box:Box = boxList[i];
- resultSp.addChild(box);
- box.x = (i % 3) * box.width;
- box.y = Math.floor((i / 3)) * box.height;
- scrollBar.scrollTarget = resultSp;
- }
- trace(resultSp.x,resultSp.y)
- trace(boxList.length);
- }
- private function resizeHandler(e:Event = null):void
- {
- scrollBar.x = stage.stageWidth - scrollBar.width - 5;
- scrollBar.y = resultPos.y;
- trace(stage.stageWidth ,stage.stageHeight);
- }
- }
- }
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...