flex截图

http://www.cnblogs.com/mklp98999/archive/2008/10/27/1320517.html

http://www.moorwind.com/read.php?9

http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&productId=2&postId=8406(上传一个air保存图片)

Xml代码

  1. <mx:Application
  2. xmlns:mx="http://www.adobe.com/2006/mxml"
  3. layout="absolute">
  4. <mx:Script>
  5. <![CDATA[
  6.   import mx.core.UIComponent;
  7.   private function captureFullScreen() : void    //截屏
  8.   {
  9.     var bd : BitmapData = getBitmapData( UIComponent( mx.core.Application.application ) );
  10.     targetImage.source = new Bitmap( bd );
  11.   }
  12.   private function captureHiddenDatagrid() : void  //截单个UI
  13.   {
  14.     var bd : BitmapData = getBitmapData( UIComponent( hiddenDg ) );
  15.     targetImage.source = new Bitmap( bd );
  16.   }    private function getBitmapData( target : UIComponent ) : BitmapData  //截图功能函数  {
  17.     var bd : BitmapData = new BitmapData( target.width, target.height );
  18.     var m : Matrix = new Matrix();
  19.     bd.draw( target, m );
  20.     return bd;
  21.   }
  22.   ]]>
  23. </mx:Script>
  24. <mx:Button
  25. id="captureButton"
  26. label="Capture Full Screen"
  27. click="captureFullScreen()" />
  28. <mx:Button
  29. id="captureButton2"
  30. label="Capture Hidden Datagrid"
  31. click="captureHiddenDatagrid()"
  32. x="153"/>
  33. <mx:Image
  34. id="targetImage"
  35. x="10"
  36. y="30"/>
  37. <mx:DataGrid
  38. x="99"
  39. y="64"
  40. id="hiddenDg"
  41. visible="false">
  42. <mx:columns>
  43. <mx:DataGridColumn headerText="Column 1" dataField="col1"/>
  44. <mx:DataGridColumn headerText="Column 2" dataField="col2"/>
  45. <mx:DataGridColumn headerText="Column 3" dataField="col3"/>
  46. </mx:columns>
  47. </mx:DataGrid>
  48. </mx:Application>

  [code]

<utils:JavaScript>
        <![CDATA[
            function takeSnapshot(imgString, width, height) {
                window.open ("data:image/png;base64," + imgString, "", "width=" + width + ",height=" + height + ",resizable=1");
            }
        ]]>
    </utils:JavaScript>
    <mx:Script>
        <![CDATA[
            import com.adobe.images.PNGEncoder;
            import mx.utils.Base64Encoder;
            private function takeSnapshot(comp:DisplayObject):void {
                var bitmapData:BitmapData = new BitmapData(comp.width, comp.height, true, 0xffffff);
                bitmapData.draw(comp);
                var bytes:ByteArray = PNGEncoder.encode(bitmapData);
                var b64encoder:Base64Encoder = new Base64Encoder();
                b64encoder.encodeBytes(bytes);
                ExternalInterface.call("takeSnapshot", b64encoder.flush(), comp.width + 10, comp.height + 10);
            }
        ]]>
    </mx:Script>

[/code]

Air代码

  1. 保存到了D:共享文档(my document)下 
  2. <?xml version="1.0" encoding="utf-8"?> 
  3. <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
  4.     layout="vertical" backgroundGradientAlphas="[1.0, 1.0]"
  5.     backgroundGradientColors="[#FFFFFF, #C0C0C0]"
  6.     width="700" height="650"> 
  7.     <mx:Script> 
  8.         <![CDATA[ 
  9.             import mx.controls.Alert; 
  10.             import mx.core.Application; 
  11.             import flash.display.BitmapData; 
  12.             import mx.graphics.codec.JPEGEncoder; 
  13.             import mx.graphics.codec.PNGEncoder; 
  14.             import mx.collections.ArrayCollection; 
  15.             import mx.core.UIComponent; 
  16.             //Format to save the image 
  17.             public static const FORMAT_JPEG:uint = 0x00; 
  18.             public static const FORMAT_PNG:uint = 0x01; 
  19.             //Extensions for the file 
  20.             private static const EXT_JPEG:String = ".jpg"; 
  21.             private static const EXT_PNG:String = ".png"; 
  22.             //Choose the a directory in user's document folder to store the images 
  23.             private static const IMAGE_FOLDER:String = "SaveChart/images/"; 
  24.             //Sample data to show in line chart 
  25.             [Bindable] 
  26.             public var expenses:ArrayCollection = new ArrayCollection([ 
  27.                 {Month:"Jan", Profit:2000, Expenses:1500, Amount:450}, 
  28.                 {Month:"Feb", Profit:1000, Expenses:200, Amount:600}, 
  29.                 {Month:"Mar", Profit:1500, Expenses:500, Amount:300} 
  30.                 ]); 
  31.             //Saves the image snapshot in specified format 
  32.             private function saveImage(comp:DisplayObject,format:uint):void{ 
  33.                 //Bitmapdata from the component to take snapshot 
  34.                 mx.controls.Alert.show("save"); 
  35.                 var bmpd:BitmapData = new BitmapData(comp.width,comp.height); 
  36.                 bmpd.draw(comp); 
  37.                 //Bytearray of the final image 
  38.                 var imgByteArray:ByteArray; 
  39.                 //extension to save the file 
  40.                 var ext:String; 
  41.                 //verify specified format and use the correct encoder to produce the bytearray 
  42.                 switch(format){ 
  43.                     case FORMAT_JPEG: 
  44.                         ext = EXT_JPEG; 
  45.                         var jpgenc:JPEGEncoder = new JPEGEncoder(80); 
  46.                         imgByteArray = jpgenc.encode(bmpd); 
  47.                         break; 
  48.                     case FORMAT_PNG: 
  49.                         ext = EXT_PNG; 
  50.                         var pngenc:PNGEncoder = new PNGEncoder(); 
  51.                         imgByteArray = pngenc.encode(bmpd); 
  52.                         break; 
  53.                 } 
  54.                 //gets a reference to a new empty image file  
  55.                 var fl:File = getNewImageFile(ext); 
  56.                 //Use a FileStream to save the bytearray as bytes to the new file 
  57.                 var fs:FileStream = new FileStream(); 
  58.                 try{ 
  59.                     //open file in write mode 
  60.                     fs.open(fl,FileMode.WRITE); 
  61.                     //write bytes from the byte array 
  62.                     fs.writeBytes(imgByteArray); 
  63.                     //close the file 
  64.                     fs.close(); 
  65.                     trace("save"); 
  66.                 }catch(e:Error){ 
  67.                     trace(e.message); 
  68.                     mx.controls.Alert.show(e.message); 
  69.                 } 
  70.             } 
  71.             //Returns a unique new image file reference 
  72.             //with specified extension 
  73.             private function getNewImageFile(ext:String):File{ 
  74.                 //Create a new unique filename based on date/time 
  75.                 var fileName:String = "image"+getNowTimestamp()+ext; 
  76.                 //Create a reference to a new file on app folder 
  77.                 //We use resolvepath to get a file object that points to the correct  
  78.                 //image folder - [USER]/[Documents]/[YOUR_APP_NAME]/images/ 
  79.                 //it also creates any directory that does not exists in the path 
  80.                 var fl:File = File.documentsDirectory.resolvePath(IMAGE_FOLDER+fileName); 
  81.                 mx.controls.Alert.show(File.documentsDirectory.name.toString()); 
  82.                 //verify that the file really does not exist 
  83.                 if(fl.exists){ 
  84.                     //if exists , tries to get a new one using recursion 
  85.                     return getNewImageFile(ext); 
  86.                 } 
  87.                 return fl; 
  88.             }    
  89.             //Returns a string in the format 
  90.             //200831415144243
  91.             private function getNowTimestamp():String{ 
  92.                 var d:Date = new Date(); 
  93.                 var tstamp:String = d.getFullYear().toString()+d.getMonth()+d.getDate()+d.getHours()+d.getMinutes()+d.getSeconds()+d.getMilliseconds(); 
  94.                 return  tstamp;          
  95.             } 
  96.             //Handler for the save button click 
  97.             private function onClickSave(e:MouseEvent):void{ 
  98.                 saveImage(myChart,FORMAT_JPEG); 
  99.             } 
  100.         ]]> 
  101.     </mx:Script> 
  102.     <mx:Label text="Save a chart snapshot" color="#171717" fontSize="20" fontWeight="bold"/> 
  103.     <mx:Panel title="Line Chart"> 
  104.         <mx:LineChart id="myChart"
  105.             dataProvider="{expenses}"
  106.             showDataTips="true"> 
  107.             <mx:horizontalAxis> 
  108.                <mx:CategoryAxis  
  109.                     dataProvider="{expenses}"
  110.                     categoryField="Month" /> 
  111.             </mx:horizontalAxis> 
  112.             <mx:series> 
  113.                <mx:LineSeries  
  114.                     yField="Profit"
  115.                     displayName="Profit" /> 
  116.                <mx:LineSeries  
  117.                     yField="Expenses"
  118.                     displayName="Expenses" /> 
  119.             </mx:series> 
  120.         </mx:LineChart> 
  121.         <mx:Legend dataProvider="{myChart}"/> 
  122.     </mx:Panel> 
  123.     <mx:Button id="save_btn" label="Click to save a snapshot" width="181" click="onClickSave(event)"/> 
  124. <?xml version="1.0" encoding="utf-8"?> 
  125. <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
  126.     layout="vertical" backgroundGradientAlphas="[1.0, 1.0]"
  127.     backgroundGradientColors="[#FFFFFF, #C0C0C0]"
  128.     width="400" height="350"
  129.     creationComplete="init()"> 
  130.     <mx:Script> 
  131.         <![CDATA[ 
  132.             import mx.core.Application; 
  133.             import flash.display.BitmapData; 
  134.             import mx.graphics.codec.JPEGEncoder; 
  135.             import mx.graphics.codec.PNGEncoder; 
  136.             import mx.collections.ArrayCollection; 
  137.             import mx.core.UIComponent; 
  138.             //Format to save the image 
  139.             public static const FORMAT_JPEG:uint = 0x00; 
  140.             public static const FORMAT_PNG:uint = 0x01; 
  141.             //Extensions for the file 
  142.             private static const EXT_JPEG:String = ".jpg"; 
  143.             private static const EXT_PNG:String = ".png"; 
  144.             //Choose the a directory in user's document folder to store the images 
  145.             private static const IMAGE_FOLDER:String = "SaveWebcam/images/"; 
  146.             //Sample data to show in line chart 
  147.             [Bindable] 
  148.             public var expenses:ArrayCollection = new ArrayCollection([ 
  149.                 {Month:"Jan", Profit:2000, Expenses:1500, Amount:450}, 
  150.                 {Month:"Feb", Profit:1000, Expenses:200, Amount:600}, 
  151.                 {Month:"Mar", Profit:1500, Expenses:500, Amount:300} 
  152.                 ]); 
  153.             private var cam:Camera; 
  154.             //Inits the webcam 
  155.             private function init():void{ 
  156.                 //gets the webcam reference and show in the videodisplay comp 
  157.                 cam = Camera.getCamera(); 
  158.                 camVideo.attachCamera(cam); 
  159.             } 
  160.             //Saves the image snapshot in specified format 
  161.             private function saveImage(comp:DisplayObject,format:uint):void{ 
  162.                 //Bitmapdata from the component to take snapshot 
  163.                 var bmpd:BitmapData = new BitmapData(comp.width,comp.height); 
  164.                 bmpd.draw(comp); 
  165.                 //Bytearray of the final image 
  166.                 var imgByteArray:ByteArray; 
  167.                 //extension to save the file 
  168.                 var ext:String; 
  169.                 //verify specified format and use the correct encoder to produce the bytearray 
  170.                 switch(format){ 
  171.                     case FORMAT_JPEG: 
  172.                         ext = EXT_JPEG; 
  173.                         var jpgenc:JPEGEncoder = new JPEGEncoder(80); 
  174.                         imgByteArray = jpgenc.encode(bmpd); 
  175.                         break; 
  176.                     case FORMAT_PNG: 
  177.                         ext = EXT_PNG; 
  178.                         var pngenc:PNGEncoder = new PNGEncoder(); 
  179.                         imgByteArray = pngenc.encode(bmpd); 
  180.                         break; 
  181.                 } 
  182.                 //gets a reference to a new empty image file  
  183.                 var fl:File = getNewImageFile(ext); 
  184.                 //Use a FileStream to save the bytearray as bytes to the new file 
  185.                 var fs:FileStream = new FileStream(); 
  186.                 try{ 
  187.                     //open file in write mode 
  188.                     fs.open(fl,FileMode.WRITE); 
  189.                     //write bytes from the byte array 
  190.                     fs.writeBytes(imgByteArray); 
  191.                     //close the file 
  192.                     fs.close(); 
  193.                 }catch(e:Error){ 
  194.                     trace(e.message); 
  195.                 } 
  196.             } 
  197.             //Returns a unique new image file reference 
  198.             //with specified extension 
  199.             private function getNewImageFile(ext:String):File{ 
  200.                 //Create a new unique filename based on date/time 
  201.                 var fileName:String = "image"+getNowTimestamp()+ext; 
  202.                 //Create a reference to a new file on app folder 
  203.                 //We use resolvepath to get a file object that points to the correct  
  204.                 //image folder - [USER]/[Documents]/[YOUR_APP_NAME]/images/ 
  205.                 //it also creates any directory that does not exists in the path 
  206.                 var fl:File = File.documentsDirectory.resolvePath(IMAGE_FOLDER+fileName); 
  207.                 //verify that the file really does not exist 
  208.                 if(fl.exists){ 
  209.                     //if exists , tries to get a new one using recursion 
  210.                     return getNewImageFile(ext); 
  211.                 } 
  212.                 return fl; 
  213.             }    
  214.             //Returns a string in the format 
  215.             //200831415144243
  216.             private function getNowTimestamp():String{ 
  217.                 var d:Date = new Date(); 
  218.                 var tstamp:String = d.getFullYear().toString()+d.getMonth()+d.getDate()+d.getHours()+d.getMinutes()+d.getSeconds()+d.getMilliseconds(); 
  219.                 return  tstamp;          
  220.             } 
  221.             //Handler for the save button click 
  222.             private function onClickSave():void{ 
  223.                 saveImage(camVideo,FORMAT_PNG); 
  224.             } 
  225.         ]]> 
  226.     </mx:Script> 
  227.     <mx:Label text="Save your picture" fontSize="14" color="#000000" fontWeight="bold"/>   
  228.     <mx:VideoDisplay id="camVideo" width="320" height="240" /> 
  229.     <mx:Button id="save_btn" label="Click to save a snapshot" width="181" click="onClickSave()"/> 
  230. </mx:WindowedApplication> 
  231. </mx:WindowedApplication> 
posted @ 2010-01-05 17:45  真功夫  阅读(827)  评论(0编辑  收藏  举报