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保存图片)

 

<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
<mx:Script>
  <![CDATA[
  import mx.core.UIComponent;
  private function captureFullScreen() : void    //截屏
  {
    var bd : BitmapData = getBitmapData( UIComponent( mx.core.Application.application ) );
    targetImage.source = new Bitmap( bd );
  }
  private function captureHiddenDatagrid() : void  //截单个UI
  {
    var bd : BitmapData = getBitmapData( UIComponent( hiddenDg ) );
    targetImage.source = new Bitmap( bd );
  }    private function getBitmapData( target : UIComponent ) : BitmapData  //截图功能函数  {
    var bd : BitmapData = new BitmapData( target.width, target.height );
    var m : Matrix = new Matrix();
    bd.draw( target, m );
    return bd;
  }
  ]]>
</mx:Script>
<mx:Button
  id="captureButton"
  label="Capture Full Screen"
  click="captureFullScreen()" />
<mx:Button
  id="captureButton2"
  label="Capture Hidden Datagrid"
  click="captureHiddenDatagrid()" 
  x="153"/>
<mx:Image
  id="targetImage"
  x="10"
  y="30"/>
<mx:DataGrid
  x="99"
  y="64"
  id="hiddenDg"
  visible="false">
  <mx:columns>
  <mx:DataGridColumn headerText="Column 1" dataField="col1"/>
  <mx:DataGridColumn headerText="Column 2" dataField="col2"/>
  <mx:DataGridColumn headerText="Column 3" dataField="col3"/>
  </mx:columns>
</mx:DataGrid>
</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]

保存到了D:共享文档(my document)下
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" 
	layout="vertical" backgroundGradientAlphas="[1.0, 1.0]" 
	backgroundGradientColors="[#FFFFFF, #C0C0C0]"
	width="700" height="650">
	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.core.Application;
			import flash.display.BitmapData;
			import mx.graphics.codec.JPEGEncoder;
			import mx.graphics.codec.PNGEncoder;
			import mx.collections.ArrayCollection;
			import mx.core.UIComponent;
			
			//Format to save the image
			public static const FORMAT_JPEG:uint = 0x00;
			public static const FORMAT_PNG:uint = 0x01;
			
			//Extensions for the file
			private static const EXT_JPEG:String = ".jpg";
			private static const EXT_PNG:String = ".png";
			
			//Choose the a directory in user's document folder to store the images
			private static const IMAGE_FOLDER:String = "SaveChart/images/";
			
			//Sample data to show in line chart
			[Bindable]
			public var expenses:ArrayCollection = new ArrayCollection([
				{Month:"Jan", Profit:2000, Expenses:1500, Amount:450},
				{Month:"Feb", Profit:1000, Expenses:200, Amount:600},
				{Month:"Mar", Profit:1500, Expenses:500, Amount:300}
				]);

			
			//Saves the image snapshot in specified format
			private function saveImage(comp:DisplayObject,format:uint):void{
				//Bitmapdata from the component to take snapshot
				mx.controls.Alert.show("save");
				var bmpd:BitmapData = new BitmapData(comp.width,comp.height);
				bmpd.draw(comp);
				
				//Bytearray of the final image
				var imgByteArray:ByteArray;
				
				//extension to save the file
				var ext:String;
				
				//verify specified format and use the correct encoder to produce the bytearray
				switch(format){
					case FORMAT_JPEG:
						ext = EXT_JPEG;
						var jpgenc:JPEGEncoder = new JPEGEncoder(80);
						imgByteArray = jpgenc.encode(bmpd);
						break;
					case FORMAT_PNG:
						ext = EXT_PNG;
						var pngenc:PNGEncoder = new PNGEncoder();
						imgByteArray = pngenc.encode(bmpd);
						break;
				}
				
				//gets a reference to a new empty image file 
				var fl:File = getNewImageFile(ext);
				
				//Use a FileStream to save the bytearray as bytes to the new file
				var fs:FileStream = new FileStream();
				try{
					//open file in write mode
					fs.open(fl,FileMode.WRITE);
					//write bytes from the byte array
					fs.writeBytes(imgByteArray);
					
					
					//close the file
					fs.close();
					trace("save");
				}catch(e:Error){
					trace(e.message);
					mx.controls.Alert.show(e.message);
				}
			}
			
			//Returns a unique new image file reference
			//with specified extension
			private function getNewImageFile(ext:String):File{
				//Create a new unique filename based on date/time
				var fileName:String = "image"+getNowTimestamp()+ext;

				//Create a reference to a new file on app folder
				//We use resolvepath to get a file object that points to the correct 
				//image folder - [USER]/[Documents]/[YOUR_APP_NAME]/images/
				//it also creates any directory that does not exists in the path
				
				var fl:File = File.documentsDirectory.resolvePath(IMAGE_FOLDER+fileName);
				mx.controls.Alert.show(File.documentsDirectory.name.toString());
				
				//verify that the file really does not exist
				if(fl.exists){
					//if exists , tries to get a new one using recursion
					return getNewImageFile(ext);
				}
				
				
				return fl;
			}	
			
			//Returns a string in the format
			//200831415144243
			private function getNowTimestamp():String{
				var d:Date = new Date();
				var tstamp:String = d.getFullYear().toString()+d.getMonth()+d.getDate()+d.getHours()+d.getMinutes()+d.getSeconds()+d.getMilliseconds();
				return 	tstamp;			
			}
			
			//Handler for the save button click
			private function onClickSave(e:MouseEvent):void{
				saveImage(myChart,FORMAT_JPEG);
			}
		]]>
	</mx:Script>
	<mx:Label text="Save a chart snapshot" color="#171717" fontSize="20" fontWeight="bold"/>
	<mx:Panel title="Line Chart">
		<mx:LineChart id="myChart" 
		    dataProvider="{expenses}" 
		    showDataTips="true">
		    <mx:horizontalAxis>
		       <mx:CategoryAxis 
		            dataProvider="{expenses}" 
		            categoryField="Month" />
		    </mx:horizontalAxis>
		    <mx:series>
		       <mx:LineSeries 
		            yField="Profit" 
		            displayName="Profit" />
		       <mx:LineSeries 
		            yField="Expenses" 
		            displayName="Expenses" />
		    </mx:series>
		</mx:LineChart>
		<mx:Legend dataProvider="{myChart}"/>
	</mx:Panel>
	<mx:Button id="save_btn" label="Click to save a snapshot" width="181" click="onClickSave(event)"/>
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" 
	layout="vertical" backgroundGradientAlphas="[1.0, 1.0]" 
	backgroundGradientColors="[#FFFFFF, #C0C0C0]"
	width="400" height="350"
	creationComplete="init()">
	
	<mx:Script>
		<![CDATA[
			import mx.core.Application;
			import flash.display.BitmapData;
			import mx.graphics.codec.JPEGEncoder;
			import mx.graphics.codec.PNGEncoder;
			import mx.collections.ArrayCollection;
			import mx.core.UIComponent;
			
			//Format to save the image
			public static const FORMAT_JPEG:uint = 0x00;
			public static const FORMAT_PNG:uint = 0x01;
			
			//Extensions for the file
			private static const EXT_JPEG:String = ".jpg";
			private static const EXT_PNG:String = ".png";
			
			//Choose the a directory in user's document folder to store the images
			private static const IMAGE_FOLDER:String = "SaveWebcam/images/";
			
			//Sample data to show in line chart
			[Bindable]
			public var expenses:ArrayCollection = new ArrayCollection([
				{Month:"Jan", Profit:2000, Expenses:1500, Amount:450},
				{Month:"Feb", Profit:1000, Expenses:200, Amount:600},
				{Month:"Mar", Profit:1500, Expenses:500, Amount:300}
				]);
			
			private var cam:Camera;
			
			
			//Inits the webcam
			private function init():void{
				//gets the webcam reference and show in the videodisplay comp
				cam = Camera.getCamera();
				camVideo.attachCamera(cam);
			}
			
			//Saves the image snapshot in specified format
			private function saveImage(comp:DisplayObject,format:uint):void{
				//Bitmapdata from the component to take snapshot
				var bmpd:BitmapData = new BitmapData(comp.width,comp.height);
				bmpd.draw(comp);
				
				//Bytearray of the final image
				var imgByteArray:ByteArray;
				
				//extension to save the file
				var ext:String;
				
				//verify specified format and use the correct encoder to produce the bytearray
				switch(format){
					case FORMAT_JPEG:
						ext = EXT_JPEG;
						var jpgenc:JPEGEncoder = new JPEGEncoder(80);
						imgByteArray = jpgenc.encode(bmpd);
						break;
					case FORMAT_PNG:
						ext = EXT_PNG;
						var pngenc:PNGEncoder = new PNGEncoder();
						imgByteArray = pngenc.encode(bmpd);
						break;
				}
				
				//gets a reference to a new empty image file 
				var fl:File = getNewImageFile(ext);
				
				//Use a FileStream to save the bytearray as bytes to the new file
				var fs:FileStream = new FileStream();
				try{
					//open file in write mode
					fs.open(fl,FileMode.WRITE);
					//write bytes from the byte array
					fs.writeBytes(imgByteArray);
					//close the file
					fs.close();
				}catch(e:Error){
					trace(e.message);
				}
			}
			
			//Returns a unique new image file reference
			//with specified extension
			private function getNewImageFile(ext:String):File{
				//Create a new unique filename based on date/time
				var fileName:String = "image"+getNowTimestamp()+ext;

				//Create a reference to a new file on app folder
				//We use resolvepath to get a file object that points to the correct 
				//image folder - [USER]/[Documents]/[YOUR_APP_NAME]/images/
				//it also creates any directory that does not exists in the path
				
				var fl:File = File.documentsDirectory.resolvePath(IMAGE_FOLDER+fileName);
				
				//verify that the file really does not exist
				if(fl.exists){
					//if exists , tries to get a new one using recursion
					return getNewImageFile(ext);
				}
				
				return fl;
			}	
			
			//Returns a string in the format
			//200831415144243
			private function getNowTimestamp():String{
				var d:Date = new Date();
				var tstamp:String = d.getFullYear().toString()+d.getMonth()+d.getDate()+d.getHours()+d.getMinutes()+d.getSeconds()+d.getMilliseconds();
				return 	tstamp;			
			}
			
			//Handler for the save button click
			private function onClickSave():void{
				saveImage(camVideo,FORMAT_PNG);
			}
		]]>
	</mx:Script>
	
	<mx:Label text="Save your picture" fontSize="14" color="#000000" fontWeight="bold"/>	
	<mx:VideoDisplay id="camVideo" width="320" height="240" />
	<mx:Button id="save_btn" label="Click to save a snapshot" width="181" click="onClickSave()"/>

</mx:WindowedApplication>
























 </mx:WindowedApplication>
 

你可能感兴趣的:(JavaScript,ext,Flex,Adobe,AIR)