bitmapData和ByteArray之间的装换

这是我工作中遇到的一个问题,才发现原来ByteArray和ByteMapData之间存在着这种转换关系,很感谢原创作者的共享。现在把它保存起来便于以后的回顾。 package com.klstudio.images {          import flash.display.BitmapData;       import flash.geom.Rectangle;       import flash.utils.ByteArray;              import com.klstudio.util.Base64;              public class BitmapBytes {                      public static function encodeByteArray(data:BitmapData):ByteArray{               if(data == null){                   throw new Error("data参数不能为空!");               }               var bytes:ByteArray = data.getPixels(data.rect);               bytes.writeShort(data.width);               bytes.writeShort(data.height);               bytes.writeBoolean(data.transparent);               bytes.compress();               return bytes;           }           public static function encodeBase64(data:BitmapData):String{               return Base64.encodeByteArray(encodeByteArray(data));           }                      public static function decodeByteArray(bytes:ByteArray):BitmapData{               if(bytes == null){                   throw new Error("bytes参数不能为空!");               }               bytes.uncompress();               if(bytes.length <  6){                   throw new Error("bytes参数为无效值!");               }                          bytes.position = bytes.length - 1;               var transparent:Boolean = bytes.readBoolean();               bytes.position = bytes.length - 3;               var height:int = bytes.readShort();               bytes.position = bytes.length - 5;               var width:int = bytes.readShort();               bytes.position = 0;               var datas:ByteArray = new ByteArray();                         bytes.readBytes(datas,0,bytes.length - 5);               var bmp:BitmapData = new BitmapData(width,height,transparent,0);               bmp.setPixels(new Rectangle(0,0,width,height),datas);               return bmp;           }                      public static function decodeBase64(data:String):BitmapData{                           return decodeByteArray(Base64.decodeToByteArray(data));           }                             public function BitmapBytes() {               throw new Error("BitmapBytes类只是一个静态类!");           }                  }          }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值