[ActionScript 3.0] 框选裁剪

[ActionScript 3.0] 框选裁剪_第1张图片

  1 package 
  2 {
  3     import flash.display.Bitmap;
  4     import flash.display.BitmapData;
  5     import flash.display.Loader;
  6     import flash.display.Shape;
  7     import flash.display.Sprite;
  8     import flash.events.Event;
  9     import flash.events.MouseEvent;
 10     import flash.geom.Point;
 11     import flash.geom.Rectangle;
 12     import flash.net.URLRequest;
 13     import flash.utils.ByteArray;
 14 
 15     /**
 16       * ...
 17       * @author Frost.Yen
 18       */
 19     public class CutOut extends Sprite
 20     {
 21         private var ldr:Loader;
 22         private var cutContainer:Sprite;//存放裁剪的图片的容器
 23         private var primitiveContainer:Sprite;//存放原始图片的容器
 24         private var frame:Shape;//裁剪框
 25         private var downPoint:Point=new Point();
 26         private var bytearr:ByteArray=new ByteArray();
 27 
 28 
 29         public function CutOut():void
 30         {
 31             init();
 32         }
 33 
 34         private function init():void
 35         {
 36             this.graphics.beginFill(0xcccccc);
 37             this.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
 38             this.graphics.endFill();
 39 
 40             cutContainer = new Sprite();
 41             primitiveContainer = new Sprite();
 42             frame = new Shape();
 43 
 44             cutContainer.x = 400;
 45 
 46             this.addChild(primitiveContainer);
 47             this.addChild(cutContainer);
 48             this.addChild(frame);
 49 
 50             ldr = new Loader();
 51             ldr.load(new URLRequest("http://hiphotos.baidu.com/frostyen/pic/item/7e49d8b53f6b48ffd9335aa2.jpg"));
 52             ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
 53             primitiveContainer.addChild(ldr);
 54             this.addEventListener(MouseEvent.MOUSE_DOWN, onStageDown);
 55             stage.addEventListener(MouseEvent.MOUSE_UP, onStageUp);
 56         }
 57         private function onStageDown(e:MouseEvent):void
 58         {
 59             while (cutContainer.numChildren>0)
 60             {
 61                 cutContainer.removeChildAt(0);
 62             }
 63 
 64             downPoint.x = mouseX;
 65             downPoint.y = mouseY;
 66             frame.graphics.clear();
 67             primitiveContainer.addEventListener(MouseEvent.MOUSE_MOVE,onMove);
 68         }
 69 
 70         private function onMove(e:MouseEvent):void
 71         {
 72             frame.graphics.clear();
 73             frame.graphics.lineStyle(1,0xff00ff);
 74             frame.graphics.drawRect(0, 0, mouseX - downPoint.x, mouseY - downPoint.y);
 75             frame.x = downPoint.x;
 76             frame.y = downPoint.y;
 77         }
 78 
 79         private function onStageUp(e:MouseEvent):void
 80         {
 81             primitiveContainer.removeEventListener(MouseEvent.MOUSE_MOVE,onMove);
 82             if (frame.width != 0)
 83             {
 84                 var bmpd1:BitmapData = new BitmapData(primitiveContainer.width,primitiveContainer.height);
 85                 bmpd1.draw(primitiveContainer);
 86                 bytearr = bmpd1.getPixels(new Rectangle(frame.x,frame.y,frame.width,frame.height));
 87                 bytearr.position = 0;
 88                 var bmpd2:BitmapData = new BitmapData(frame.width,frame.height);
 89                 try
 90                 {
 91                     bmpd2.setPixels(new Rectangle(0,0,frame.width,frame.height),bytearr);
 92                 }
 93                 catch (err:Error)
 94                 {
 95                     trace(err.message);//若裁剪框拉的范围超出原始图像范围会报此错,若解决,需深究.
 96                     frame.graphics.clear();
 97                     return;
 98                 }
 99                 var bmp:Bitmap = new Bitmap(bmpd2);
100                 cutContainer.addChild(bmp);
101             }
102 
103         }
104 
105         private function onComplete(e:Event):void
106         {
107             ldr.scaleX = ldr.scaleY = 0.6;
108         }
109 
110     }
111 
112 }

 

你可能感兴趣的:(actionscript)