今天做了丶实验,回来整理了下。还有些问题没处理好。没有处理图片原始很小导入进来,会放大的问题。没有处理,裁剪图片裁剪到很小的时候放大的问题。事件没有处理好。点击按钮的事件和拖动裁剪框的事件没处理好。
先记录下来,明天再来完善。
如果,用flash player,导入图片用FileReference,对其select进行侦听。然后load。对complete进行侦听。得到byteArray。用Loader的 loadBytes加载进来,得到Bitmap对象。然后进行大小缩变,位置处理,显示到舞台。
这个时候,就可以对Bitmap进行裁剪。这里用一个sprite的graphics的draw方法,来矩形。记录mouseDown前的stage.mouseX,stage.mouseY,然后在mouseUp的时候,进行图片裁切。因为对bitmap进行了多次缩放。
在计算Matrix的时候,通过原始的width和height乘以(矩形的宽高与bitmap的宽高的比)。处理完成后,显示到舞台,处理大小,坐标。更新原始的bitmap。
可以对处理进行重置处理。第一次导入图片的时候,byteArray还在,还可以再load。
也可以重置所有。清空byteArray,清空该清空的。然后再导入图片。再处理。
最后,觉得图片处理好了。可以点击save保存图片。目前,该程序存在问题。保存的时候,和mouseDown事件交集了。清空了bitmap。会报错。先睡觉觉了,明天处理吧。
关于,FileReference,如果定义到一个单独的函数内,然后select,然后load。load不出来,没有complete事件发生。难道这个就是它的作用域问题吗。将FileReference定义到函数外,整个类里就可以。
如果用AIR,又会是另一番景象了。
文档类:
package { import com.vini123.tool.Button; import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.JPEGEncoderOptions; import flash.display.Loader; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Matrix; import flash.geom.Point; import flash.net.FileFilter; import flash.net.FileReference; import flash.utils.ByteArray; public class ImageEditor extends Sprite { private var addBtn:Button; private var saveBtn:Button; private var resetBtn:Button; private var allResetBtn:Button; private var bitmap:Bitmap; private var ba:ByteArray; private var bitmapContainer:Sprite; private var rim:Sprite; private var tempBitmap:Bitmap; private var prevPos:Point; private var isDown:Boolean = false; private var _file:FileReference; private var state:String = "NONE"; [SWF(width = "720",height = "450",frameRate = "30")] public function ImageEditor() { stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; Init(); } private function Init():void { addBtn = new Button("添加图片"); saveBtn = new Button("保存图片"); resetBtn = new Button("重置图片"); allResetBtn = new Button("完全重置"); addBtn.name = "addBtn"; saveBtn.name = "saveBtn"; resetBtn.name = "resetBtn"; allResetBtn.name = "allResetBtn"; var tempArr:Array = [addBtn,saveBtn,resetBtn,allResetBtn]; for(var i:int = 0 ; i< tempArr.length ; i++) { tempArr[i].x = i * (tempArr[i].width +10) +10; tempArr[i].y = 10; tempArr[i].buttonMode = true; tempArr[i].addEventListener(MouseEvent.CLICK,clickHandler); addChild(tempArr[i]); } bitmapContainer = new Sprite(); addChild(bitmapContainer); rim = new Sprite(); addChild(rim); prevPos = new Point(); } private function clickHandler(e:MouseEvent):void { var tarName:String = e.currentTarget.name; switch(tarName) { case "addBtn": if(state == "NONE") { addImage(); state = "HAS"; } break; case "saveBtn": if(state == "HAS") { saveImage(); } break; case "resetBtn": if(state == "HAS") { dispose(); load(); } break; case "allResetBtn": if(state == "HAS") { dispose(); ba.clear(); state = "NONE"; } break; default: break; } } private function addImage():void { _file = new FileReference(); _file.browse([new FileFilter("Images('*.jpg;*.png;*.gif')","*.jpg;*.png;*.gif")]); _file.addEventListener(Event.SELECT,fileSelectHandler); _file.addEventListener(Event.OPEN,fileOpenHandler); _file.addEventListener(Event.COMPLETE,fileCompleteHandler); } private function saveImage():void { var tempBa:ByteArray = new ByteArray(); var jpegEncoder:JPEGEncoderOptions = new JPEGEncoderOptions(80); bitmap.bitmapData.encode(bitmap.bitmapData.rect,jpegEncoder,tempBa); _file.save(tempBa,"用我三生烟火,换你一世迷离.jpg"); } private function fileSelectHandler(e:Event):void { e.target.removeEventListener(Event.SELECT,fileSelectHandler); (e.target as FileReference).load(); } private function fileOpenHandler(e:Event):void { e.target.removeEventListener(Event.OPEN,fileOpenHandler); } private function fileCompleteHandler(e:Event):void { e.target.removeEventListener(Event.COMPLETE,fileCompleteHandler); ba = e.target.data; load(); } private function load():void { var loader:Loader = new Loader(); loader.loadBytes(ba); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadCompleteHandler); } private function loadCompleteHandler(e:Event):void { bitmap = e.target.content as Bitmap; setBitmapPos(bitmap); bitmapContainer.addChildAt(bitmap,0); bitmap.x = stage.stageWidth * 0.5 - bitmap.width * 0.5; bitmap.y = stage.stageHeight - bitmap.height; addBtn.mouseEnabled = false; InitEvent() } private function InitEvent():void { bitmapContainer.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler); bitmapContainer.name = "bitmapContainer"; stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler); } private function mouseDownHandler(e:MouseEvent):void { if(e.target.name != "bitmapContainer") { return; } isDown = true; prevPos.x = stage.mouseX; prevPos.y = stage.mouseY; stage.addEventListener(Event.ENTER_FRAME,enterHandler); } private function enterHandler(e:Event):void { rim.graphics.clear(); rim.graphics.lineStyle(1.2,0x0099ff); rim.graphics.beginFill(0x000000,0); rim.graphics.drawRect(prevPos.x,prevPos.y,(stage.mouseX - prevPos.x),(stage.mouseY - prevPos.y)); rim.graphics.endFill(); } private function mouseUpHandler(e:MouseEvent):void { if(!isDown) { return; } isDown = false; stage.removeEventListener(Event.ENTER_FRAME,enterHandler); var tempW:Number = bitmap.width/bitmap.scaleX; var tempH:Number = bitmap.height/bitmap.scaleY; var bitmapData:BitmapData = new BitmapData(tempW*(rim.width/bitmap.width),tempH * (rim.height/bitmap.height),true,0); var mx:Matrix = new Matrix(); mx.tx = - Math.min((prevPos.x - bitmap.x),(stage.mouseX - bitmap.x)) /bitmap.scaleX; mx.ty = - Math.min((prevPos.y - bitmap.y) ,(stage.mouseY - bitmap.y))/bitmap.scaleY ; bitmapData.draw(bitmap,mx); tempBitmap = new Bitmap(bitmapData); bitmapContainer.addChildAt(tempBitmap,1); setBitmapPos(tempBitmap); tempBitmap.y = stage.stageHeight - bitmap.height; tempBitmap.x = stage.stageWidth * 0.5 - tempBitmap.width * 0.5; bitmap.bitmapData.dispose(); bitmap = tempBitmap; rim.graphics.clear(); } private function setBitmapPos(bm:Bitmap):void { if(bm.width/bm.height > (stage.stageWidth/(stage.stageHeight -50))) { bm.width = stage.stageWidth; bm.scaleY = bm.scaleX; } else { bm.height = stage.stageHeight - 50; bm.scaleX = bm.scaleY; } } private function dispose():void { if(bitmap) { bitmapContainer.removeChild(bitmap); bitmap.bitmapData.dispose(); bitmap = null; } if(tempBitmap) { if(tempBitmap.parent) { tempBitmap.parent.removeChild(tempBitmap); } tempBitmap.bitmapData.dispose(); tempBitmap = null; } bitmapContainer.removeEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler); stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUpHandler); } } }
按钮类:
package com.vini123.tool { import flash.display.Sprite; import flash.events.MouseEvent; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; public class Button extends Sprite { private var _txtFormat:TextFormat; private var _txt:TextField private var _bottom:Sprite; private var _gap:int =7; public function Button(label:String):void { _txtFormat = new TextFormat(); _txtFormat.size = 13; _txtFormat.color = 0xADD0FC; _txt = new TextField(); _txt.text = label; _txt.autoSize = TextFieldAutoSize.LEFT; _txt.setTextFormat(_txtFormat); _txt.mouseEnabled = false; addChild(_txt); _bottom = new Sprite(); _bottom.graphics.lineStyle(1.0,0x2D72B5); _bottom.graphics.beginFill(0x0099ff,1); _bottom.graphics.drawRoundRect(0,0,_txt.textWidth + _gap *2,_txt.textHeight + _gap,5,5); _bottom.graphics.endFill(); addChildAt(_bottom,0); _txt.x = _gap; _txt.y = _gap * 0.5 -1; this.addEventListener(MouseEvent.ROLL_OVER,overHandler); this.addEventListener(MouseEvent.ROLL_OUT,outHandler); } private function overHandler(e:MouseEvent):void { _txt.alpha = 0.65; } private function outHandler(e:MouseEvent):void { _txt.alpha = 1; } public function dispose():void { _bottom.graphics.clear(); this.removeEventListener(MouseEvent.ROLL_OVER,overHandler); this.removeEventListener(MouseEvent.ROLL_OUT,outHandler); if(this.parent) { this.parent.removeChild(this); } } } }