flex 显示图片二进制流

Flex一般情况下是用Image控件通过source属性和load方法加载文件或URL图片,Image控件允许您在运行时导入 JPEGPNGGIF  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();

       privatevar _bonthebl:Boolean=true;//是否按照比例缩放全图显示。

       privatevar _bFillUp:Boolean = false;  ///否平铺

       publicfunction Image():void {}

       overrideprotectedfunction createChildren():void

       {

           addChild(_loader);

       }

      

       publicfunction loadBytes(bytes:ByteArray,context:LoaderContext=null):void

       {

           _loader.loadBytes(bytes, context);

           _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onBytesLoaded);

       }

       publicfunctionget bFillUp():Boolean{

           return _bFillUp;

       }

      

       publicfunctionset bFillUp(value:Boolean):void{

           this._bFillUp = value;

       }

       publicfunctionget bonthebl():Boolean{

           return _bonthebl;

       }

      

       publicfunctionset bonthebl(value:Boolean):void{

           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 = newbyte[2048];

           int temp;

           if (is == null)

              returnnull;

           temp = is.read(buffer);

           while (temp != -1) {

              bytestream.write(buffer, 0, temp);

              temp = is.read(buffer);

           }

           // ByteArrayOutputStream转换为二进制数组

           b = bytestream.toByteArray();

           is.close();

 

       } catch (Exception e) {

           e.printStackTrace();

       }

       return b;

    }

 

 

    public InputStream getStreamFromByte(byte img[]) {

       returnnew ByteArrayInputStream(img);

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值