AS截屏的类的使用方法+示例

来源:互联网 发布:js 给日期字段赋值 编辑:程序博客网 时间:2024/05/05 03:41
 1.首先先引入两个swc(aether,as3corelib)
http://code.google.com/p/aether/
http://code.google.com/p/as3corelib/
2.代码部分:
package {
import aether.utils.ScreenCapture;

import com.adobe.images.JPGEncoder;

import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.net.FileReference;
import flash.utils.ByteArray;
    [SWF(width=800,height=600,backgroundColor=0xffffff)]
public class PrintScreen extends Sprite
{
  private var bitmapData:BitmapData;
  
  public function PrintScreen()
  {
   //画了一些随机的矢量图
   
   graphics.lineStyle(1);
   graphics.drawRect(0,0,100,100);
   for(var i:int=0;i<30;i++){
    var tempx:Number=Math.random()*400;
    var tempy:Number=Math.random()*300;
    var color:Number=0xff0000*Math.random();
   
    graphics.lineStyle(1,color);
    graphics.lineTo(tempx,tempy);
    graphics.endFill();
   }
   //定义截屏的区域,并保存在bitmapData中
   //false是位图是否有transparent项
   var region:Rectangle= new Rectangle(0, 0, 100, 100);
   bitmapData= ScreenCapture.capture(stage,false, region);
     
     
   // 一个按钮,用于启动截屏
   var saveBtn:MyButton=new MyButton("save",50,30);
   saveBtn.x=700;
   saveBtn.y=150;
   addChild(saveBtn);
   saveBtn.addEventListener(MouseEvent.CLICK,save);
  }
   
  private function save(e:MouseEvent):void{
   var encoder:JPGEncoder = new JPGEncoder(80); //80是quality
   var bytes:ByteArray= encoder.encode(bitmapData);
   var fr:FileReference = new FileReference();
   fr.save(bytes, "screen.jpg");
  }
}
}
MyButton类
  package {
  import flash.display.*
  import flash.text.*;
  import flash.filters.DropShadowFilter;
  public class MyButton extends SimpleButton {
    // The text to appear on the button
    private var _text:String;
    // Save the width and height of the rectangle
    private var _width:Number;
    private var _height:Number;
   
    public function MyButton( text:String, width:Number, height:Number ) {
      // Save the values to use them to create the button states
      _text = text;
      _width = width;
      _height = height;
     
      // Create the button states based on width, height, and text value
      upState = createUpState();
      overState = createOverState();
      downState = createDownState();
      hitTestState = upState;
    }

    // Create the display object for the button's up state
    private function createUpState():Sprite {
      var sprite:Sprite = new Sprite();
     
      var background:Shape = createdColoredRectangle( 0xcccccc );
      var textField:TextField = createTextField( false );
         
      sprite.addChild( background );
      sprite.addChild( textField );
      return sprite;
    }
   
    // Create the display object for the button's up state
    private function createOverState():Sprite {
      var sprite:Sprite = new Sprite();
     
      var background:Shape = createdColoredRectangle( 0xff0000 );
      var textField:TextField = createTextField( false );
           
      sprite.addChild( background );
      sprite.addChild( textField );
      return sprite;
    }
   
    // Create the display object for the button's down state
    private function createDownState():Sprite {
      var sprite:Sprite = new Sprite();
     
      var background:Shape = createdColoredRectangle( 0xCCCCCC );
      var textField:TextField = createTextField( true );
     
      sprite.addChild( background );
      sprite.addChild( textField );
     
      return sprite;
    }
   
    // Create a rounded rectangle with a specific fill color
    private function createdColoredRectangle( color:uint ):Shape {
      var rect:Shape = new Shape();
      rect.graphics.lineStyle( 1, 0x000000 );
      rect.graphics.beginFill( color );
      rect.graphics.drawRect( 0, 0, _width, _height);
      rect.graphics.endFill();
      rect.filters = [ new DropShadowFilter( 2 ) ];
      return rect;
    }
   
    // Create the text field to display the text of the button
    private function createTextField( downState:Boolean ):TextField {
      var textField:TextField = new TextField();
      textField.text = _text;
      textField.width = _width;
           
      // Center the text horizontally
      var format:TextFormat = new TextFormat();
      format.align = TextFormatAlign.CENTER;
      textField.setTextFormat( format );
     
      // Center the text vertically
      textField.y = ( _height - textField.textHeight ) / 2;
      textField.y -= 2;  // Subtract 2 pixels to adjust for offset
     
      // The down state places the text down and to the right
      // further than the other states
      if ( downState ) {
        textField.x += 1;
        textField.y += 1;
      }
     
      return textField;
    }
  }
}

原创粉丝点击