package com.wdxc { /** * ... * @author luqinglong */ public class MouseInfo { /*鼠标左边缘、右边缘等*/ private var _position:uint = 100 ; /*鼠标状态 按下、放开*/ private var _state :uint = 100; public function MouseInfo() { } public function set position(p:uint):void { this._position = p ; } public function get position():uint { return this._position ; } public function set state(s:uint):void { this._state = s ; } public function get state():uint { return _state ; } /** * 是否满足对左边进行拖动拉伸的条件。 * @return boolean */ public function isStartLeftDrag():Boolean { if (_position == Constant.LEFT && _state == Constant.MOUSE_DOWN) { return true ; } return false ; } /** * 是否满足对右边界进行拖动拉伸的条件。 * @return boolean */ public function isStartRightDrag():Boolean { if (_position == Constant.RIGHT && _state == Constant.MOUSE_DOWN) { return true ; } return false ; } /** * 是否满足对上边界进行拖动拉伸的条件。 * @return boolean */ public function isStartUpDrag():Boolean { if (_position == Constant.UP && _state == Constant.MOUSE_DOWN) { return true ; } return false ; } /** * 是否满足对下边界进行拖动拉伸的条件。 * @return boolean */ public function isStartDownDrag():Boolean { if (_position == Constant.DOWN && _state == Constant.MOUSE_DOWN) { return true ; } return false ; } /** * 是否满足对裁剪区域进行拖动拉伸的条件。 * @return boolean */ public function isStartMiddleDrag():Boolean { if (_position == Constant.MIDDLE && _state == Constant.MOUSE_DOWN) { return true ; } return false ; } /** * 是否满足对左上角拖动拉伸的条件。 * @return boolean */ public function isStratLeftUPDrag():Boolean { if (_position == Constant.LEFT_UP && _state == Constant.MOUSE_DOWN) { return true ; } return false ; } /** * 是否满足对左下角拖动拉伸的条件。 * @return boolean */ public function isStratLeftDownDrag():Boolean { if (_position == Constant.LEFT_DOWN && _state == Constant.MOUSE_DOWN) { return true ; } return false ; } /** * 是否满足对右上角拖动拉伸的条件。 * @return boolean */ public function isStratRightUPDrag():Boolean { if (_position == Constant.RIGHT_UP && _state == Constant.MOUSE_DOWN) { return true ; } return false ; } /** * 是否满足对右下角拖动拉伸的条件。 * @return boolean */ public function isStratRightDownDrag():Boolean { if (_position == Constant.RIGHT_DOWN && _state == Constant.MOUSE_DOWN) { return true ; } return false ; } } }
代码一:
package { import com.wdxc.ImageCut; import com.wdxc.ImageRotation; import flash.geom.Point; import flash.geom.Rectangle; import flash.net.URLLoader; import flash.net.URLRequest; import flash.events.* ; import flash.display.*; import flash.geom.* ; import flash.net.*; import flash.text.TextField; import flash.utils.ByteArray; import com.wdxc.util.* ; import fl.containers.ScrollPane; import flash.display.Shape; import fl.events.ScrollEvent; import flash.external.ExternalInterface; import flash.system.Security; import flash.utils.Timer; import flash.events.TimerEvent ; import com.wdxc.* ; /** * @author luqinglong */ public class Main extends MovieClip { //存储加载进来的图片数据 private var _bitmap:Bitmap ; //进度条 private var _progressbar:TextField ; private var picname:String ; public var _imageCut:ImageCut = new ImageCut() ; //场景引用 public static var SCENE:MovieClip ; public static var _handerState:int = 0 ; public var pelsHint:TextField = new TextField(); public var photoid :String = "0"; public var ir:ImageRotation ; /** * 原图和中图的压缩比例 */ public var picScale :Number = 2 ; public function Main():void { flash.system.Security.allowDomain("*"); photoid = this.loaderInfo.parameters['photoid']; //photoid = "284186" ; SCENE = this; menu_mc.cut_btn.addEventListener(MouseEvent.MOUSE_DOWN, this._imageCut.startCutPic); menu_mc.rotate_btn.addEventListener(MouseEvent.MOUSE_DOWN, rotationHandler) ; checkAvailable(); this.addPelsHint(); startLoadPic(); } /** * 加入象素提醒 */ private function addPelsHint():void { this.addChild(pelsHint); //pelsHint.text = "象素不够" ; pelsHint.x = 150 ; pelsHint.y = 0 ; pelsHint.height = 20 ; this.pelsHint.width = 350 ; pelsHint.textColor =0xFF0000; } public function setPelsHint(p:Number,hint:String):void { this.pelsHint.text = "像素:" + String(p) + " " + hint ; } private function rotationHandler(e:MouseEvent):void { ir = new ImageRotation() ; ir.x = menu_mc.x ; ir.y = menu_mc.y ; this.stage.addChild(ir); } /** * 得到最低象素值 。 */ /* public function getMaxPels():Number { return this.plength/ 500 ; } */ //public function setPelsScale(p:Number) { //this.plength = p / 500 ; //} public function set bitmap(bp:Bitmap):void { this._bitmap = bp ; } public function get bitmap():Bitmap { return this._bitmap ; } public function startLoadPic():void { var param:URLVariables = new URLVariables() ; var url:String = "/photo/LoaderPhotoServlet"; //var url:String = "http://localhost:8080//photo/LoaderPhotoServlet" ; param.random = Math.floor(Math.random() * 1000); param.photoid = photoid ; //output.text = photoid ; //url += Math.floor(Math.random() * 1000); var request:URLRequest = new URLRequest(url); request.data = param ; var loader :Loader = new Loader(); loader.load(request); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, picLoadCompleteHandler); } /** * 导入二进制完成 * @param e */ private function picLoadCompleteHandler(e:Event):void { /**删除进度条*/ if (Main._handerState == 1||Main._handerState==2) { this.delVagueBg(); delLoadingBar(); calljs(); } Main._handerState = 0 ; /* if (this._bitmap.bitmapData != null) { this._bitmap.bitmapData.dispose() ; //this.removeChild(_bitmap); } */ this._bitmap = new Bitmap() ; var loader:Loader = e.target.loader as Loader; var _bitmapdata:BitmapData = new BitmapData(loader.width, loader.height); _bitmapdata.draw(loader); /** * 判断横版照片,竖版照片 */ /* if ( loader.height > loader.width) { //竖版照片 this.cutscaleFlag = Constant.VERTICAL ; } */ this._bitmap.bitmapData = _bitmapdata; this.addChild(_bitmap); this.reset() ; //设置原图引用 this._imageCut.bitmap = this._bitmap ; this.addChild(this._imageCut); e.currentTarget.removeEventListener(Event.COMPLETE, picLoadCompleteHandler); } /** * 删除以前的Bitmapt对象,注入新的进去 。 */ public function removeBitmap():void { this.removeChild(_bitmap); } public function reset():void { this._bitmap.x = (Constant.LEFT_AREA_WIDTH-_bitmap.width)/2 ; this._bitmap.y = (Constant.SCRENE_HEIGHT - _bitmap.height) / 2 ; } public function checkAvailable():void { if (ExternalInterface.available){ try { ExternalInterface.addCallback("sendToActionScript", receivedFromJavaScript); if (checkJavaScriptReady()) { //trace("JavaScript is ready.\n"); } else { //trace("JavaScript is not ready, creating timer.\n"); var readyTimer:Timer = new Timer(100, 0); readyTimer.addEventListener(TimerEvent.TIMER, timerHandler); readyTimer.start(); } }catch (error:SecurityError) { //trace("A SecurityError occurred: " + error.message + "\n"); //output.text = "error1"; }catch (error:Error) { //trace("An Error occurred: " + error.message + "\n"); } }else{ //trace("External interface is not available for this container."); } } private function timerHandler(event:TimerEvent):void { var isReady:Boolean = checkJavaScriptReady(); //output.appendText("123"); if (isReady) { //trace("JavaScript is ready.\n"); Timer(event.target).stop(); } } private function receivedFromJavaScript(pid:String, plength:Number):void { //pelsHint.text = pid + "==" + plength ; this.photoid = pid ; //设置中图和原图的像素比 this.picScale = plength/500 ; //this._bitmap.bitmapData.dispose(); //output.text = value ; removeBitmap(); // 点击了裁剪按钮时 if (Main._handerState == 5) { _imageCut.removeCutMenu(); } // 裁剪时候单击保存时候 else if (Main._handerState == 6) { _imageCut.removeCutMenu(); _imageCut.removeHint(); } else if (Main._handerState == 10) { ir.remove(); } //this._bitmap = null ; startLoadPic(); } private function checkJavaScriptReady():Boolean { var isReady:Boolean = ExternalInterface.call("isReady"); return isReady; } public function calljs():void { if (ExternalInterface.available) { ExternalInterface.call("rationAndCropComplete", "123"); } } public var _vaguebg:Sprite ; public function addVagueBg():void { _vaguebg = new Sprite() ; _vaguebg.graphics.beginFill(0x000000); _vaguebg.graphics.drawRect(0, 0, 770, 530); _vaguebg.graphics.endFill(); _vaguebg.alpha = 0.5; this.addChild(_vaguebg); } public function delVagueBg():void { this.removeChild(_vaguebg); } public var loadingbar:Sprite ; public function addLoadingBar():void { loadingbar = new LoadingBar(); this.addChild(loadingbar) ; loadingbar.x = 200 ; loadingbar.y = 150 ; } public function delLoadingBar():void { this.removeChild(loadingbar); } } }
代码二:
package com.wdxc { import flash.display.Bitmap; import flash.display.BitmapData; import flash.events.* ; import flash.display.* ; import flash.geom.Matrix; import flash.geom.Point; import flash.geom.Rectangle; import flash.printing.PrintJob; import flash.ui.Mouse ; //import flash.utils.Timer; import flash.utils.* ; import com.wdxc.Constant ; import fl.controls.* ; /** * 图像裁剪 * @author luqinglong */ public class ImageCut extends Sprite{ //原始图像数据数据 private var _bitmap:Bitmap ; //裁剪区域 private var _rect:Sprite ; //裁剪控制菜单 private var _cutMenu:CutMenu ; //拖动时光标图形 private var _cursor1:Sprite ; //拉伸时光标图形 private var _cursor2:Sprite ; //记录鼠标信息, private var _mouseInfo:MouseInfo = new MouseInfo() ; //鼠标按下时,保存绘制矩形的信息 private var _tempObj:Object = new Object() ; //预览时,临时保存图片数据 private var _tempBitmapData:BitmapData ; private var bg1:Sprite ;//预览加载背景时用,覆盖菜单 //模糊背景 private var _vaguebg:Sprite ; //裁剪比例 private var _cutScale:Number = 0 ; //宽和高度裁剪比例 public var cutscaleFlag:int = Constant.HORIZONTAL; public var minPels:Number = 800 * 600; private var scaleInfo:String = "0"; public function ImageCut() { } /** * 创建光标,并使其不可见 。 */ private function createCursor():void { _cursor1 = new Cursor1() ; _cursor1.x = mouseX ; _cursor1.y = mouseY ; _cursor1.visible = false ; _cursor2 = new Cursor2() ; _cursor2.x = mouseX ; _cursor2.y = mouseY ; _cursor2.visible = false ; this.addChild(_cursor1); this.addChild(_cursor2) ; } /** * 初始化,裁剪控制菜单 */ private function createCutMenu():void { //显示像素提醒 ; Main.SCENE.pelsHint.visible = true ; _cutMenu = new CutMenu(); _cutMenu.x = Main.SCENE.menu_mc.x ; _cutMenu.y = Main.SCENE.menu_mc.y ; this.addChild(_cutMenu); _cutMenu.cutok_btn.addEventListener(MouseEvent.CLICK, saveHint) ; _cutMenu.cutcancle_btn.addEventListener(MouseEvent.CLICK, cutCancle) ; _cutMenu.preview_btn.addEventListener(MouseEvent.CLICK, cutPreview) ; _cutMenu.rd5_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd6_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd62_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd7_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd8_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd10_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd12_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd16_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd18_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd20_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd24_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd30_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd40_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd0_mc.addEventListener(MouseEvent.CLICK, scaleSelectHandler) ; } /** * 创建裁剪框 */ private function createCutRect():void { _rect = new Sprite(); _rect.x = _bitmap.x ; _rect.y = _bitmap.y ; /***设置裁剪区域边框样式*/ _rect.graphics.lineStyle(Constant.BORDER, 0xFF0000, 0.8, true, LineScaleMode.NONE, CapsStyle.ROUND); _rect.graphics.beginFill(0xCCFF00,0); _rect.graphics.drawRect(0,0,_bitmap.width-20,_bitmap.height-20); _rect.graphics.endFill(); this.addChild(_rect); } public function set bitmap(bp:Bitmap):void { this._bitmap = bp ; } public function get bitmap():Bitmap { return this._bitmap ; } /**** * 开始裁剪。初始化裁剪工具。 * @param e */ public function startCutPic(e:MouseEvent):void { /** * 判断横版照片,竖版照片 */ if ( _bitmap.height > _bitmap.width) { //竖版照片 this.cutscaleFlag = Constant.VERTICAL ; } else { this.cutscaleFlag = Constant.HORIZONTAL ; } Main._handerState = 5; //cutpicing(); this.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler) ; this.stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler) ; this.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler) ; this.createCutMenu() ; this.createCutRect() ; this.createCursor() ; //_cutMenu.RadioButtonGroup1.addEventListener(Event.CHANGE, changeHandler); } private function changeHandler(e:Event):void { //RadionButton = e.target as RadionButton ; trace(123); } /** * 比例裁剪 */ private function scaleSelectHandler(e:MouseEvent):void { var rb:RadioButton = e.target as RadioButton; //为横排照片 switch(rb.value) { case "5" : this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(5 / 3.5):(3.5 / 5) ; scaleInfo = "5 : 3.5"; this.minPels = 800 * 600 ; break ; case "6" : this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(3 / 2):(2 / 3) ; scaleInfo = "3 : 2"; this.minPels = 1024 * 768 ; break ; case "62": this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(4 / 3):(3 / 4) ; scaleInfo = "4 : 3"; this.minPels = 1024 * 768 ; break ; case "7" : this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(7 / 5):(5 / 7) ; scaleInfo = "7 : 5"; this.minPels = 1280 * 960 ; break ; case "8" : this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(4 / 3):(3 / 4) ; scaleInfo = "4 : 3"; this.minPels = 1280 * 960 ; break ; case "10": this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(5 / 4):(4 / 5) ; scaleInfo = "5 : 4"; this.minPels = 1600 * 1200 ; break ; case "12": this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(6 / 5):(5 / 6) ; scaleInfo = "6 : 5"; this.minPels = 1600 * 1200 ; break ; case "16": this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(4 / 3):(3 / 4) ; this.minPels = 2048 * 1953 ; scaleInfo = "4 : 3"; break ; case "18": this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(3 / 2):(2 / 3) ; this.minPels = 2048 * 1953 ; scaleInfo = "3 : 2"; break ; case "20": this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(5 / 4):(4 / 5) ; this.minPels = 2048 * 1953 ; scaleInfo = "5 : 4"; break ; case "24": this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(6 / 5):(5 / 6) ; this.minPels = 2048 * 1953 ; scaleInfo = "6 : 5"; break ; case "30": this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(5 / 4):(4 / 5) ; this.minPels = 2048 * 1953 ; scaleInfo = "5 : 4"; break ; case "40": this._cutScale = (this.cutscaleFlag == Constant.HORIZONTAL)?(4 / 3):(3 / 4) ; this.minPels = 2048 * 1953 ; scaleInfo = "4 : 3"; break case "0": this._cutScale = 0; this.minPels = 800 * 600 ; scaleInfo = "0"; break ; } this._rect.graphics.clear(); this._rect.x = _bitmap.x ; this._rect.y = _bitmap.y ; _rect.graphics.lineStyle(Constant.BORDER, 0xFF0000, 0.8, true, LineScaleMode.NONE, CapsStyle.ROUND); _rect.graphics.beginFill(0xCCFF00, 0); if (this._cutScale == 0) { _rect.graphics.drawRect(0, 0, _bitmap.width-20,_bitmap.height-20); }else { var w = _bitmap.width ; var h = w / this._cutScale ; /*按比例显示最大尺寸*/ if (h < _bitmap.height) { _rect.graphics.drawRect(0,0,w,h); }else { _rect.graphics.drawRect(0,0,_bitmap.height*this._cutScale,_bitmap.height); } } _rect.graphics.endFill(); } private function cutCancle(e:MouseEvent):void { Main._handerState = 0 ; removeCutMenu(); } /** * 保存裁剪应用 */ private function cutApplication(e:MouseEvent):void { // 保存裁剪 导入条 Main.SCENE.addLoadingBar(); var server :ImageService = new ImageService(); server.photoid = Main.SCENE.photoid ; var tempx:Number = Math.round(_rect.x - this._bitmap.x); var tempy:Number = Math.round(_rect.y - this._bitmap.y) ; server.submitCut(tempx, tempy, Math.round(_rect.width), Math.round(_rect.height),this.scaleInfo); this.removeCutMenu(); _cutSaveHint.removeEventListener(MouseEvent.CLICK, removeHintHandler); _cutSaveHint.removeEventListener(MouseEvent.CLICK, cutApplication); this.stage.removeChild(_cutSaveHint); Main._handerState = 1 ; //得到长边 var ml = _rect.width > _rect.height?_rect.width:_rect.height ; //重新计算中图和大图的比例 Main.SCENE.picScale = Main.SCENE.picScale * ml/500 ; } public var _cutSaveHint:CutSaveHint ; /** * 保存时候提示说明 * @param e */ private function saveHint(e:MouseEvent):void { // = 背景和提示同时存在 Main._handerState = 6 ; Main.SCENE.addVagueBg(); _cutSaveHint = new CutSaveHint(); _cutSaveHint.x = (Constant.LEFT_AREA_WIDTH-_cutSaveHint.width)/2 ; _cutSaveHint.y = 200 ; this.stage.addChild(_cutSaveHint); _cutSaveHint.ok_btn.addEventListener(MouseEvent.CLICK, cutApplication); _cutSaveHint.cancle1_btn.addEventListener(MouseEvent.CLICK,removeHintHandler); this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler) ; } private function removeHintHandler(e:MouseEvent):void { removeHint(); } public function removeHint():void { //单击裁剪 Main._handerState = 5 ; //删除提示按钮的监听器 _cutSaveHint.removeEventListener(MouseEvent.CLICK, removeHintHandler); _cutSaveHint.removeEventListener(MouseEvent.CLICK, cutApplication); this.stage.removeChild(_cutSaveHint); //戍边回复监听移动 this.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler) ; Main.SCENE.delVagueBg(); } /** * 裁剪时预览 * @param e */ private function cutPreview(e:MouseEvent):void { bg1 = new Sprite() ; bg1.graphics.beginFill(0x000000); bg1.graphics.drawRect(0, 0, 770, 530); bg1.graphics.endFill(); bg1.alpha = 0; bg1.x = Constant.LEFT_AREA_WIDTH+Constant.MIDDLE_AREA_WIDTH ; this.addChild(bg1); //b保存一份副本,预览时候来用。 _tempBitmapData = this._bitmap.bitmapData.clone() ; //存放裁剪出的新图片 var cutbitmapdata:BitmapData = new BitmapData(Math.floor(_rect.width),Math.ceil(_rect.height)) ; //坐标转化,把坐标移到裁剪区域的位置,宽度和高度在cutbitmapdata里指定。 var matrix:Matrix = new Matrix(1, 0, 0, 1, -Math.floor(_rect.x-_bitmap.x), -Math.floor(_rect.y-_bitmap.y)); cutbitmapdata.draw(this._bitmap.bitmapData, matrix); this._bitmap.bitmapData.dispose() ; this._bitmap.bitmapData = cutbitmapdata ; this._rect.visible = false ; Main.SCENE.reset(); setTimeout(previewCompletehandler, 2000) ; } private function previewCompletehandler():void { this._bitmap.bitmapData.dispose(); this._bitmap.bitmapData = this._tempBitmapData ; Main.SCENE.reset(); this._rect.visible = true ; this.removeChild(bg1) ; } /** * 删除裁剪控制菜单 */ public function removeCutMenu():void { //恢复原值 Main._handerState = 0 ; //象素不提醒 Main.SCENE.pelsHint.visible = false ; _cutMenu.cutok_btn.removeEventListener(MouseEvent.MOUSE_DOWN, cutApplication); _cutMenu.cutcancle_btn.removeEventListener(MouseEvent.MOUSE_DOWN, cutCancle) ; /*删除裁剪比例监听*/ _cutMenu.rd5_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd6_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd62_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd7_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd8_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd10_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd12_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd16_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd18_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd20_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd24_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd30_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd40_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ; _cutMenu.rd0_mc.removeEventListener(MouseEvent.CLICK, scaleSelectHandler) ; this._cutScale = 0 ; this.stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler) ; this.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler) ; this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler) ; this.removeChild(_cutMenu) ; this.removeChild(_rect) ; this.removeChild(_cursor1); this.removeChild(_cursor2) ; } /** * 鼠标按下处理 * @param e */ private function mouseDownHandler(e:MouseEvent):void { /*保存按下前状态**/ this._tempObj.width = _rect.width-Constant.BORDER ; this._tempObj.height = _rect.height-Constant.BORDER ; this._tempObj.x = _rect.x ; this._tempObj.y = _rect .y ; /*鼠标为按下状态*/ this._mouseInfo.state = Constant.MOUSE_DOWN ; } private function mouseUpHandler(e:MouseEvent):void { _rect.stopDrag(); /*状态设置为弹起状态**/ this._mouseInfo.state = Constant.MOUSE_UP ; } /** * 鼠标移动 * @param e */ private function mouseMoveHandler(e:MouseEvent):void { Mouse.hide() ; this._cursor1.visible = false ; this._cursor2.visible = false ; /*拖动左边界*/ if (_mouseInfo.isStartLeftDrag()) { this.resetCursorPosition(Constant.CURSOR_HORIZONTAL) ; this.repaint(Constant.LEFT); } /*拖动右边界*/ else if(_mouseInfo.isStartRightDrag()) { this.resetCursorPosition(Constant.CURSOR_HORIZONTAL); this.repaint(Constant.RIGHT); } /*拖动上边界*/ else if(_mouseInfo.isStartUpDrag()) { this.resetCursorPosition(Constant.CURSOR_VERTICAL); this.repaint(Constant.UP); } /*拖动下边界*/ else if(_mouseInfo.isStartDownDrag()) { this.resetCursorPosition(Constant.CURSOR_VERTICAL); this.repaint(Constant.DOWN); } /*拖动整个区域*/ else if (_mouseInfo.isStartMiddleDrag()) { this.resetCursorPosition(Constant.CURSOR_MOVE); this.dragHandler(); } /*拖动左上角*/ else if (_mouseInfo.isStratLeftUPDrag()) { this.resetCursorPosition(Constant.CURSOR_DIAGONAL_LEFT) ; this.repaint(Constant.LEFT_UP) ; } /*拖动左下角*/ else if (_mouseInfo.isStratLeftDownDrag()) { this.resetCursorPosition(Constant.CURSOR_DIAGONAL_RIGHT) ; this.repaint(Constant.LEFT_DOWN) ; } /*拖动右上角*/ else if (_mouseInfo.isStratRightUPDrag()) { this.resetCursorPosition(Constant.CURSOR_DIAGONAL_RIGHT) ; this.repaint(Constant.RIGHT_UP) ; } /*拖动右下角*/ else if (_mouseInfo.isStratRightDownDrag()) { this.resetCursorPosition(Constant.CURSOR_DIAGONAL_LEFT) ; this.repaint(Constant.RIGHT_DOWN) ; } /*鼠标在左上角**/ else if (isLeftUpAngle()) { this.resetCursorPosition(Constant.CURSOR_DIAGONAL_LEFT); this._mouseInfo.position = Constant.LEFT_UP ; } /*鼠标在左下角**/ else if (isLeftDownAngle()) { this.resetCursorPosition(Constant.CURSOR_DIAGONAL_RIGHT); this._mouseInfo.position = Constant.LEFT_DOWN ; } /*鼠标在右上角**/ else if (isRightUpAngle()) { this.resetCursorPosition(Constant.CURSOR_DIAGONAL_RIGHT); this._mouseInfo.position = Constant.RIGHT_UP ; } /*鼠标在右下角**/ else if (isRightDownAngle()) { this.resetCursorPosition(Constant.CURSOR_DIAGONAL_LEFT); this._mouseInfo.position = Constant.RIGHT_DOWN ; } /*鼠标在左边界**/ else if (isLeftEdge()) { this.resetCursorPosition(Constant.CURSOR_HORIZONTAL); this._mouseInfo.position = Constant.LEFT ; } /*鼠标在右边界**/ else if (isRightEdge()) { this.resetCursorPosition(Constant.CURSOR_HORIZONTAL); this._mouseInfo.position = Constant.RIGHT ; } /*鼠标在上边界**/ else if (isUpEdge()) { this.resetCursorPosition(Constant.CURSOR_VERTICAL); this._mouseInfo.position = Constant.UP ; } /*鼠标在下边界**/ else if (isDownEdge()) { this.resetCursorPosition(Constant.CURSOR_VERTICAL); this._mouseInfo.position = Constant.DOWN ; } /* 鼠标在裁剪区域里。*/ else if (this.isInCutRect()) { this.resetCursorPosition(Constant.CURSOR_MOVE); this._mouseInfo.position = Constant.MIDDLE ; } else { Mouse.show () ; } e.updateAfterEvent(); /*象素提示**/ this.pelsHint(); } private function pelsHint():void { var p = _rect.width * _rect.height * Main.SCENE.picScale * Main.SCENE.picScale ; p = Math.round(p); if ( p < this.minPels) { Main.SCENE.setPelsHint(p,"像素太低,继续裁剪将会影响照片的冲印质量。"); }else { Main.SCENE.setPelsHint(p,""); } } /** * 重绘裁剪区域 。 * @param state */ private function repaint(state:uint):void { this._rect.graphics.clear() ; _rect.graphics.lineStyle(Constant.BORDER, 0xFF0000, 0.8, true, LineScaleMode.NONE, CapsStyle.ROUND); _rect.graphics.beginFill(0xCCFF00, 0); //var h:Number = 0 ; //var w:Number = 0 ; switch(state) { case Constant.LEFT: this.leftDragHandler() ; break; case Constant.RIGHT: this.rightDragHandler(); break ; case Constant.UP: this.upDragHandler(); break ; case Constant.DOWN: /*下端拖动*/ downDragHandler(); break; case Constant.LEFT_UP: this.leftUpDragHandler() ; break; case Constant.LEFT_DOWN: /*左下角拖动*/ this.leftDownDragHandler(); break; case Constant.RIGHT_UP: /*右上角拖动*/ this.rightUpDragHandler(); break; case Constant.RIGHT_DOWN: /*右下角拖动*/ this.rightDownDragHandler(); break; } _rect.graphics.endFill(); } /*左端拖动处理*/ private function leftDragHandler():void { var w = 0 ; var h = 0; if(this._cutScale!=0) { _rect.x = mouseX < _bitmap.x?_bitmap.x:mouseX ; _rect.x = _rect.x >= (_tempObj.width + _tempObj.x)?(_tempObj.width + _tempObj.x):_rect.x ; w = _tempObj.width + _tempObj.x - _rect.x ; h = w / this._cutScale ; var h2 = this._bitmap.height+this._bitmap.y - this._tempObj.y ; if(h>=h2){ _rect.x = this._tempObj.x + this._tempObj.width -h2*this._cutScale; w = _tempObj.width + _tempObj.x - _rect.x ; _rect.graphics.drawRect(0, 0, w, h2 ); } else { _rect.graphics.drawRect(0, 0, w , h ); } } else { /*左端拖动*/ _rect.x = mouseX < _bitmap.x?_bitmap.x:mouseX ; _rect.x = _rect.x >= (_tempObj.width + _tempObj.x)?(_tempObj.width + _tempObj.x):_rect.x ; _rect.graphics.drawRect(0, 0, _tempObj.width + _tempObj.x - _rect.x, _tempObj.height); } } private function rightDragHandler():void { var w:Number = 0 ; var h:Number = 0 ; if (this._cutScale != 0) { /*右端拖动*/ w = mouseX > (w = (_bitmap.x + _bitmap.width))?w:mouseX ; w = w <= this._tempObj.x?_tempObj.x:w ; //宽度 w = w -this._tempObj.x ; h = w/ this._cutScale ; _rect.y = _tempObj.height + _tempObj.y - h; if (_rect.y < _bitmap.y) { h = _tempObj.height + _tempObj.y -_bitmap.y ; _rect.y = _bitmap.y ; w = h * this._cutScale ; //w = w - this._tempObj.x ; //trace("h=="+h); } //trace("_rect.y=="+_rect.y+" bitmap.y=="+_bitmap.y); _rect.graphics.drawRect(0, 0, w,h); } else { /*右端拖动,任意裁剪*/ w = mouseX > (w = (_bitmap.x + _bitmap.width))?w:mouseX ; w = w <= this._tempObj.x?_tempObj.x:w ; _rect.graphics.drawRect(0, 0, w -this._tempObj.x, _tempObj.height); } } /** * 上端拖动处理 */ private function upDragHandler():void { if (this._cutScale != 0) { /*按一定比例裁剪**/ _rect.y = (mouseY < _bitmap.y)?_bitmap.y:mouseY; _rect.y = _rect.y >=(_tempObj.height + _tempObj.y)?(_tempObj.height + _tempObj.y): _rect.y ; var h = _tempObj.height + _tempObj.y-_rect.y ; var w = h * this._cutScale ; _rect.x = _tempObj.x + _tempObj.width - w ; /** * 超过最左边 */ if (_rect.x < _bitmap.x) { w = _tempObj.x + _tempObj.width - _bitmap.x ; _rect.x = _bitmap.x ; h = w / this._cutScale ; _rect.y = _tempObj.y + _tempObj.height - h ; } _rect.graphics.drawRect(0, 0,w,h); }else { /*任意尺寸裁剪**/ _rect.y = (mouseY < _bitmap.y)?_bitmap.y:mouseY; _rect.y = _rect.y >=(_tempObj.height + _tempObj.y)?(_tempObj.height + _tempObj.y): _rect.y ; _rect.graphics.drawRect(0, 0, _tempObj.width,_tempObj.height+_tempObj.y-_rect.y); } } /** * 下端拖动处理 */ private function downDragHandler():void { var h = 0 ; var w = 0; if (this._cutScale != 0) { h = mouseY > (h = (_bitmap.y + _bitmap.height))?h:mouseY ; h = h <= this._tempObj.y?_tempObj.y:h ; //高度 h = h - _tempObj.y ; w = h * this._cutScale ; if ((w + _tempObj.x) >= (_bitmap.width+_bitmap.x)) { w = _bitmap.x + _bitmap.width - _rect.x ; h = w / this._cutScale ; } _rect.graphics.drawRect(0, 0, w, h); }else { h = mouseY > (h = (_bitmap.y + _bitmap.height))?h:mouseY ; h = h <= this._tempObj.y?_tempObj.y:h ; _rect.graphics.drawRect(0, 0, _tempObj.width, h-_tempObj.y); } } /** * 左上拖动处理 */ private function leftUpDragHandler():void { if (this._cutScale != 0) { this.upDragHandler(); }else { /*左上角拖动*/ _rect.x = mouseX <= _bitmap.x?_bitmap.x:mouseX ; _rect.y = (mouseY <= _bitmap.y)?_bitmap.y:mouseY; var w = _tempObj.width + _tempObj.x ; var h = _tempObj.height + _tempObj.y ; _rect.x = _rect.x >= (w)?(w):_rect.x ; _rect.y = _rect.y >=(h)?(h): _rect.y ; _rect.graphics.drawRect(0, 0, w-_rect.x,h-_rect.y); } } /** * 左下按比例拖动处理 */ private function leftDownDragHandler():void { if (this._cutScale != 0) { this.leftDragHandler(); }else { _rect.x = mouseX <= _bitmap.x?_bitmap.x:mouseX ; _rect.x = _rect.x >= (_tempObj.width + _tempObj.x)?(_tempObj.width + _tempObj.x):_rect.x ; var h = mouseY > (h = (_bitmap.y + _bitmap.height))?h:mouseY ; h = h <= this._tempObj.y?_tempObj.y:h ; _rect.graphics.drawRect(0, 0, _tempObj.width + _tempObj.x - _rect.x,h-_tempObj.y); } } private function rightDownDragHandler():void { if (this._cutScale != 0) { this.downDragHandler(); }else { var w = mouseY > (w = (_bitmap.y + _bitmap.height))?w:mouseY ; w = w <= this._tempObj.y?_tempObj.y:w ; var h = mouseX > (h = (_bitmap.x + _bitmap.width))?h:mouseX ; h = h <= this._tempObj.x?_tempObj.x:h _rect.graphics.drawRect(0, 0, h -this._tempObj.x, w-_tempObj.y); } } private function rightUpDragHandler():void { if (this._cutScale != 0) { this.rightDragHandler(); }else { _rect.y = (mouseY < _bitmap.y)?_bitmap.y:mouseY; _rect.y = _rect.y >=(_tempObj.height + _tempObj.y)?(_tempObj.height + _tempObj.y): _rect.y ; var h = mouseX > (h = (_bitmap.x + _bitmap.width))?h:mouseX ; h = h <= this._tempObj.x?_tempObj.x:h _rect.graphics.drawRect(0, 0, h -this._tempObj.x,_tempObj.height+_tempObj.y-_rect.y); } } /** * 重新设置光标的位置,形状 。 * @param p */ private function resetCursorPosition(cursor:uint):void { /*移动状态*/ if (cursor == Constant.CURSOR_MOVE) { this._cursor1.x = mouseX ; this._cursor1.y = mouseY ; this._cursor1.visible = true ; return ; } this._cursor2.x = mouseX ; this._cursor2.y = mouseY ; this._cursor2.visible = true ; switch(cursor) { case Constant.CURSOR_HORIZONTAL: _cursor2.rotation = 0 ; break ; case Constant.CURSOR_VERTICAL: _cursor2.rotation = 90 ; break ; case Constant.CURSOR_DIAGONAL_LEFT: _cursor2.rotation = 45 ; break ; case Constant.CURSOR_DIAGONAL_RIGHT: _cursor2.rotation = 135 ; break ; } } /** * 拖动 裁剪区域 */ private function dragHandler():void { //裁剪区域移动范围,不能超过原图区域。 var _rectangle:Rectangle = new Rectangle(); _rectangle.x = _bitmap.x ; _rectangle.y = _bitmap.y ; _rectangle.width = this._bitmap.width-_rect.width ; _rectangle.height = this._bitmap.height-_rect.height ; _rect.startDrag(false, _rectangle); } /** * 检查光标是否在左边界上 * @return boolean */ private function isLeftEdge():Boolean { /**检查左边界*/ if (mouseX>=(this._rect.x - Constant.ACTIVERANGE) && mouseX<=(this._rect.x + Constant.ACTIVERANGE) && mouseY>this._rect.y && mouseY<(this._rect.y +this._rect.height)) { return true ; } return false ; } /** * 检查光标是否在右边界上 */ private function isRightEdge():Boolean { if (mouseX>=(this._rect.x + this._rect.width - Constant.ACTIVERANGE) && mouseX<=(this._rect.x+this._rect.width + Constant.ACTIVERANGE) & mouseY>this._rect.y&& mouseY<(this._rect.y+this._rect.height)) { return true ; } return false ; } /** * 检查光标是否在上边界上 。 * @return boolean . */ private function isUpEdge():Boolean { if (mouseX > this._rect.x && mouseX < (this._rect.x + _rect.width) & mouseY > (_rect.y - Constant.ACTIVERANGE) && mouseY<(_rect.y+Constant.ACTIVERANGE)) { return true ; } return false ; } /** * 检查光标是否在下边界上 。 * @return boolean . */ private function isDownEdge():Boolean { if (mouseX > this._rect.x && mouseX < (this._rect.x + _rect.width) & mouseY > (_rect.y + _rect.height - Constant.ACTIVERANGE) && mouseY<(_rect.y+_rect.height+Constant.ACTIVERANGE)) { return true ; } return false ; } /** * 检查光标是否在左上角 。 * @return boolean . */ private function isLeftUpAngle():Boolean { if (mouseX > (this._rect.x-Constant.ACTIVERANGE) && mouseX < (this._rect.x +Constant.ACTIVERANGE) & mouseY > (_rect.y - Constant.ACTIVERANGE) && mouseY<(_rect.y+Constant.ACTIVERANGE)) { return true ; } return false ; } /** * 检查光标是否在左下角 。 * @return boolean . */ private function isLeftDownAngle():Boolean { if (mouseX > (this._rect.x-Constant.ACTIVERANGE) && mouseX < (this._rect.x +Constant.ACTIVERANGE) & mouseY > (_rect.y+_rect.height - Constant.ACTIVERANGE) && mouseY<(_rect.y+_rect.height+Constant.ACTIVERANGE)) { return true ; } return false ; } /** * 检查光标是否在右上角 。 * @return boolean . */ private function isRightUpAngle():Boolean { if (mouseX > (this._rect.x + _rect.width - Constant.ACTIVERANGE) && mouseX < (this._rect.x+_rect.width +Constant.ACTIVERANGE) & mouseY > (_rect.y- Constant.ACTIVERANGE) && mouseY<(_rect.y+Constant.ACTIVERANGE)) { return true ; } return false ; } /** * 检查光标是否在右下角 。 * @return boolean . */ private function isRightDownAngle():Boolean { if (mouseX > (this._rect.x + _rect.width - Constant.ACTIVERANGE) && mouseX < (this._rect.x+_rect.width +Constant.ACTIVERANGE) & mouseY > (_rect.y+_rect.height- Constant.ACTIVERANGE) && mouseY<(_rect.y+_rect.height+Constant.ACTIVERANGE)) { return true ; } return false ; } /** * 判断光标标是否在裁剪区域里面 * @return boolean */ private function isInCutRect():Boolean { if (mouseX>(this._rect.x + Constant.ACTIVERANGE) && mouseX<(this._rect.x + this._rect.width - Constant.ACTIVERANGE) & mouseY>(this._rect.y + Constant.ACTIVERANGE) && mouseY<(this._rect.y + this._rect.height -Constant.ACTIVERANGE)) { return true ; } return false ; } } }
代码三:
package com.wdxc { import flash.display.MovieClip; import flash.display.Sprite import flash.display.* ; import flash.events.MouseEvent ; import flash.geom.Matrix ; /** * 图片旋转 * @author luqinglong */ public class ImageRotation extends Sprite{ private var _bitmap:Bitmap ; private var _x:Number ; private var _y:Number ; private var _w:Number ; private var _h:Number ; //private var _angle:Number ; public function ImageRotation() { _bitmap = Main.SCENE.bitmap ; _x = _bitmap.x ; _y = _bitmap.y ; _w = _bitmap.width ; _h = _bitmap.height ; cancle_btn.addEventListener(MouseEvent.CLICK, cancleHandler); left_btn.addEventListener(MouseEvent.CLICK, leftRotationHandler); right_btn.addEventListener(MouseEvent.CLICK, rightRotationHandler) ; save_btn.addEventListener(MouseEvent.CLICK, applicationHandler); //单击了旋转 Main._handerState = 10 ; //go_cut_btn.addEventListener(MouseEvent.CLICK, goCutMenu); } /* private function goCutMenu(e:MouseEvent):void { this.remove(); Main.SCENE._imageCut.cutpicing(); }*/ /** * 取消操作 * @param e */ private function cancleHandler(e:MouseEvent):void { //Main.SCENE.reset() ; Main._handerState = 0 ; this.remove(); } public function remove():void { Main.SCENE.bitmap.x = _x ; Main.SCENE.bitmap.y = _y ; Main.SCENE.bitmap.rotation = 0 ; left_btn.removeEventListener(MouseEvent.CLICK, leftRotationHandler); cancle_btn.removeEventListener(MouseEvent.CLICK, cancleHandler) ; save_btn.removeEventListener(MouseEvent.CLICK, applicationHandler); //go_cut_btn.removeEventListener(MouseEvent.CLICK, goCutMenu); this.stage.removeChild(this) ; } private function applicationHandler(e:MouseEvent):void { Main.SCENE.addVagueBg(); Main.SCENE.addLoadingBar(); var server :ImageService = new ImageService(); server.photoid = Main.SCENE.photoid ; server.submitRotation(fixAng( _bitmap.rotation) ); left_btn.removeEventListener(MouseEvent.CLICK, leftRotationHandler); cancle_btn.removeEventListener(MouseEvent.CLICK, cancleHandler) ; save_btn.addEventListener(MouseEvent.CLICK, applicationHandler); this.stage.removeChild(this) ; Main._handerState = 2 ; } /** * 左旋转处理 * @param e */ private function rightRotationHandler(e:MouseEvent):void { var _angle = fixAng(_bitmap.rotation) ; if (_angle== 0) { _bitmap.x = _x + (_h + _w) / 2 ; _bitmap.y = _y+(_h - _w) / 2 ; } else if (_angle == 90) { _bitmap.x = _x + _w ; _bitmap.y = _y + _h ; } else if (_angle== 180) { _bitmap.x = _x - (_h - _w) / 2; _bitmap.y = _y + (_w+_h)/2 ; } else if (_angle == 270) { _bitmap.x = _x ; _bitmap.y = _y; } _bitmap.rotation += 90 ; _angle = fixAng( _bitmap.rotation) ; trace("angle="+_angle); } private function leftRotationHandler(e:MouseEvent):void { var _angle = fixAng( _bitmap.rotation) ; if (_angle == 0) { _bitmap.x = _x - (_h - _w) / 2; _bitmap.y = _y + (_w+_h)/2 ; } else if (_angle == 270) { _bitmap.x = _x + _w ; _bitmap.y = _y + _h ; } else if (_angle == 180 ){ _bitmap.x = _x + (_h + _w) / 2 ; _bitmap.y = _y+(_h - _w) / 2 ; } else if (_angle == 90) { _bitmap.x = _x ; _bitmap.y = _y; } _bitmap.rotation -= 90 ; trace("angle="+_angle); } /** * 转换成标准角度 * @param _ang */ public static function fixAng (_ang:Number){ var _ang:Number = _ang % 360; var ang:Number = _ang % 360 < 0 ? _ang + 360 : _ang; return ang; } } }
代码四:
package com.wdxc { import com.wdxc.util.JPGEncoder; import flash.events.Event; import flash.net.URLLoader; import flash.net.URLRequest; import flash.net.URLRequestMethod ; import flash.net.URLVariables; /** * 与服务端通信 * @author luqinglong */ public class ImageService { private var url:String = "/photo/PhotoEditServlet" ; //private var url:Stri