flex图片剪切示例--预览、保存到本地、保存到服务器(附源码)
flex图片剪切示例--预览、保存到本地、保存到服务器(附源码)
图片剪切功能:
效果图: flex代码: ?
<?
xml?version="1.0"?encoding="utf-8"
?>
< mx:Application? xmlns:mx ="http://www.adobe.com/2006/mxml" ?layout ="absolute" ?initialize ="init()" ?xmlns:local ="astion.*" > ? mx:Script ?? <![CDATA[ ???import?mx.controls.Image; ???import?mx.graphics.ImageSnapshot; ???import?flash.net.FileReference; ???import?mx.graphics.codec.JPEGEncoder; ???import?mx.managers.PopUpManager; ???import?mx.containers.TitleWindow; ???import?mx.controls.Alert; ???import?mx.events.CloseEvent; ???import?mx.core.IFlexDisplayObject; ???import?mx.utils.*; ???import?mx.core.Application; ???import?astion.Dot; ???import?astion.ScaleBox; ??? ???public?static?const?LINE_WIDTH:Number?=?1;//缩放边框宽度 ???private?var?file:FileReference; ???public?var?IMAGE_URL:String="http://localhost:8080/cutPicuter/aa/aa.jpg"; ???private?var?loader:Loader; ???private?var?bmp:Bitmap; ????????????private?var?stream:URLStream; ????????????public?var?realPath:String="D:myWorkSpacecutPicuterWebRootaaaa.jpg"; ??? ???//初始化数据 ???private?function?init():void{ ????this.loader?=?new?Loader(); ????????????????this.stream?=?new?URLStream(); ????????????????this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.onComplete); ????????????????this.loader.load(new?URLRequest(encodeURI(this.IMAGE_URL)));//解决中文乱码 ????????????????this.stream.load(new?URLRequest(encodeURI(this.IMAGE_URL))); ????????????????this.stream.addEventListener(Event.COMPLETE,this.onLoaded); ???} ???private?function?onLoaded(e:Event):void ????????????{???????????????????????????????? ????????????????var?bytearray:ByteArray?=?new?ByteArray();???? ????????????????this.stream.readBytes(bytearray); ???????????????? ????????????????if(this.stream.connected) ????????????????????this.stream.close(); ???????????????????? ????????????????this.loader.loadBytes(bytearray); ????????????} ????????????private?function?onComplete(e:Event):void ????????????{ ????????????????try ????????????????{ ????????????????????this.bmp?=?this.loader.content?as?Bitmap; ????????????????????var?showImage:Image=?new?Image(); ????????????????????showImage.source=this.loader.content; ????????????????????canvas.addChild(showImage); ????????????????????canvas.setChildIndex(box,1); ????????????????????canvas.setChildIndex(showImage,0); ????????????????} ????????????????catch(e:Error) ????????????????{ ???????????????????? ????????????????} ????????????} ??? ???//截图,显示缩放选择框 ???private?function?doCapture():void{ ????box.x?=?100; ????box.y?=?100; ????box.visible?=?true; ???} ??? ???//获取缩放选择框内的图像 ???private?function?getImg():BitmapData{ ????//截取整个区域 ????box.scaleEnable?=?false; ????var?bmp:BitmapData?=?ImageSnapshot.captureBitmapData(canvas); ????box.scaleEnable?=?true; ???? ????//矩形为要截取区域???????????????? ????????????????var?re:Rectangle?=?new?Rectangle(box.x+LINE_WIDTH,box.y+LINE_WIDTH,box.boxWidth-LINE_WIDTH,box.boxHeight-LINE_WIDTH);? ????????????????var?bytearray:ByteArray?=?new?ByteArray();??? ????????????????//截取出所选区域的像素集合???????????????????????? ????????????????bytearray?=?bmp.getPixels(re);? ???????????????? ???????????????? ????????????????var?imgBD:BitmapData?=?new?BitmapData(box.boxWidth-LINE_WIDTH,box.boxHeight-LINE_WIDTH);??????? ????????????????//当前的bytearray.position为最大长度,要设为从0开始读取??????? ????????????????bytearray.position=0;???????????? ????????????????var?fillre:Rectangle?=?new?Rectangle(0,box.boxHeight-LINE_WIDTH); ????????????????//将截取出的像素集合存在新的bitmapdata里,大小和截取区域一样 ????????????????imgBD.setPixels(fillre,bytearray); ???????????????? ????????????????return?imgBD; ???} ??? ???//预览图片 ???private?function?doScan():void{ ????var?t:TitleWindow?=?new?TitleWindow(); ????t.showCloseButton=true; ????t.addEventListener(CloseEvent.CLOSE,closeWindow); ????t.width?=?box.boxWidth+t.getStyle("borderThickness"); ????t.height?=box.boxHeight+t.getStyle("borderThickness")+t.getStyle("headerHeight"); ????var?img:Image?=?new?Image(); ????img.width?=?box.boxWidth; ????img.height?=?box.boxHeight;? ????img.source?=?new?Bitmap(getImg()); ????t.addChild(img); ????PopUpManager.addPopUp(t,this,true); ????PopUpManager.centerPopUp(t); ???} ??? ???private?function?closeWindow(e:CloseEvent):void{???????????? ????????????????var?t:TitleWindow?=?e.currentTarget?as?TitleWindow;???????????????????? ????????????????PopUpManager.removePopUp(t);???????????????? ????????????} ???????????? ????????????//保存图片到本地 ???private?function?downloadPicture():void{ ????file=new?FileReference(); ????file.addEventListener(Event.COMPLETE,downloadComplete); ????file.save(new?JPEGEncoder(80).encode(getImg()),"default.jpg"); ???} ??? ???private?function?downloadComplete(event:Event):void{ ????Alert.show("成功保存图片到本地!","提示"); ???} ??? ???//保存图片到服务器即覆盖原来的图片 ???private?function?save():void{ ????Alert.show("是否保存剪切图片?","提示",3,?this,?function(event:CloseEvent):void?{ ??????????if?(event.detail==Alert.YES){ ???????????var?request:URLRequest?=?new?URLRequest("http://localhost:8080/cutPicuter/servlet/FileManagerSaveFileServlet?realPath="+encodeURIComponent(StringUtil.trim(realPath))); ?????request.method=URLRequestMethod.POST; ?????request.contentType?=?"application/octet-stream"; ?????request.data?=?new?JPEGEncoder(80).encode(getImg()); ?????var?loader:URLLoader?=?new?URLLoader(); ?????loader.load(request); ?????loader.addEventListener(Event.COMPLETE,saveResult); ??????????}}); ???} ??? ???private?function?saveResult(event:Event):void{ ????Application.application.reLoadFolderFiles(realPath.substr(0,realPath.lastIndexOf(""))); ????Alert.show("保存剪切图片成功","提示"); ???} ?? ]]> </ mx:HBox? x ="0" ?y ="0" ???????? mx:LinkButton? label ="剪裁" ?click ="doCapture();" ?icon ="@Embed('assets/cut.png')" /> ="预览" ="doScan();" ="@Embed('assets/ok.png')" mx:VRule? height ="22" ="保存" ??click ="save()" ??icon ="@Embed('assets/save.png')" ="另存为" ="downloadPicture();" ="@Embed('assets/saveAs.png')" ???? mx:HBox mx:Canvas? id ="canvas" ="23" ?x ="1" local:ScaleBox? ="box" ?visible ="false" ?width ="100" ?height ="100" mx:Canvas mx:Application > package
?com; ? 源码:?flex图片剪切示例 转载:http://www.blogjava.net/obpm/archive/2010/09/01/330501.html (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |