如何将 bytearray 转换为 image 或 image 转换为 bytearray ?
Posted
技术标签:
【中文标题】如何将 bytearray 转换为 image 或 image 转换为 bytearray ?【英文标题】:How to convert bytearray to image or image to bytearray ? 【发布时间】:2010-10-28 18:04:02 【问题描述】:如何将 bytearray 值分配给面板背景图像。如果有人有想法或经验请帮助我克服这个问题。简要经验:
我有面板控制并想加载从 web 服务获取的图像作为背景图像。所以我使用了 setstyle() 但它不接受该图像。那么如何将该图像添加到我的面板背景图像中。请在此处告诉我您的想法。
【问题讨论】:
更多信息会有所帮助。 【参考方案1】:对于 AS3,您可以使用 adobe corelib。检查本教程... http://ntt.cc/2009/01/09/as3corelib-tutorialhow-to-use-jpegencoder-and-pngencoder-class-in-flex.html
【讨论】:
刚刚将链接更改为类似教程,但请检查有关不使用 Alex Rios 发布的外部库的其他答案。【参考方案2】:嗯,我想,因为它是一个图像,你有它在一个 BitmapData,让我们说“myBmp”...... 然后使用以下方法从 BitmapData 中提取所有数据:
var bytes:ByteArray = myBmp.getPixels(myBmp.rect);
还有以下要写的:
myBmp.setPixels(myBmp.rect, bytes);
注意,ByteArray 中只存储原始的 32 位像素数据,没有压缩,也没有原始图像的尺寸。
对于压缩,你应该参考corelib,正如ozke所说的......
【讨论】:
【参考方案3】:使用 adobe 的 JPGEncoder (com.adobe.images.JPGEncoder) 类和 ByteArray 几乎是您所需要的。将图像转换为字节数组(假设 CAPS 是您需要填写的变量):
// -- first draw (copy) the image's bitmap data
var image:DisplayObject = YOUR_IMAGE;
var src:BitmapData = new BitmapData(image.width, image.height);
src.draw(image);
// -- encode the jpg
var quality:int = 75;
var jpg:JPGEncoder = new JPGEncoder(quality);
var byteArray:ByteArray = jpg.encode(src);
【讨论】:
【参考方案4】:我使用 flash.display.Loader() 将图像加载到数组中。它有一个 Complete 事件,在加载图像后触发。然后我将图像绘制到一个位图上,我将其设置为可以放置在面板中的图像数据。希望你能从这个好运开始。
public static function updateImage(img:Image, matrix:Matrix,
pageDTO:PageDTO, bitmapData:BitmapData):void
var loader:flash.display.Loader = new flash.display.Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function (e:Event) : void
bitmapData.draw(loader.content, matrix);
pageViewer.data = new Bitmap(bitmapData);
);
loader.loadBytes(pageDTO.thumbnail);
<mx:Panel>
<mx:Image id="pageViewer"/>
</mx:Panel>
【讨论】:
【参考方案5】:我遇到了同样的问题。作为一种解决方法,我创建了嵌套在主画布内的子画布,并将图像添加到子画布后面的主画布中。在子 Canvas 上绘制的任何内容都会出现在 Image 之上。
【讨论】:
【参考方案6】:只需使用 loadBytes 函数将其加载到 Loader 实例中即可。
var ldr:Loader = new Loader();
ldr.loadBytes(myByteArray);
addChild(ldr);
【讨论】:
【参考方案7】:在 Flex 3 或更高版本中,您只需:
yourImage.source = yourByteArray;
问候!
【讨论】:
+1 嘿。这看起来真的很整洁。为什么有人告诉我,要使用byteArray
作为图像的来源,我必须写img.load(myByteArray)
之类的东西?这有意义吗?
问题是关于背景图像样式,这不适用于 setStyle('backgroundImage',...)以上是关于如何将 bytearray 转换为 image 或 image 转换为 bytearray ?的主要内容,如果未能解决你的问题,请参考以下文章