和师傅写C#写的思维混乱,方法变量几乎第一反应就是大写,习惯都被改写了,.NET在代码管理上的确比Flex强悍的多。
OK,进入主题,毕设还没结束就先回公司报道了,哈哈,接下来事情不少。这个是一个项目开始要用到的,一大堆的Rectangle运算处理,头痛了一整天,其中的SelectRectanle用于选取框的生成,可拖拽,缩放,返回一个给定可视元素的被选区域(Rectangle), CutImage用于剪裁图片。
查看源代码:http://www.moorwind.com/as3app/imageprocess/srcview/index.html
代码中注意,因为要对图片进行操作,所以使用了URLStream()加载待处理图像。
view plaincopy to clipboardprint?
//选取框
package com.moorwind.imageprocesser.utils
{
import com.moorwind.imageprocesser.ui.ScaleButton;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.net.URLLoader;
public class SelectRectangle extends URLLoader
{
private var target:Sprite
private var onlayer:Sprite;
private var startX:Number;
private var startY:Number;
private var dragX:Number;
private var dragY:Number;
private var drawRect:Rectangle;
private var scaleButton:ScaleButton;
public static const SELECTED:String = "selected";
public var lineColor:int = 0x0099FF;
public var fillColor:int = 0x0099FF;
public var fillAlpha:Number = 0.1;
public var isSelectRectangleFinished:Boolean = false;
public var isScaleable:Boolean = false;
public function SelectRectangle(target:Sprite, onlayer:Sprite)
{
this.target = target;
this.onlayer = onlayer;
this.onlayer.buttonMode = true;
}
//get selected rectangle
public function get selectRectangle():Rectangle
{
return this.isSelectRectangleFinished ? this.drawRect : null;
}
// initial rectangle
public function InitSelectRectangle():void
{
if(this.isSelectRectangleFinished)
return;
this.target.addEventListener(MouseEvent.MOUSE_DOWN, this.StartDrawRect);
}
private function StartDrawRect(e:MouseEvent):void
{
this.startX = this.target.mouseX;
this.startY = this.target.mouseY;
this.target.addEventListener(MouseEvent.MOUSE_MOVE, this.DrawingRect);
this.target.addEventListener(MouseEvent.MOUSE_UP, this.EndDrawRect);
}
private function DrawingRect(e:MouseEvent):void
{
this.drawRect = new Rectangle(this.startX,this.startY,this.target.mouseX - this.startX,this.target.mouseY - this.startY);
this.onlayer.graphics.clear();
this.onlayer.graphics.lineStyle(1,this.lineColor,1);
this.onlayer.graphics.beginFill(this.fillColor,this.fillAlpha);
this.onlayer.graphics.drawRect(this.drawRect.x,this.drawRect.y,this.drawRect.width,this.drawRect.height);
this.onlayer.graphics.endFill();
e.updateAfterEvent();
}
private function EndDrawRect(e:MouseEvent):void
{
this.isSelectRectangleFinished = true;
this.target.removeEventListener(MouseEvent.MOUSE_DOWN, this.StartDrawRect);
this.target.removeEventListener(MouseEvent.MOUSE_MOVE, this.DrawingRect);
this.target.removeEventListener(MouseEvent.MOUSE_UP, this.EndDrawRect);
this.scaleButton = new ScaleButton();
this.onlayer.addChild(this.scaleButton);
this.scaleButton.x = this.drawRect.x+this.drawRect.width;
this.scaleButton.y = this.drawRect.y+this.drawRect.height;
this.dispatchEvent(new Event(SelectRectangle.SELECTED));
this.StartScaleButton();
this.InitDragEvent();
}
// scale rectangle
public function StartScaleButton():void
{
this.scaleButton.addEventListener(MouseEvent.MOUSE_DOWN, this.StartScaleRect);
}
private function StartScaleRect(e:MouseEvent):void
{
this.isScaleable = true;
this.target.addEventListener(MouseEvent.MOUSE_MOVE, this.ScalingRect);
this.target.addEventListener(MouseEvent.MOUSE_UP, this.EndScaleRect);
}
private function ScalingRect(e:MouseEvent):void
{
this.drawRect = new Rectangle(this.drawRect.x,this.drawRect.y,this.target.mouseX - this.drawRect.x,this.target.mouseY - this.drawRect.y);
this.onlayer.graphics.clear();
this.onlayer.graphics.lineStyle(1,this.lineColor,1);
this.onlayer.graphics.beginFill(this.fillColor,this.fillAlpha);
this.onlayer.graphics.drawRect(this.drawRect.x,this.drawRect.y,this.drawRect.width,this.drawRect.height);
this.onlayer.graphics.endFill();
this.scaleButton.x = this.drawRect.x+this.drawRect.width;
this.scaleButton.y = this.drawRect.y+this.drawRect.height;
e.updateAfterEvent();
}
private function EndScaleRect(e:MouseEvent):void
{
this.isScaleable = false;
this.target.removeEventListener(MouseEvent.MOUSE_MOVE, this.ScalingRect);
this.target.removeEventListener(MouseEvent.MOUSE_UP, this.EndScaleRect);
this.dispatchEvent(new Event(SelectRectangle.SELECTED));
}
// drag rectangle
public function InitDragEvent():void
{
this.onlayer.addEventListener(MouseEvent.MOUSE_DOWN, this.StartDragRect);
}
private function StartDragRect(e:MouseEvent):void
{
if(this.isScaleable)
return;
this.dragX = this.target.mouseX - this.drawRect.x;
this.dragY = this.target.mouseY - this.drawRect.y;
this.target.addEventListener(MouseEvent.MOUSE_MOVE, this.DragingRect);
this.target.addEventListener(MouseEvent.MOUSE_UP, this.EndDragEvent);
}
private function DragingRect(e:MouseEvent):void
{
this.drawRect = new Rectangle(this.target.mouseX - this.dragX,this.target.mouseY - this.dragY,this.drawRect.width, this.drawRect.height);
this.onlayer.graphics.clear();
this.onlayer.graphics.lineStyle(1,this.lineColor,1);
this.onlayer.graphics.beginFill(this.fillColor,this.fillAlpha);
this.onlayer.graphics.drawRect(this.drawRect.x,this.drawRect.y,this.drawRect.width,this.drawRect.height);
this.onlayer.graphics.endFill();
this.scaleButton.x = this.drawRect.x+this.drawRect.width;
this.scaleButton.y = this.drawRect.y+this.drawRect.height;
e.updateAfterEvent();
}
private function EndDragEvent(e:MouseEvent):void
{
this.target.removeEventListener(MouseEvent.MOUSE_MOVE, this.DragingRect);
this.target.removeEventListener(MouseEvent.MOUSE_UP, this.EndDragEvent);
this.dispatchEvent(new Event(SelectRectangle.SELECTED));
}
// cancel selected box
public function Cancel():void
{
this.onlayer.graphics.clear();
this.onlayer.removeEventListener(MouseEvent.MOUSE_DOWN, this.StartDragRect);
this.scaleButton.removeEventListener(MouseEvent.MOUSE_DOWN, this.StartScaleRect);
this.onlayer.removeChild(this.scaleButton);
this.isSelectRectangleFinished = false;
}
}
}
//选取框
package com.moorwind.imageprocesser.utils
{
import com.moorwind.imageprocesser.ui.ScaleButton;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.net.URLLoader;
public class SelectRectangle extends URLLoader
{
private var target:Sprite
private var onlayer:Sprite;
private var startX:Number;
private var startY:Number;
private var dragX:Number;
private var dragY:Number;
private var drawRect:Rectangle;
private var scaleButton:ScaleButton;
public static const SELECTED:String = "selected";
public var lineColor:int = 0x0099FF;
public var fillColor:int = 0x0099FF;
public var fillAlpha:Number = 0.1;
public var isSelectRectangleFinished:Boolean = false;
public var isScaleable:Boolean = false;
public function SelectRectangle(target:Sprite, onlayer:Sprite)
{
this.target = target;
this.onlayer = onlayer;
this.onlayer.buttonMode = true;
}
//get selected rectangle
public function get selectRectangle():Rectangle
{
return this.isSelectRectangleFinished ? this.drawRect : null;
}
// initial rectangle
public function InitSelectRectangle():void
{
if(this.isSelectRectangleFinished)
return;
this.target.addEventListener(MouseEvent.MOUSE_DOWN, this.StartDrawRect);
}
private function StartDrawRect(e:MouseEvent):void
{
this.startX = this.target.mouseX;
this.startY = this.target.mouseY;
this.target.addEventListener(MouseEvent.MOUSE_MOVE, this.DrawingRect);
this.target.addEventListener(MouseEvent.MOUSE_UP, this.EndDrawRect);
}
private function DrawingRect(e:MouseEvent):void
{
this.drawRect = new Rectangle(this.startX,this.startY,this.target.mouseX - this.startX,this.target.mouseY - this.startY);
this.onlayer.graphics.clear();
this.onlayer.graphics.lineStyle(1,this.lineColor,1);
this.onlayer.graphics.beginFill(this.fillColor,this.fillAlpha);
this.onlayer.graphics.drawRect(this.drawRect.x,this.drawRect.y,this.drawRect.width,this.drawRect.height);
this.onlayer.graphics.endFill();
e.updateAfterEvent();
}
private function EndDrawRect(e:MouseEvent):void
{
this.isSelectRectangleFinished = true;
this.target.removeEventListener(MouseEvent.MOUSE_DOWN, this.StartDrawRect);
this.target.removeEventListener(MouseEvent.MOUSE_MOVE, this.DrawingRect);
this.target.removeEventListener(MouseEvent.MOUSE_UP, this.EndDrawRect);
this.scaleButton = new ScaleButton();
this.onlayer.addChild(this.scaleButton);
this.scaleButton.x = this.drawRect.x+this.drawRect.width;
this.scaleButton.y = this.drawRect.y+this.drawRect.height;
this.dispatchEvent(new Event(SelectRectangle.SELECTED));
this.StartScaleButton();
this.InitDragEvent();
}
// scale rectangle
public function StartScaleButton():void
{
this.scaleButton.addEventListener(MouseEvent.MOUSE_DOWN, this.StartScaleRect);
}
private function StartScaleRect(e:MouseEvent):void
{
this.isScaleable = true;
this.target.addEventListener(MouseEvent.MOUSE_MOVE, this.ScalingRect);
this.target.addEventListener(MouseEvent.MOUSE_UP, this.EndScaleRect);
}
private function ScalingRect(e:MouseEvent):void
{
this.drawRect = new Rectangle(this.drawRect.x,this.drawRect.y,this.target.mouseX - this.drawRect.x,this.target.mouseY - this.drawRect.y);
this.onlayer.graphics.clear();
this.onlayer.graphics.lineStyle(1,this.lineColor,1);
this.onlayer.graphics.beginFill(this.fillColor,this.fillAlpha);
this.onlayer.graphics.drawRect(this.drawRect.x,this.drawRect.y,this.drawRect.width,this.drawRect.height);
this.onlayer.graphics.endFill();
this.scaleButton.x = this.drawRect.x+this.drawRect.width;
this.scaleButton.y = this.drawRect.y+this.drawRect.height;
e.updateAfterEvent();
}
private function EndScaleRect(e:MouseEvent):void
{
this.isScaleable = false;
this.target.removeEventListener(MouseEvent.MOUSE_MOVE, this.ScalingRect);
this.target.removeEventListener(MouseEvent.MOUSE_UP, this.EndScaleRect);
this.dispatchEvent(new Event(SelectRectangle.SELECTED));
}
// drag rectangle
public function InitDragEvent():void
{
this.onlayer.addEventListener(MouseEvent.MOUSE_DOWN, this.StartDragRect);
}
private function StartDragRect(e:MouseEvent):void
{
if(this.isScaleable)
return;
this.dragX = this.target.mouseX - this.drawRect.x;
this.dragY = this.target.mouseY - this.drawRect.y;
this.target.addEventListener(MouseEvent.MOUSE_MOVE, this.DragingRect);
this.target.addEventListener(MouseEvent.MOUSE_UP, this.EndDragEvent);
}
private function DragingRect(e:MouseEvent):void
{
this.drawRect = new Rectangle(this.target.mouseX - this.dragX,this.target.mouseY - this.dragY,this.drawRect.width, this.drawRect.height);
this.onlayer.graphics.clear();
this.onlayer.graphics.lineStyle(1,this.lineColor,1);
this.onlayer.graphics.beginFill(this.fillColor,this.fillAlpha);
this.onlayer.graphics.drawRect(this.drawRect.x,this.drawRect.y,this.drawRect.width,this.drawRect.height);
this.onlayer.graphics.endFill();
this.scaleButton.x = this.drawRect.x+this.drawRect.width;
this.scaleButton.y = this.drawRect.y+this.drawRect.height;
e.updateAfterEvent();
}
private function EndDragEvent(e:MouseEvent):void
{
this.target.removeEventListener(MouseEvent.MOUSE_MOVE, this.DragingRect);
this.target.removeEventListener(MouseEvent.MOUSE_UP, this.EndDragEvent);
this.dispatchEvent(new Event(SelectRectangle.SELECTED));
}
// cancel selected box
public function Cancel():void
{
this.onlayer.graphics.clear();
this.onlayer.removeEventListener(MouseEvent.MOUSE_DOWN, this.StartDragRect);
this.scaleButton.removeEventListener(MouseEvent.MOUSE_DOWN, this.StartScaleRect);
this.onlayer.removeChild(this.scaleButton);
this.isSelectRectangleFinished = false;
}
}
}
view plaincopy to clipboardprint?
//剪裁图像
package com.moorwind.imageprocesser.edit
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Point;
import flash.geom.Rectangle;
public class CutImage
{
public static function Cut(data:Bitmap, rect:Rectangle):Bitmap
{
var bitmapdata:BitmapData = data.bitmapData;
var retdata:BitmapData = new BitmapData(rect.width, rect.height, false);
retdata.copyPixels(bitmapdata,rect,new Point(0,0));
return new Bitmap(retdata);
}
}
}
本文详细介绍了在C#项目中使用SelectRectangle类进行矩形选取、剪裁图片的操作流程,包括选取框的生成、拖拽、缩放及取消选择等关键功能。
6474

被折叠的 条评论
为什么被折叠?



