2008/04/21 | AS3___一个抠图小程序实验
类别(flash学习) | 评论(0) | 阅读(437) | 发表于 03:48

package {
 import flash.display.*;
 import flash.events.*;
 import flash.net.URLRequest;
 import flash.geom.*;
 import flash.filters.*;

 /**
  * @author CYPL
  * 设置图片元件实例名为Image
  */
 public class CutImageTest extends Sprite {
  private var _imageBitmapData : BitmapData;
  private var _imageHotAreaData:BitmapData;
  private var _imageBitmap : Bitmap;
  private var _mouseRectContainer:Sprite;
  private var _mouseRectStartX:Number;
  private var _mouseRectStartY:Number;
  private var _imageClipDraging:Boolean;
  private var _currentDragClip:Sprite;
  public function CutImageTest() {
   
   _mouseRectContainer=new Sprite;
   Image.visible=false;
   _imageBitmapData=new BitmapData(Image.width,Image.height,true,0),_imageBitmapData.draw(Image);
   _imageBitmap=Bitmap(addChild(new Bitmap(_imageBitmapData)))
   configMouseEvent();
   //----------hitTestArea------------------------
   var c:ColorTransform=new ColorTransform;
   c.color=0xff0000;
   _imageHotAreaData=_imageBitmapData.clone();
   _imageHotAreaData.draw(_imageHotAreaData,null,c);
  }

  private function configMouseEvent():void {
   stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler,false,0,true);
   stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler,false,0,true);
  }
  /**************************drawRect handler*******************************/
  private function mouseDownHandler(evt:MouseEvent):void {//mouse_down

   if (_imageClipDraging) {
    return;
   }
   addChild(_mouseRectContainer);
   _mouseRectStartX=evt.stageX;
   _mouseRectStartY=evt.stageY;
   stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
  }
  private function mouseUpHandler(evt:MouseEvent):void {//mouse_up
   
   //_currentDragClip&&();
   _imageClipDraging&&(_currentDragClip.stopDrag(),_imageClipDraging=false,_currentDragClip.alpha=1)||(cutImage(checkIntersection()),_mouseRectContainer.graphics.clear(),stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler))
   
  }
  private function mouseMoveHandler(evt:MouseEvent):void {//mouse_move
   evt.updateAfterEvent();
   var minX:Number=Math.min(evt.stageX,_mouseRectStartX)
   var minY:Number=Math.min(evt.stageY,_mouseRectStartY)
   var maxX:Number=Math.max(evt.stageX,_mouseRectStartX)
   var maxY:Number=Math.max(evt.stageY,_mouseRectStartY)

with(_mouseRectContainer.graphics){
   clear();
   lineStyle(0);
   beginFill(0xffff00,.5);
   drawRect(0,0,maxX-minX,maxY-minY);}
   _mouseRectContainer.x=minX;
   _mouseRectContainer.y=minY;
  }
  /************************************************************************/
  /**************************drag  handler*******************************/
  private function clipMouseDownHandler(evt:MouseEvent):void {//mouse_down
   var target:Sprite=evt.target as Sprite;
   _currentDragClip=target;
   _currentDragClip.alpha=.5;
   _imageClipDraging=true;
   addChild(target);
   _currentDragClip.startDrag(false);

  }
  /************************************************************************/

  private function checkIntersection():Rectangle {
   var intersectRect:Rectangle=_imageBitmapData.rect.intersection(new Rectangle(_mouseRectContainer.x,_mouseRectContainer.y,_mouseRectContainer.width,_mouseRectContainer.height));
   trace("与源图BitmapData相交范围:"+intersectRect);
   if (intersectRect.width==0 || intersectRect.height==0) {
    return null;
   }
   var bitmapData:BitmapData=new BitmapData(intersectRect.width,intersectRect.height,true,0);
   bitmapData.draw(_imageHotAreaData,new Matrix(1,0,0,1,-intersectRect.x,-intersectRect.y),null,null,new Rectangle(0,0,intersectRect.width,intersectRect.height));
   var intersectHotAreaRect:Rectangle=bitmapData.getColorBoundsRect(0xFFFF0000, 0xFFFF0000,true);
   trace("最终切图块的范围:"+intersectHotAreaRect);
   if (intersectHotAreaRect.width==0 || intersectHotAreaRect.height==0) {
    //这里两句是进行纠正错处理,切很细的话,会出现处理上的偏差
    _imageBitmapData.fillRect(intersectRect,0);
   _imageHotAreaData.fillRect(intersectRect,0);
    return null;
   }
   intersectHotAreaRect.width+=1
   intersectHotAreaRect.height+=1
   return intersectHotAreaRect;
  }
  private function cutImage(rect:Rectangle):void {//关键的切图部分
   if (!rect) {
    return;
   }
   var clipBitmapData:BitmapData=new BitmapData(rect.width,rect.height,true,0);
   var cliptX:Number=(_mouseRectContainer.x=_mouseRectContainer.x<_imageBitmap.x?0:_mouseRectContainer.x-_imageBitmap.x)+rect.x;
   var cliptY:Number=(_mouseRectContainer.y=_mouseRectContainer.y<_imageBitmap.y?0:_mouseRectContainer.y-_imageBitmap.y)+rect.y;
   clipBitmapData.draw(_imageBitmapData,new Matrix(1,0,0,1,-cliptX,-cliptY),null,null,new Rectangle(0,0,rect.width,rect.height));//intersectHotAreaRect)
   var clipBitmap:Bitmap=new Bitmap(clipBitmapData);
   clipBitmap.filters=[new GlowFilter(0,1,2,2,10,1)];
   var sprite:Sprite=new Sprite
   with(sprite.graphics){
   lineStyle(0);
   lineTo(rect.width,0);
   lineTo(rect.width,rect.height);
   lineTo(0,rect.height);
   lineTo(0,0);}
   sprite.x=_mouseRectContainer.x+rect.x;
   sprite.y=_mouseRectContainer.y+rect.y;
   sprite.addEventListener(MouseEvent.MOUSE_DOWN,clipMouseDownHandler);
   Sprite(addChild(sprite)).addChild(clipBitmap)
   var fillRect:Rectangle=new Rectangle(sprite.x,sprite.y,rect.width,rect.height);
   _imageBitmapData.fillRect(fillRect,0);
   _imageHotAreaData.fillRect(fillRect,0);
   
  }
 }
}

0

评论Comments

日志分类
首页[38]
flash学习[35]
图片收藏[1]
Apollo_Flex[2]