BitmapData 与 ByteArray 互相转换
2009-11-09 10:56
323 查看
package com.klstudio.images
{
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.utils.ByteArray;
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类只是一个静态类!");
}
}
}
//注释的部分是在我这里编译没有通过,Base64 不知是哪里的一个对象.原创也没有贴出来,希望有知道的朋友贴在下面.
{
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.utils.ByteArray;
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类只是一个静态类!");
}
}
}
//注释的部分是在我这里编译没有通过,Base64 不知是哪里的一个对象.原创也没有贴出来,希望有知道的朋友贴在下面.
相关文章推荐
- (转载)BitmapData与ByteArray的转换
- Android Bitmap 和 ByteArray的互相转换
- 将BitmapData数据转换为ByteArray, 保存ByteArray
- Flex中利用ByteArray与BitmapData互相转换实现图片的二进制保存与复原
- Flex中ByteArray与BitmapData互相转换实现图片的二进制保存与复原
- Flex中ByteArray与BitmapData互相转换实现图片的二进制保存与复原
- Flex中ByteArray与BitmapData互相转换实现图片的二进制保存与复原 .
- Flex中ByteArray与BitmapData互相转换实现图片的二进制保存与复原
- Flex中利用ByteArray与BitmapData互相转换实现图片的二进制保存与复原[转]
- Flex中利用ByteArray与BitmapData互相转换实现图片的二进制保存与复原
- Flex中利用ByteArray与BitmapData互相转换实现图片的二进制保存与复原
- Flex中利用ByteArray与BitmapData互相转换实现图片的二进制保存与复原
- Flex中利用ByteArray与BitmapData互相转换实现图片的二进制保存与复原
- Android Bitmap 和 ByteArray的互相转换
- flex Image,Bitmap, BitmapData、ByteArray相互关系和转换
- WPF Image控件中的BitmapSource与Bitmap的互相转换
- Bitmap和Drawable的互相转换
- Android中Bitmap和Drawable互相转换
- Wpf ImageSource对象与Bitmap对象的互相转换
- bytearray和bitmapdata之间的转换