[AS3]BitmapBytes类-实现BitmapData与ByteArray(Base64)之间互转功能

转载:http://www.klstudio.com/post/145.html

[AS3]BitmapBytes类-实现BitmapData与ByteArray(Base64)之间互转功能


【组件版本】:0.5

【功能简述】:实现BitmapData与ByteArray(Base64)之间互转功能。

【运行平台】:Flash CS3,ActionScript3,Flash Player 9 +

【类包路径】: com.klstudio.images.BitmapBytes

【方法说明】:

  1. /** 
  2. * @link http://www.klstudio.com 
  3. * @author Kinglong 
  4. * @version 0.5 
  5. */  
  6.   
  7. package com.klstudio.images {  
  8.   
  9.     import flash.display.BitmapData;  
  10.     import flash.geom.Rectangle;  
  11.     import flash.utils.ByteArray;  
  12.       
  13.     import com.klstudio.util.Base64;  
  14.       
  15.     public class BitmapBytes {  
  16.           
  17.         public static function encodeByteArray(data:BitmapData):ByteArray{  
  18.             if(data == null){  
  19.                 throw new Error("data参数不能为空!");  
  20.             }  
  21.             var bytes:ByteArray = data.getPixels(data.rect);  
  22.             bytes.writeShort(data.width);  
  23.             bytes.writeShort(data.height);  
  24.             bytes.writeBoolean(data.transparent);  
  25.             bytes.compress();  
  26.             return bytes;  
  27.         }  
  28.         public static function encodeBase64(data:BitmapData):String{  
  29.             return Base64.encodeByteArray(encodeByteArray(data));  
  30.         }  
  31.           
  32.         public static function decodeByteArray(bytes:ByteArray):BitmapData{  
  33.             if(bytes == null){  
  34.                 throw new Error("bytes参数不能为空!");  
  35.             }  
  36.             bytes.uncompress();  
  37.             if(bytes.length <  6){  
  38.                 throw new Error("bytes参数为无效值!");  
  39.             }             
  40.             bytes.position = bytes.length - 1;  
  41.             var transparent:Boolean = bytes.readBoolean();  
  42.             bytes.position = bytes.length - 3;  
  43.             var height:int = bytes.readShort();  
  44.             bytes.position = bytes.length - 5;  
  45.             var width:int = bytes.readShort();  
  46.             bytes.position = 0;  
  47.             var datas:ByteArray = new ByteArray();            
  48.             bytes.readBytes(datas,0,bytes.length - 5);  
  49.             var bmp:BitmapData = new BitmapData(width,height,transparent,0);  
  50.             bmp.setPixels(new Rectangle(0,0,width,height),datas);  
  51.             return bmp;  
  52.         }  
  53.           
  54.         public static function decodeBase64(data:String):BitmapData{              
  55.             return decodeByteArray(Base64.decodeToByteArray(data));  
  56.         }         
  57.           
  58.         public function BitmapBytes() {  
  59.             throw new Error("BitmapBytes类只是一个静态类!");  
  60.         }  
  61.           
  62.     }  
  63.       


打包文件】: http://www.klstudio.com/demo/mxp/BitmapBytes.mxp



P.S. 此转换类是有很多实用价值,我可以简单一例:你可以把常用的位图进行通过encodeBase64方法生成Base64编码字符串,作为程序的常量存储下 来,如果使用的话,只需将字符串常量再通过decodeBase64还原成BitmapData位图对象就可以了!此图片存储数据的方式要比我以前写的“
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值