Featured Post

Buy and Sell Flash Components

Buy and Sell flash files. You can send the product to services@activetofocus.com for activetofocus.com to sell. First, please sign up PAYPAL account, from the above web site to facilitate payment or withdrawal.

Read More

FLASH image processing / tailoring, dynamic selection

Posted by activetofocus | Posted in Flash Technology | Posted on 19-10-2009

Tags: ,

0

Here is the code related technologies.

//Marquee
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 = 0×0099FF;
public var fillColor:int = 0×0099FF;
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;
}

}
}
//Marquee
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 = 0×0099FF;
public var fillColor:int = 0×0099FF;
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?
//Crop Image
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);
}

}
}

Write a comment

Powered by WP Hashcash