`
hereson
  • 浏览: 1449707 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

AS3图像处理之剪裁、动态选取

阅读更多

和师傅写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);   
    }   
  
  }   
}  

评论

相关推荐

    matlab programs_图形处理_剪裁图像_

    我们将探讨MATLAB的基本图像处理概念、图像剪裁的原理以及相关的编程技巧。 首先,了解MATLAB中的图像处理基础。在MATLAB中,图像被存储为二维数组,通常是一个灰度图像或RGB三通道图像。灰度图像由一个二维矩阵...

    Matlab实现图像的批量剪裁程序源码

    在图像处理领域,批量剪裁是一项常见的任务,尤其在研究或数据分析中,当需要处理大量图像时,手动剪裁每一张图片将非常耗时。Matlab作为一种强大的数值计算和图形处理环境,提供了丰富的图像处理工具箱,使得我们...

    picturein.zip_MATLAB图片处理_Matlab剪裁图片_图像处理 剪裁_图像处理图片_图片图像处理

    图片初步处理,包括图片的读入,灰度图像转换,图片剪裁。

    图像处理论文剪裁不同处理图像的同一位置的图像块,可手动设置图像块的尺寸。MATLAB程序。

    图像处理论文剪裁不同处理图像的同一位置的图像块,可手动设置图像块的尺寸。MATLAB程序。

    as3+.net剪裁上传图片

    3. **图像处理**:在AS3中,可能使用了像`BitmapData`类来处理图像数据,实现剪裁功能。用户选择的图像会被读取、裁剪,然后以适合的格式发送到服务器。 4. **.NET Web服务或API**:在服务器端,.NET代码接收来自AS...

    基于MATLAB的GUI图像处理剪裁程序设计

    基于MATLAB的GUI图像处理程序设计实现图像的打开、剪裁、保存

    用VB写高效的图像处理程序

    "用VB写高效的图像处理程序" 图像处理是计算机视觉和图形学中非常重要的一部分,它涉及到图像的读取、处理和保存。在VB中,图像处理可以通过使用PSet和SetPixel函数来实现,但是这些函数的执行速度非常慢,特别是在...

    QQ自定义图像剪裁

    QQ自定义图像剪裁是一种常见的图像处理功能,广泛应用于社交应用如QQ中,允许用户根据自己的需求裁剪图片,以创建个性化头像或其他用途。在本文中,我们将深入探讨这一功能涉及的技术点、实现步骤以及可能遇到的问题...

    php图像处理类.zip

    这个“php图像处理类”可能是基于其中之一或两者封装的,以提供更高层次的接口和更便捷的用法。 1. 图像缩放:图像缩放是调整图像大小以适应不同的显示需求。这个类可能包含一个方法,接受原始图像路径和目标尺寸,...

    Delphi控件Draw_hss图形图像处理库

    3. 使用控件:在Form上添加控件,如TDrawImage,然后在代码中调用相关方法和属性进行图像处理。 4. 编写处理逻辑:根据需要编写处理图像的代码,例如使用滤镜函数、变换函数等。 总的来说,Draw_hss图形图像处理库...

    数字图像处理与分析 课件 刘定生

    通过刘定生老师的《数字图像处理与分析》课程,学生不仅能掌握图像处理的基本理论和技术,还能了解到该领域的前沿动态,为今后从事相关工作或研究打下坚实基础。同时,课件中的实例和习题将帮助学生将理论知识转化为...

    图像处理各种算法matlab

    在图像处理领域,MATLAB是一种常用的工具,因其强大的矩阵运算能力和丰富的图像处理库而备受青睐。这个名为"图像处理各种算法matlab"的压缩包很可能包含了一系列基于MATLAB实现的图像处理实验,这些实验覆盖了图像...

    精巧的图像剪裁(10分钟学会)

    在数字图像处理领域,图像剪裁是一项基础且实用的技术,它允许用户从一张图片中选择并提取出特定区域,以聚焦于感兴趣的细节或去除不必要部分。通过精巧的图像剪裁,我们可以创建更加紧凑、突出主题的视觉作品,无论...

    基于MATLAB的简单图像处理系统的实现.pdf

    数字图像处理是利用计算机及其它相关知识,对图像进行一系列的运算和处理的综合性学科。该学科理论较为复杂,掌握其原理对于图像处理领域的学者至关重要。由于目前的图像处理软件往往将算法封装起来,不利于学习和...

    VC++图像处理源码

    在IT行业中,图像处理是一项重要的技术,特别是在软件开发领域,如视觉艺术、计算机视觉和游戏开发等。"VC++图像处理源码"是针对初学者的一份宝贵资源,它源自吕凤军的《数字图象处理编程入门》一书。这本书通过实际...

    BMP图像灰度化 缩放 剪裁 函数合集

    在图像处理领域,BMP(Bitmap)是一种常见的位图文件格式,用于存储未经压缩的图像数据。本资源包集合了BMP图像的灰度化、缩放和剪裁等核心处理函数,对于理解和实践基本的图像操作具有重要作用。这些功能在计算机...

    Vb 图像处理

    【标题】"Vb 图像处理"涉及到的知识点主要集中在使用Visual Basic (Vb) 进行图像处理软件的开发。Visual Basic 是一种由微软公司推出的面向对象的编程语言,常用于开发桌面应用程序,因其易学性和丰富的控件库而受到...

    3d 消隐 剪裁

    消隐算法处理的是如何在3D场景中正确地隐藏那些被其他对象遮挡的物体部分,使得图像看起来更加真实。剪裁则涉及到将超出视口范围的3D对象部分剔除,以减少不必要的计算,提高渲染效率。 消隐算法主要有深度排序法...

    VC图像处理—《VC++图像处理程序设计》光盘代码

    《VC++图像处理程序设计》是一本深入探讨VC++在图像处理领域的专著,而提供的压缩包文件包含了这本书配套的源代码,这对于学习和实践VC++图像处理技术来说是宝贵的资源。VC++作为Microsoft开发的一款强大的C++集成...

    Python-thumbor一个小型图像服务具有剪裁尺寸重设和翻转功能

    其高效、灵活的特点使其成为Python开发中的图像处理首选工具之一。 总的来说,Python-thumbor是一个强大的图像处理解决方案,它的智能裁剪、尺寸重设和翻转功能使得它在处理图像时既高效又精确。通过理解其核心功能...

Global site tag (gtag.js) - Google Analytics