AS3图像处理之剪裁、动态选取 |
| [来源:圣叹 | 作者:圣叹 | 时间:2008-06-09 | 点击: | 收藏本文 【大 中 小】] |
|
和师傅写C#写的思维混乱,方法变量几乎第一反应就是大写,习惯都被改写了,.NET在代码管理上的确比Flex强悍的多。 OK,进入主题,毕设还没结束就先回公司报道了,哈哈,接下来事情不少。这个是一个项目开始要用到的,一大堆的Rectangle运算处理,头痛了一整天,其中的SelectRectanle用于选取框的生成,可拖拽,缩放,返回一个给定可视元素的被选区域(Rectangle), CutImage用于剪裁图片。
查看源代码:http://www.moorwind.com/as3app/imageprocess/srcview/index.html
代码中注意,因为要对图片进行操作,所以使用了URLStream()加载待处理图像。
-
- 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;
- }
-
-
-
- public function get selectRectangle():Rectangle
- {
- return this.isSelectRectangleFinished ? this.drawRect : null;
- }
-
-
- 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();
- }
-
-
- 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));
- }
-
-
- 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));
- }
-
-
- 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.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);
- }
-
- }
- }
|
|
| 文章如果有错误或者缺少文件,请发邮件提交给我们 |
|
|
|
|
|
|
|
| >>> 最新评论:(共有 0 位网友发表了评论) 查看所有评论 |
|
|
| 发表评论 |
|
| ·本站发布内容均为客观表达作者观点,不代表闪无忧立场,请勿攻击和漫骂 |
| ·用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任 |
| ·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据 |
| ·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为 |
|
|
|
 |
教程分类 |
|
|
|
|
|
|