as3 抠图

 
  1.  import flash.display.*;      
  2.  import flash.events.*;       
  3.  import flash.filters.*;      
  4.  import flash.geom.*;          
  5.   
  6.  import mx.controls.Image;     
  7.   
  8.   import mx.core.UIComponent;      
  9.   
  10.  /**      * @author CYPL      * 设置图片元件实例名为Image      */       
  11.   
  12. public class CutImageTest extends UIComponent {           
  13.   
  14. private var _imageBitmapData : BitmapData;           
  15.   
  16. private var _imageHotAreaData:BitmapData;           
  17.   
  18. private var _imageBitmap : Bitmap;           
  19.   
  20. private var _mouseRectContainer:Sprite;           
  21.   
  22. private var _mouseRectStartX:Number;          
  23.   
  24. private var _mouseRectStartY:Number;           
  25.   
  26. private var _imageClipDraging:Boolean;           
  27.   
  28. private var _currentDragClip:Sprite;                  
  29.   
  30.  [Bindable]           
  31.   
  32. public var clibImg:Bitmap;// 剪切图片                   
  33.   
  34. public function CutImageTest( image:Image ) {                          
  35.   
  36.  _mouseRectContainer=new Sprite;               
  37.   
  38. image.visible=false;              
  39.   
  40.  _imageBitmapData=new BitmapData(image.width,image.height,true,0),_imageBitmapData.draw(image);             _imageBitmap=Bitmap(addChild(new Bitmap(_imageBitmapData)))              
  41.   
  42.  _imageBitmap.x=30              
  43.   
  44.  _imageBitmap.y=30               
  45.   
  46. configMouseEvent();               
  47.   
  48. //----------hitTestArea------------------------               
  49.   
  50. var c:ColorTransform=new ColorTransform;               
  51.   
  52. c.color=0xff0000;               
  53.   
  54. _imageHotAreaData=_imageBitmapData.clone();               
  55.   
  56. _imageHotAreaData.draw(_imageHotAreaData,null,c);         }           
  57.   
  58. private function configMouseEvent():void {              
  59.  this.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler,false,0,true);               
  60. this.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler,false,0,true);         }           
  61.   
  62. /**************************drawRect handler*******************************/           
  63.   
  64. private function mouseDownHandler(evt:MouseEvent):void {//mouse_down              
  65.   
  66.  if (_imageClipDraging) {                 return;             }               
  67.   
  68. addChild(_mouseRectContainer);               
  69.   
  70. _mouseRectStartX=evt.stageX;               
  71.   
  72. _mouseRectStartY=evt.stageY;               
  73.   
  74. this.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);         }           
  75.   
  76. private function mouseUpHandler(evt:MouseEvent):void {//mouse_up                           
  77.   
  78. //_currentDragClip&&();              
  79.  _imageClipDraging&&(_currentDragClip.stopDrag(),_imageClipDraging=false,_currentDragClip.alpha=1)  
  80. ||(cutImage(checkIntersection()),_mouseRectContainer.graphics.clear(),this.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler))                     }           
  81.   
  82. private function mouseMoveHandler(evt:MouseEvent):void {//mouse_move               
  83.   
  84. evt.updateAfterEvent();               
  85.   
  86. var minX:Number=Math.min(evt.stageX,_mouseRectStartX)               
  87.   
  88. var minY:Number=Math.min(evt.stageY,_mouseRectStartY)               
  89.   
  90. var maxX:Number=Math.max(evt.stageX,_mouseRectStartX)               
  91.   
  92. var maxY:Number=Math.max(evt.stageY,_mouseRectStartY)   
  93.   
  94. with(_mouseRectContainer.graphics){               
  95.   
  96. clear();               
  97.   
  98. lineStyle(0);               
  99.   
  100. beginFill(0xffff00,.5);               
  101.   
  102. drawCircle( (maxX-minX)/2, (maxY-minY)/2, (maxX-minX)/2 );              
  103.  //drawRect(0,0,maxX-minX,maxY-minY);    
  104.   
  105.            }              
  106.   
  107.  _mouseRectContainer.x=minX;              
  108.   
  109.  _mouseRectContainer.y=minY;           
  110.   
  111. }           
  112.   
  113. /************************************************************************/          
  114.  /**************************drag  handler*******************************/           
  115. private function clipMouseDownHandler(evt:MouseEvent):void {//mouse_down               
  116.   
  117. var target:Sprite=evt.target as Sprite;               
  118.   
  119. _currentDragClip=target;              
  120.   
  121.  _currentDragClip.alpha=.5;               
  122.   
  123. _imageClipDraging=true;               
  124.   
  125. addChild(target);               
  126.   
  127. _currentDragClip.startDrag(false);          
  128.   
  129.  }           
  130.   
  131. /************************************************************************/           
  132.   
  133. private function checkIntersection():Rectangle {               
  134.   
  135. var intersectRect:Rectangle=_imageBitmapData.rect.intersection(new Rectangle(_mouseRectContainer.x-_imageBitmap.x,_mouseRectContainer.y-_imageBitmap.y,_mouseRectContainer.width,_mouseRectContainer.height));             //trace("与源图BitmapData相交范围:"+intersectRect);               
  136.   
  137. if (intersectRect.width==0 || intersectRect.height==0) {                 return null;             }              
  138.   
  139.  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));               
  140.   
  141. var intersectHotAreaRect:Rectangle=bitmapData.getColorBoundsRect(0xFFFF0000, 0xFFFF0000,true);              
  142.   
  143.  trace("最终切图块的范围:"+intersectHotAreaRect);              
  144.   
  145.  if (intersectHotAreaRect.width==0 || intersectHotAreaRect.height==0) {                                                 return null;             }   
  146.   
  147.  //扩展范围避免误差               
  148.   
  149. intersectHotAreaRect.x-=1               
  150.   
  151. intersectHotAreaRect.y-=1              
  152.   
  153.  intersectHotAreaRect.width+=2               
  154.   
  155. intersectHotAreaRect.height+=2               
  156.   
  157. return intersectHotAreaRect;         }           
  158.   
  159. private function cutImage(rect:Rectangle):void {//关键的切图部分               
  160.   
  161. if (!rect) {                 return;             }               
  162.   
  163. var clipBitmapData:BitmapData=new BitmapData(rect.width,rect.height,true,0);               
  164.   
  165. varcliptX:Number=(_mouseRectContainer.x=_mouseRectContainer.x<_imageBitmap.x?0:_mouseRectContainer.x-_imageBitmap.x)+rect.x;             varcliptY: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)               
  166.   
  167. var clipBitmap:Bitmap=new Bitmap(clipBitmapData);               
  168.   
  169. clibImg = clipBitmap;// 剪切图片           
  170.   }      
  171.  }   
  172. }   
  173. /*****************************************************MXML**********************************************/           
  174.   
  175. <?xml version="1.0" encoding="utf-8"?>  
  176.   
  177.  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">     <mx:Script>         <![CDATA[               
  178.   
  179. [Bindable]               
  180.   
  181. private var cutImg:CutImageTest;               
  182.   
  183. private function init():void {                   
  184.   
  185. cutImg = new CutImageTest( image );                   
  186.   
  187. this.addChild( cutImg );               
  188.   
  189. }           
  190.   
  191. ]]>       
  192.   
  193. </mx:Script>       
  194.   
  195. <mx:Image left="60" top="50" right="520" bottom="220" id="image" source="../img/11.jpg"/>       
  196.   
  197. <mx:Image  id="clibImg" source="{cutImg.clibImg}" width="200" height="200" right="50" bottom="100"/>   
  198.   
  199. </mx:Application>   
  200.   
  201.   
  202.   
  203. /**************************************************************************************************************/       
  204.   
  205. 封装类 CutImageTest   (抠图功能实现)       
  206.   
  207. package {       
  208.   
  209. import flash.display.*;       
  210.   
  211. import flash.events.*;       
  212.   
  213. import flash.filters.*;       
  214.   
  215. import flash.geom.*;           
  216.   
  217. import mx.controls.Image;       
  218.   
  219. import mx.core.UIComponent;       
  220.   
  221. /**      * @author CYPL      * 设置图片元件实例名为Image      */       
  222.   
  223. public class CutImageTest extends UIComponent {           
  224.   
  225. private var _imageBitmapData : BitmapData;           
  226.   
  227. private var _imageHotAreaData:BitmapData;           
  228.   
  229. private var _imageBitmap : Bitmap;           
  230.   
  231. private var _mouseRectContainer:Sprite;           
  232.   
  233. private var _mouseRectStartX:Number;           
  234.   
  235. private var _mouseRectStartY:Number;           
  236.   
  237. private var _imageClipDraging:Boolean;           
  238.   
  239. private var _currentDragClip:Sprite;                   
  240.   
  241. [Bindable]           
  242.   
  243. public var clibImg:Bitmap;// 剪切图片                   
  244.   
  245. public function CutImageTest( image:Image ) {                          
  246.   
  247.  _mouseRectContainer=new Sprite;               
  248.   
  249. image.visible=false;               
  250.   
  251. _imageBitmapData=new BitmapData(image.width,image.height,true,0),_imageBitmapData.draw(image);             _imageBitmap=Bitmap(addChild(new Bitmap(_imageBitmapData)))               
  252.   
  253. _imageBitmap.x=30 ;              
  254.   
  255. _imageBitmap.y=30 ;             
  256.   
  257. configMouseEvent();               
  258.   
  259. //----------hitTestArea------------------------               
  260.   
  261. var c:ColorTransform=new ColorTransform;               
  262.   
  263. c.color=0xff0000;               
  264.   
  265. _imageHotAreaData=_imageBitmapData.clone();               
  266.   
  267. _imageHotAreaData.draw(_imageHotAreaData,null,c);           
  268.   
  269. }           
  270.   
  271. private function configMouseEvent():void {               
  272. this.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler,false,0,true);               
  273. this.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler,false,0,true);           
  274.   
  275. }           
  276.   
  277. /**************************drawRect handler*******************************/           
  278.   
  279. private function mouseDownHandler(evt:MouseEvent):void {//mouse_down               
  280.   
  281. if (_imageClipDraging) {                 return;             }              
  282.   
  283.  addChild(_mouseRectContainer);              
  284.   
  285.  _mouseRectStartX=evt.stageX;               
  286.   
  287. _mouseRectStartY=evt.stageY;               
  288.   
  289. this.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);           
  290.   
  291. }         private function mouseUpHandler(evt:MouseEvent):void {//mouse_up                           
  292.   
  293. //_currentDragClip&&();               
  294. _imageClipDraging&&(_currentDragClip.stopDrag(),_imageClipDraging=false,_currentDragClip.alpha=1)  
  295. ||(cutImage(checkIntersection()),_mouseRectContainer.graphics.clear(),this.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler))                     }          
  296.   
  297.  private function mouseMoveHandler(evt:MouseEvent):void {//mouse_move               
  298.   
  299. evt.updateAfterEvent();               
  300.   
  301. var minX:Number=Math.min(evt.stageX,_mouseRectStartX)               
  302.   
  303. var minY:Number=Math.min(evt.stageY,_mouseRectStartY)               
  304.   
  305. var maxX:Number=Math.max(evt.stageX,_mouseRectStartX)               
  306.   
  307. var maxY:Number=Math.max(evt.stageY,_mouseRectStartY)   
  308.   
  309. with(_mouseRectContainer.graphics){    
  310.   
  311.            clear();              
  312.   
  313.  lineStyle(0);               
  314.   
  315. beginFill(0xffff00,.5);               
  316.   
  317. drawRect(0,0,maxX-minX,maxY-minY);}              
  318.   
  319.  _mouseRectContainer.x=minX;               
  320.   
  321. _mouseRectContainer.y=minY;           
  322.   
  323. }           
  324.   
  325. /************************************************************************/           
  326.   
  327. /**************************drag  handler*******************************/           
  328.   
  329. private function clipMouseDownHandler(evt:MouseEvent):void {//mouse_down               
  330.   
  331. var target:Sprite=evt.target as Sprite;               
  332.   
  333. _currentDragClip=target;               
  334.   
  335. _currentDragClip.alpha=.5;               
  336.   
  337. _imageClipDraging=true;               
  338.   
  339. addChild(target);               
  340.   
  341. _currentDragClip.startDrag(false);           
  342.   
  343. }           
  344.   
  345. /************************************************************************/           
  346.   
  347. private function checkIntersection():Rectangle {               
  348.   
  349. var intersectRect:Rectangle=_imageBitmapData.rect.intersection(new Rectangle(_mouseRectContainer.x-_imageBitmap.x,_mouseRectContainer.y-_imageBitmap.y,_mouseRectContainer.width,_mouseRectContainer.height));             trace("与源图BitmapData相交范围:"+intersectRect);               
  350.   
  351. if (intersectRect.width==0 || intersectRect.height==0) {                 return null;             }   
  352.   
  353.  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));               
  354.   
  355. var intersectHotAreaRect:Rectangle=bitmapData.getColorBoundsRect(0xFFFF0000, 0xFFFF0000,true);               
  356.   
  357. trace("最终切图块的范围:"+intersectHotAreaRect);               
  358.   
  359. if (intersectHotAreaRect.width==0 || intersectHotAreaRect.height==0) {                                                 return null;             }    
  360.   
  361.            //扩展范围避免误差               
  362.   
  363. intersectHotAreaRect.x-=1              
  364.   
  365.  intersectHotAreaRect.y-=1              
  366.   
  367.  intersectHotAreaRect.width+=2               
  368.   
  369. intersectHotAreaRect.height+=2               
  370.   
  371. return intersectHotAreaRect;          
  372.   
  373.  }           
  374.   
  375. private function cutImage(rect:Rectangle):void {//关键的切图部分              
  376.   
  377.  if (!rect) {                 return;             }               
  378.   
  379. var clipBitmapData:BitmapData=new BitmapData(rect.width,rect.height,true,0);               
  380.   
  381. varcliptX:Number=(_mouseRectContainer.x=_mouseRectContainer.x<_imageBitmap.x?0:_mouseRectContainer.x-_imageBitmap.x)+rect.x;             varcliptY: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)     
  382.   
  383.           var clipBitmap:Bitmap=new Bitmap(clipBitmapData);              
  384.   
  385.  clipBitmap.filters=[new GlowFilter(0,1,2,2,10,1)];               
  386.   
  387. var sprite:Sprite=new Sprite              
  388.   
  389.  with(sprite.graphics){              
  390.   
  391.  lineStyle(0);               
  392.   
  393. lineTo(rect.width,0);              
  394.   
  395.  lineTo(rect.width,rect.height);               
  396.   
  397. lineTo(0,rect.height);               
  398.   
  399. lineTo(0,0);}              
  400.   
  401.  sprite.x=_mouseRectContainer.x+rect.x+_imageBitmap.x              
  402.   
  403.  sprite.y=_mouseRectContainer.y+rect.y+_imageBitmap.y              
  404.  sprite.addEventListener(MouseEvent.MOUSE_DOWN,clipMouseDownHandler);               
  405.   
  406. Sprite(addChild(sprite)).addChild(clipBitmap)              
  407.   
  408.  var fillRect:Rectangle=new Rectangle(sprite.x-_imageBitmap.x,sprite.y-_imageBitmap.y,rect.width,rect.height);             _imageBitmapData.fillRect(fillRect,0);               
  409.   
  410. _imageHotAreaData.fillRect(fillRect,0);               
  411.   
  412.   clibImg = clipBitmap;// 剪切图片        
  413.   
  414.    }   
  415.   
  416.     }   
  417.   
  418. }   
  419.   
  420. /--------------------------------------------------------------结束------- ---------------------------------------------------------------------/  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帆软爱好者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值