Flex中利用ByteArray与BitmapData互相转换实现图片的二进制保存与复原

本文介绍了一个名为BitmapBytes的类,它提供了将UIComponent组件转换为ByteArray数组的方法,并且能将ByteArray转换回Bitmap。通过实例演示了如何在MXML文件中实现这一功能,包括创建UI组件、调用转换方法以及显示转换后的图像。此技术适用于需要在代码中灵活处理图像资源的应用场景。
[b]转[/b][url] http://hjy2099.iteye.com/blog/260329[/url]


package com.kyit.lg..util
{ /*
author:李广业
*/
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.utils.ByteArray;
import mx.core.UIComponent;
public class BitmapBytes
{
public function BitmapBytes()
{
super();
}
//将可视的UIComponent组件转换为ByteArray数组,我就是在UIComponent那里放了一个图片
public static function BitmapDataToByteArray(target : UIComponent):ByteArray{
var imageWidth:uint = target.width;
var imageHeight:uint = target.height;
var srcBmp:BitmapData = new BitmapData( imageWidth, imageHeight );
//将组件读取为BitmapData对象,bitmagData的数据源
srcBmp.draw( target );
//getPixels方法用于读取指定像素区域生成一个ByteArray,Rectangle是一个区域框,就是起始坐标
var pixels:ByteArray = srcBmp.getPixels( new Rectangle(0,0,imageWidth,imageHeight) );
//下面俩行将数据源的高和宽一起存储到数组中,为翻转的时候提供高度和宽度
pixels.writeShort(imageHeight);
pixels.writeShort(imageWidth);
return pixels;
}
//次方法的参数必须是像上面的ByteArray形式一样的,即需要对象的大小;
//此方法返回的Bitmap可以直接赋值给Image的source属性
public static function ByteArrayToBitmap(byArr:ByteArray):Bitmap{
if(byArr==null){
return null;
}
//读取出存入时图片的高和宽,因为是最后存入的数据,所以需要到尾部读取
var bmd:ByteArray= byArr;
bmd.position=bmd.length-2;
var imageWidth:int = bmd.readShort();
bmd.position=bmd.length-4;
var imageHeight:int= bmd.readShort();
var copyBmp:BitmapData = new BitmapData( imageWidth, imageHeight, true );
//利用setPixel方法给图片中的每一个像素赋值,做逆操作
//ByteArray数组从零开始存储一直到最后都是图片数据,因为读入时的高和宽都是一样的,所以当循环结束是正好读完
bmd.position = 0;
for( var i:uint=0; i<imageHeight ; i++ )
{
for( var j:uint=0; j<imageWidth; j++ )
{
copyBmp.setPixel( j, i, bmd.readUnsignedInt() );
}
}
var bmp:Bitmap = new Bitmap( copyBmp );
return bmp;
}
}
}
以下是mxml文件,只是我做测试用的没有进行序列化,有需要的留言给我,
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import com.kyit.lg.util .BitmapBytes;
private var byteArr:ByteArray;
internal function copyToTar():void{
//这俩个方法都是静态的,因为项目当中常用
byteArr=BitmapBytes.BitmapDataToByteArray(sorImg);
tarImg.source=BitmapBytes.ByteArrayToBitmap(byteArr);
}

]]>
</mx:Script>
<mx:Image x="33" y="56" id="sorImg" source="a.jpg"/>
<mx:Image x="33" y="265" id="tarImg" />
<mx:Button x="33" y="26" label="a1" click="copyToTar();" />
</mx:WindowedApplication>
为避免将 `Bitmap` 转换为 `ByteArray` 时出现内存溢出问题,可以从以下几个方面对代码进行优化: ### 缩放位图 在将位图转换为字节数组之前,通过缩放位图来减少其占用的内存。可以使用 `Bitmap.createScaledBitmap` 方法对 `Bitmap` 进行缩放: ```kotlin import android.graphics.Bitmap fun scaleBitmap(bitmap: Bitmap, maxWidth: Int, maxHeight: Int): Bitmap { val width = bitmap.width val height = bitmap.height var newWidth = width var newHeight = height if (width > maxWidth) { newWidth = maxWidth newHeight = (newWidth * height / width) } if (newHeight > maxHeight) { newHeight = maxHeight newWidth = (newHeight * width / height) } return Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true) } ``` 在 `bitmapToByteArray` 方法中使用缩放后的位图: ```kotlin import android.graphics.Bitmap import java.io.ByteArrayOutputStream const val BITMAP_COMPRESS_QUALITY = 100 fun bitmapToByteArray(bitmap: Bitmap): ByteArray { val scaledBitmap = scaleBitmap(bitmap, 800, 600) // 示例最大尺寸 val outputStream = ByteArrayOutputStream() scaledBitmap.compress(Bitmap.CompressFormat.PNG, BITMAP_COMPRESS_QUALITY, outputStream) return outputStream.toByteArray() } ``` ### 选择合适的压缩格式和质量 不同的压缩格式和质量会影响压缩后的数据量。例如,JPEG 格式通常比 PNG 格式占用更少的空间。可以尝试使用 JPEG 格式并调整压缩质量: ```kotlin fun bitmapToByteArray(bitmap: Bitmap): ByteArray { val outputStream = ByteArrayOutputStream() bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outputStream) // 使用 JPEG 格式,质量为 80 return outputStream.toByteArray() } ``` ### 及时回收资源 在使用完 `Bitmap` 和 `ByteArrayOutputStream` 后,及时调用 `recycle()` 方法释放 `Bitmap` 占用的内存,并关闭 `ByteArrayOutputStream`: ```kotlin import java.io.IOException fun bitmapToByteArray(bitmap: Bitmap): ByteArray { val outputStream = ByteArrayOutputStream() try { bitmap.compress(Bitmap.CompressFormat.PNG, BITMAP_COMPRESS_QUALITY, outputStream) return outputStream.toByteArray() } finally { bitmap.recycle() try { outputStream.close() } catch (e: IOException) { e.printStackTrace() } } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值