发新话题
打印

[分享] BitmapData抠图小实验

本主题由 ycccc8202 于 2008-4-21 21:33 设置高亮

BitmapData抠图小实验

代码如下:
复制内容到剪贴板
代码:
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)))
   _imageBitmap.x=30
   _imageBitmap.y=30
   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-_imageBitmap.x,_mouseRectContainer.y-_imageBitmap.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) {
   
   
    return null;
   }
   //扩展范围避免误差
   intersectHotAreaRect.x-=1
   intersectHotAreaRect.y-=1
   intersectHotAreaRect.width+=2
   intersectHotAreaRect.height+=2
   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+_imageBitmap.x
   sprite.y=_mouseRectContainer.y+rect.y+_imageBitmap.y
   sprite.addEventListener(MouseEvent.MOUSE_DOWN,clipMouseDownHandler);
   Sprite(addChild(sprite)).addChild(clipBitmap)
   var fillRect:Rectangle=new Rectangle(sprite.x-_imageBitmap.x,sprite.y-_imageBitmap.y,rect.width,rect.height);
   _imageBitmapData.fillRect(fillRect,0);
   _imageHotAreaData.fillRect(fillRect,0);
   
  }
}
}

附件

CI.rar (117.98 KB)

2008-4-21 21:30, 下载次数: 303

CutImageTest.swf (56.36 KB)

2008-4-21 21:30, 下载次数: 335

寻觅终生伴侣!

my blog

TOP

真强
水星

TOP

佩服!

TOP

很好

TOP

佩服!

TOP

这个游戏好玩。用边边覆盖图中缺失的部分很难也

TOP

真的很厉害

TOP

不错
54371309[请发送:flash8会员问题]
正在更新中..070815   http://zszen.com

TOP

学习

TOP

发新话题