·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3压缩位图BitmapData.encode用法源代码示例(2)

时间:2013-11-04 09:02cnblogs.com
AIR 的,其实,AIR的本来只想做一个处理的过程,做着做着就做多了。揉在一起了。文档类:JpegEncodeDemo.as package { importcom.vini123.tool.Button; importcom.vini123.tool.ScrollBar; importcom.vini123.utils

AIR 的,其实,AIR的本来只想做一个处理的过程,做着做着就做多了。揉在一起了。文档类:JpegEncodeDemo.as

  1. package 
  2.     import com.vini123.tool.Button; 
  3.     import com.vini123.tool.ScrollBar; 
  4.     import com.vini123.utils.Box; 
  5.      
  6.     import flash.display.Bitmap; 
  7.     import flash.display.BitmapData; 
  8.     import flash.display.JPEGEncoderOptions; 
  9.     import flash.display.Loader; 
  10.     import flash.display.LoaderInfo; 
  11.     import flash.display.PixelSnapping; 
  12.     import flash.display.Sprite; 
  13.     import flash.display.StageAlign; 
  14.     import flash.display.StageScaleMode; 
  15.     import flash.events.Event; 
  16.     import flash.events.FileListEvent; 
  17.     import flash.events.MouseEvent; 
  18.     import flash.filesystem.File; 
  19.     import flash.filesystem.FileMode; 
  20.     import flash.filesystem.FileStream; 
  21.     import flash.geom.Matrix; 
  22.     import flash.geom.Point; 
  23.     import flash.geom.Rectangle; 
  24.     import flash.net.FileFilter; 
  25.     import flash.net.URLRequest; 
  26.     import flash.utils.ByteArray; 
  27.      
  28.     [SWF(width = "790"height = "612",frameRate = "30")] 
  29.     public class JpegEncodeDemo extends Sprite 
  30.     { 
  31.         private var addBtn:Button; 
  32.         private var creatBtn:Button; 
  33.         private var maskSp:Sprite; 
  34.         private var resultSp:Sprite; 
  35.         private var scrollBar:ScrollBar; 
  36.         private var bg:Bg; 
  37.          
  38.         private var resultPos:Point; 
  39.          
  40.         private var _file:File; 
  41.         private var _fs:FileStream 
  42.          
  43.         private var index:int = 0
  44.         private var urlList:Vector.<String> = new Vector.<String>(); 
  45.         private var boxList:Vector.<Box> = new Vector.<Box>(); 
  46.  
  47.         public function JpegEncodeDemo() 
  48.         { 
  49.             bg = new Bg(); 
  50.             addChild(bg); 
  51.             stagestage.nativeWindow.maxSize = stage.nativeWindow.minSize = new Point(stage.stageWidth,stage.stageHeight); 
  52.             stage.nativeWindow.title = "用我三生烟火,换你一世迷离:图片生成器"
  53.             stage.scaleMode = StageScaleMode.NO_SCALE; 
  54.             stage.align = StageAlign.TOP_LEFT; 
  55.              
  56.             addBtn = new Button("添加图片"); 
  57.             addChild(addBtn); 
  58.              
  59.             creatBtn = new Button("生成图片"); 
  60.             addChild(creatBtn); 
  61.              
  62.             addBtn.x = 5
  63.             addBtn.y = 5
  64.              
  65.             creatBtn.x = addBtn.x + addBtn.width + 5; 
  66.             creatBtn.y = 5
  67.              
  68.             addBtn.buttonMode = true
  69.             addBtn.name = "addBtn"
  70.             addBtn.addEventListener(MouseEvent.CLICK,clickHandler); 
  71.             creatBtn.buttonMode = true
  72.             creatBtn.name = "creatBtn"
  73.             creatBtn.addEventListener(MouseEvent.CLICK,clickHandler); 
  74.              
  75.             resultSp = new Sprite(); 
  76.             resultSp.x = 5
  77.             resultSp.y = 35 
  78.             addChild(resultSp); 
  79.             resultSp.alpha = 1
  80.             resultPos = new Point(5,35); 
  81.              
  82.             scrollBar = new ScrollBar(); 
  83.             addChild(scrollBar); 
  84.              
  85.             scrollBar.setSize(stage.stageHeight - 35 - 5,15,0x65A5FA,0xADD0FC,0x2D72B5,0,true); 
  86.             scrollBar.scrollTarget = resultSp
  87.              
  88.             maskSp = new Sprite(); 
  89.             maskSp.graphics.beginFill(0x00ffff,0.2); 
  90.             maskSp.graphics.drawRect(0,0,stage.stageWidth - scrollBar.width -5,scrollBar.height); 
  91.             maskSp.graphics.endFill(); 
  92.             addChild(maskSp); 
  93.             maskSp.x = 5
  94.             maskSp.y = 35
  95.             resultSp.mask = maskSp
  96.              
  97.              
  98.             resizeHandler(); 
  99.             stage.addEventListener(Event.RESIZE,resizeHandler); 
  100.              
  101.         } 
  102.          
  103.         private function clickHandler(e:MouseEvent):void 
  104.         { 
  105.             var tarName:String = e.currentTarget.name; 
  106.              
  107.             switch(tarName) 
  108.             { 
  109.                 case "addBtn": 
  110.                     addImage(); 
  111.                     break; 
  112.                 case "creatBtn": 
  113.                     creatImage(); 
  114.                     break; 
  115.             } 
  116.         } 
  117.          
  118.         private function addImage():void 
  119.         { 
  120.             _file = File.desktopDirectory; 
  121. //            _file.browseForDirectory("Please import a Directory that contains image"); 
  122. //            _file.addEventListener(Event.SELECT,addSelectHandler); 
  123.              
  124.             var fileFilter:FileFilter = new FileFilter("Images(*.jpg;*.gif;*.png;*.jpeg)","*.jpg;*.gif;*.png;*.jpeg"); 
  125.             _file.browseForOpenMultiple("Please import image files",[fileFilter]); 
  126.             _file.addEventListener(FileListEvent.SELECT_MULTIPLE,selectEnd); 
  127.         } 
  128.          
  129.         private function selectEnd(e:FileListEvent):void 
  130.         { 
  131.             e.target.removeEventListener(FileListEvent.SELECT_MULTIPLE,selectEnd); 
  132.              
  133.             var len:int = e.files.length; 
  134.             index = urlList.length; 
  135.              
  136.             for(var i:int =0 ; i < len ; i++) 
  137.             { 
  138.                 trace(File(e.files[i]).size/(1024*1024) + "M"); 
  139.                  
  140.                 urlList.push(File(e.files[i]).nativePath); 
  141.             } 
  142.              
  143.             load(); 
  144.         } 
  145.          
  146.         private function creatImage():void 
  147.         { 
  148.             _file = File.desktopDirectory; 
  149.             _file.browseForDirectory("Please select a Directory that place this image"); 
  150.             _file.addEventListener(Event.SELECT,creatSelectHandler); 
  151.         } 
  152.          
  153.         private function creatSelectHandler(e:Event):void 
  154.         { 
  155.             e.target.removeEventListener(Event.SELECT,creatSelectHandler); 
  156.              
  157.             var path:String = (e.target as File).nativePath; 
  158.             var len:int = boxList.length; 
  159.              
  160.             var jpegEncoder:JPEGEncoderOptions ; 
  161.             var ba:ByteArray; 
  162.              
  163.              
  164.             for(var i:int = 0 ; i< len; i++) 
  165.             { 
  166.                 jpegEncoder = new  JPEGEncoderOptions(80); 
  167.                 var rec:Rectangle =  boxList[i].bitmap.bitmapData.rect; 
  168.                 ba = boxList[i].bitmap.bitmapData.encode(rec,jpegEncoder); 
  169.                 _file_file = _file.resolvePath(path + "/" + i + ".jpg"); 
  170.                 _fs = new FileStream(); 
  171.                 _fs.open(_file,FileMode.WRITE); 
  172.                 _fs.writeBytes(ba); 
  173.                 _fs.addEventListener(Event.COMPLETE,creatFull); 
  174.                 _fs.close(); 
  175.                 ba.clear(); 
  176.             } 
  177.         } 
  178.          
  179.         private function creatFull(e:Event):void 
  180.         { 
  181.              
  182.          
  183.         } 
  184.          
  185.         private function addSelectHandler(e:Event):void 
  186.         { 
  187.             e.target.removeEventListener(Event.SELECT,creatSelectHandler); 
  188.             if(e.target.isDirectory) 
  189.             { 
  190.                 index = urlList.length; 
  191.                 var tempArr:Array = e.target.getDirectoryListing(); 
  192.                 for(var i:int = 0 ; i< tempArr.length; i++) 
  193.                 { 
  194.                     var str:String = (tempArr[i] as File).nativePath; 
  195.                     var type:String = (str.slice(str.lastIndexOf(".")+1)).toLowerCase(); 
  196.                     if(type == "jpg" || type == "png" || type == "gif" || type == "jpeg") 
  197.                     { 
  198.                         urlList.push(str); 
  199.                     } 
  200.                 } 
  201.             } 
  202.              
  203.             load(); 
  204.         } 
  205.          
  206.         private function load():void 
  207.         { 
  208.             if(index >= urlList.length) 
  209.             { 
  210.                 return; 
  211.             } 
  212.             var loader:Loader = new Loader(); 
  213.             var request:URLRequest = new URLRequest(); 
  214.             request.url = urlList[index]; 
  215.             loader.load(request); 
  216.             loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler); 
  217.         } 
  218.          
  219.  
  220.              
  221.         private function completeHandler(e:Event):void 
  222.         { 
  223.             e.target.removeEventListener(Event.COMPLETE,completeHandler); 
  224.              
  225.             var box:Box =  new Box(); 
  226.             var bm:Bitmap = e.target.content as Bitmap; 
  227.             bm.smoothing = true
  228.             bm.pixelSnapping = PixelSnapping.AUTO; 
  229.              
  230.             var poinSize:Point = setWh(bm,1280,720) 
  231.                  
  232.             var bd:BitmapData = new BitmapData(poinSize.x,poinSize.y,false); 
  233.             bd.draw(bm,new Matrix(poinSize.x/bm.width,0,0,poinSize.y/bm.height,0,0)); 
  234.             bm = new Bitmap(bd,PixelSnapping.AUTO,true); 
  235.              
  236.                  
  237.             box.bitmap =  bm
  238.             resultSp.addChild(box); 
  239.             boxList.push(box); 
  240.              
  241.             box.x = (index % 3)  * box.width; 
  242.             box.y = Math.floor((index / 3)) * box.height; 
  243.             box.index = index; 
  244.             box.deleBtn.addEventListener(MouseEvent.CLICK,boxClickHandler) 
  245.             //(e.target as LoaderInfo).loader.unloadAndStop(); 
  246.  
  247.             index ++; 
  248.             scrollBar.scrollTarget = resultSp
  249.             load(); 
  250.         } 
  251.          
  252.         private function setWh(bm:Bitmap,_w,_h):Point 
  253.         { 
  254.             var po:Point = new Point(); 
  255.             if(_w/_h > bm.width/bm.height) 
  256.             { 
  257.                 po.x = _w
  258.                 po.y = bm.height * _w/bm.width; 
  259.             } 
  260.             else 
  261.             { 
  262.                 po.x = bm.width * _h/bm.height; 
  263.                 po.y = _h
  264.             } 
  265.             return po; 
  266.         } 
  267.          
  268.         private function boxClickHandler(e:MouseEvent):void 
  269.         { 
  270.             if(e.currentTarget.name == "deleBtn") 
  271.             { 
  272.                 var tempIndex:int = e.currentTarget.parent.index; 
  273.                 for(var i:int = 0 ; i< boxList.length ;i++) 
  274.                 { 
  275.                     if(boxList[i].index == tempIndex) 
  276.                     { 
  277.                         urlList.splice(i,1); 
  278.                         boxList.splice(i,1); 
  279.                         (e.currentTarget.parent as Box).dispose(); 
  280.                         e.currentTarget.removeEventListener(MouseEvent.CLICK,boxClickHandler); 
  281.                         index = urlList.length; 
  282.                     } 
  283.                 } 
  284.             } 
  285.              
  286.             resultSp.removeChildren(); 
  287.              
  288.             for(i = 0 ; i< boxList.length ;i++) 
  289.             { 
  290.                 var box:Box = boxList[i]; 
  291.                 resultSp.addChild(box); 
  292.                 box.x = (i % 3)  * box.width; 
  293.                 box.y = Math.floor((i / 3)) * box.height; 
  294.                 scrollBar.scrollTarget = resultSp
  295.             } 
  296.             trace(resultSp.x,resultSp.y) 
  297.             trace(boxList.length); 
  298.         } 
  299.          
  300.         private function resizeHandler(e:Event = null):void 
  301.         { 
  302.             scrollBar.x = stage.stageWidth - scrollBar.width - 5; 
  303.             scrollBar.y = resultPos.y; 
  304.             trace(stage.stageWidth ,stage.stageHeight); 
  305.         } 
  306.     } 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器