如何将bytearray转换为图像或将图像转换为bytearray?

6

如何将bytearray值分配给面板背景图像。如果有人有想法或经验,请帮助我解决这个问题。简要说明:

我有一个面板控件,想要从Web服务获取图像作为背景图像加载。因此,我使用了setstyle(),但它不接受该图像。那么如何将该图像添加到我的面板背景图像中。请在此处告诉我您的想法。


更多的信息会很有帮助。 - C. Ross
7个回答

12
在Flex 3或更高版本中,您只需要执行以下操作:

 yourImage.source = yourByteArray;

敬礼!


嘿,那看起来真不错。为什么告诉我要像 img.load(myByteArray) 这样写来使用 byteArray 作为图像源呢?这有意义吗? - Felipe
问题是关于背景图片样式的,使用setStyle('backgroundImage',...)无效。 - César Alforde

4

嗯,我猜,既然它是一张图片,你应该已经将其存储在BitmapData中,假设为“myBmp”... 那么使用以下代码从BitmapData中提取所有数据:

var bytes:ByteArray = myBmp.getPixels(myBmp.rect);

以下是需要编写的内容:

myBmp.setPixels(myBmp.rect, bytes);

请注意,ByteArray 中仅存储原始的 32 位像素数据,没有压缩,也没有原始图像的尺寸信息。
如果需要压缩,请参考 corelib,正如 ozke 所说...

2

刚刚将链接更改为类似的教程,但请检查其他答案,不要使用像Alex Rios发布的外部库。 - ozke

1

使用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);

1
我使用了flash.display.Loader()来加载一个图像到数组中。它有一个Complete事件,在图像加载完成后触发。然后,我将图像绘制到位图上,并将其设置为可以放置在面板中的Image的数据。希望你能理解这个过程,祝你好运。
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>

0
只需使用loadBytes函数将其加载到Loader实例中即可。
var ldr:Loader = new Loader(); 
ldr.loadBytes(myByteArray); 
addChild(ldr); 

0

我曾经遇到过同样的问题。为了解决这个问题,我创建了一个子画布嵌套在主画布中,并在主画布后面添加了一个图像。在子画布上绘制的任何内容都会出现在图像的顶部。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接