flex 显示图片二进制流
Flex一般情况下是用Image控件通过source属性和load方法加载文件或URL图片,Image控件允许您在运行时导入?JPEG、PNG、GIF?和?SWF?文件。 注意:不支持bmp格式的文件,要想显示bmp,一般是将BMP图片转换成二进制,再转成BitmapData,最后把获取位图数据的Bitmap传给Image源,让Image控件显示出来。 Image组件的source属性是不支持二进制数据的,我们可以自己定义一个组件用于显示二进制图片数据流。网上也有很多相关的例子,但是有些是不能直接改变图片大小,直接根据二进制图片原始大小显示,我根据相关代码修改了一下,可以实现缩放比例显示,已经固定了图片大小。 代码: ? package?com.image { ????import?flash.display.Loader; ????import?flash.events.Event; ????import?flash.system.LoaderContext; ????import?flash.utils.ByteArray; ????import?mx.controls.Image; ????/** ?????*?用来加载二进制数据的图片类 ?????* */ ????publicclass?ByteArrayImage?extends?mx.controls.Image ????{ ???????privatevar?_loader:Loader =?new?Loader(); ???????var?_bonthebl:Boolean=true;//是否按照比例缩放全图显示。 ???????var?_bFillUp:Boolean =?false;??是/否平铺 ???????publicfunction?Image():void?{} ???????overrideprotectedfunction?createChildren():void ???????{ ???????????addChild(_loader); ???????} ?????? ???????function?loadBytes(bytes:ByteArray,context:LoaderContext=null):void ???????{ ???????????_loader.loadBytes(bytes,context); ???????????_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onBytesLoaded); ???????} ???????functionget?bFillUp():Boolean{ ???????????return?_bFillUp; ???????} ?????? ???????set?bFillUp(value:Boolean):void{ ???????????this._bFillUp = value; ???????} ???????get?bonthebl():Boolean{ ???????????return?_bonthebl; ???????} ?????? ???????set?bonthebl(value:Boolean):this._bonthebl = value; ???????} ???????privatefunction?onBytesLoaded(e:Event):void ???????{ ???????/*??this.width = e.target.width; ???????????this.height = e.target.height;*/ ???????????this.width=150; ???????????this.height=200; ???????????if(_bonthebl){ ??????????????var?ih:int=e.currentTarget.height; ??????????????var?iw:int=e.currentTarget.width; ??????????????var?blh:Number=this.height/ih; ??????????????var?blw:Number=this.width/iw; ??????????????if(blh>1&&blw>1){ ??????????????????_loader.width=e.currentTarget.width; ??????????????????_loader.height=e.currentTarget.height; ??????????????} ??????????????elseif(blh>1||blw>1){ ??????????????????if(blh<1){ ?????????????????????_loader.width = Math.round(iw*blh); ?????????????????????_loader.height =?this.height; ??????????????????}else{ ?????????????????????_loader.width =?this.width; ?????????????????????_loader.height = Math.round(ih*blw); ??????????????????} ??????????????}else{ ??????????????????if(blh>blw){ ?????????????????????_loader.width = Math.round(iw*blw); ?????????????????????_loader.height = Math.round(ih*blw); ??????????????????} ??????????????????else{ ?????????????????????_loader.width = Math.round(iw*blh); ?????????????????????_loader.height = Math.round(ih*blh); ??????????????????} ??????????????} ??????????????this.width=_loader.width; ??????????????this.height=_loader.height; ???????????} ???????????if(_bFillUp) ???????????{ ??????????????_loader.width =?this.width; ??????????????_loader.height =?this.height; ???????????}? ???????} ????} ? } 页面调用的部分代码: ???????? <img:ByteArrayImage id="img"? /> ???????img.loadBytes(“二进制数据流”); ?后台相关代码省略。 一般情况下,数据库的blob字段映射为java.sql.blob类型。所以在后台将blob转换为byte数据类型,转换的相关代码 /*** 用于将从数据库中读取的二进制文件流转换为byte数组 ?????*/ ????publicbyte[] getByteFromStream(InputStream is) { ???????byte[] b =?newbyte[1]; ???????try?{ ???????????ByteArrayOutputStream bytestream =?new?ByteArrayOutputStream(); ???????????//?创建数据读取缓存byte数组 ???????????byte[] buffer =?byte[2048]; ???????????int?temp; ???????????if?(is ==?null) ??????????????returnnull; ???????????temp = is.read(buffer); ???????????while?(temp != -1) { ??????????????bytestream.write(buffer,temp); ??????????????temp = is.read(buffer); ???????????} ???????????将ByteArrayOutputStream转换为二进制数组 ???????????b = bytestream.toByteArray(); ???????????is.close(); ? ???????}?catch?(Exception e) { ???????????e.printStackTrace(); ???????} ???????return?b; ????} ? ? ????public?InputStream getStreamFromByte(byte?img[]) { ???????new?ByteArrayInputStream(img); ????} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |