Update: Please visit the revision: Part II – Actionscript 3 Image Cropping
Actionscript 3 offers a method to copy pixels / Bitmap Data from a Bitmap Object. I was thinking of just setting up a little demo of this by first creating a tool that will draw out a rectangle to highlight an area of an image to be copied.
If you press and drag your mouse over the movie below it will draw out the area to crop, if you use this code remember to drop in the Button component to you library.
Here is my Document Class:
package
{
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.Event;
import flash.events.MouseEvent;
import fl.controls.Button;
public class BitmapDataCrop extends Sprite
{
private var myRectangle:Shape;
private var myRectSp:Sprite;
private var ltx:Number; // top left x position
private var lty:Number; // top left y position
private var rtx:Number; // bottom right x position
private var rty:Number; // bottom right y position
private var button:Button;
private var buttonDrag:Button;
public function BitmapDataCrop()
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, rectStartingPoint);
}
private function rectStartingPoint(m:MouseEvent):void
{
ltx = mouseX;
lty = mouseY;
stage.removeEventListener(MouseEvent.MOUSE_DOWN, rectStartingPoint);
stage.addEventListener(MouseEvent.MOUSE_MOVE, rectDraw);
}
private function rectDraw(m:MouseEvent):void
{
rtx = mouseX;
rty = mouseY;
var myRectangle:Shape = new Shape();
myRectangle.graphics.beginFill(0xffffff);
myRectangle.graphics.lineStyle(1, 0x000000);
myRectangle.graphics.drawRect(ltx, lty, rtx - ltx, rty - lty);
myRectangle.graphics.endFill();
var myRectSp:Sprite = new Sprite();
myRectSp.addChild(myRectangle);
addChild (myRectSp);
stage.addEventListener(MouseEvent.MOUSE_UP, stopRectDraw);
}
private function stopRectDraw(m:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, rectDraw);
stage.removeEventListener(MouseEvent.MOUSE_UP, stopRectDraw);
addButtons();
}
private function addButtons():void
{
button = new Button();
button.width = 100;
button.height = 30;
button.move ( rtx - button.width - 10,rty - button.height - 10 );
button.label = "Copy Area";
addChild(button);
button.addEventListener(MouseEvent.CLICK, copyImage);
}
private function copyImage(m:Event):void
{
trace ("begin copy image code");
}
}
}
Thanks for the example with code!
I am trying to implement the same with flex 3. It gives me error on stage.addEventListener(MouseEvent.MOUSE_DOWN, rectStartingPoint);
I don’t know where actually things are going wrong.
can u help me out implementing this with flex 3.
awaiting for the reply.
Try this link………….it is a croppping component for flex…it will make things easier for you………….]
flexblocks.com/2008/02/20/flex-image-cropping-component/
……….download the component add the SRC in you project…..bye have a nice time
thanks a lot for the example above…
how can i save the croped area as an bitmap(jgp)
how can i save the croped area as an bitmap(jgp)?