Get a ByteArray from BitmapData in AS3

若如初见. 提交于 2019-12-13 04:45:21

问题


I would like to get a ByteArray from a BitmapData in AS3. I tried the following:

var ba:ByteArray = myBitmapData.getPixels(0,0);

It didn't work and I got this error ArgumentError: Error #1063: Argument count mismatch on flash.display::BitmapData/getPixels(). Expected 1, got 2.:


回答1:


As Adobe said about BitmapData.getPixels : "Generates a byte array from a rectangular region of pixel data...", so your parameter should be a Rectangle object.

Take this example from Adobe.com :

import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.utils.ByteArray;

var bmd:BitmapData = new BitmapData(80, 40, true);
var seed:int = int(Math.random() * int.MAX_VALUE);
bmd.noise(seed);

var bounds:Rectangle = new Rectangle(0, 0, bmd.width, bmd.height);
var pixels:ByteArray = bmd.getPixels(bounds);



回答2:


As was mentioned in the other answer, getpixels expects a .rect meaning rectangular area of some displayObject (like Sprite, MovieClip, Shape or other already exisiting BitmapData.

getPixels(). Expected 1, got 2. is becuase it should have been:

var ba:ByteArray = myBitmapData.getPixels( myBitmapData.rect);

Study this code and see if it helps you :

import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.net.URLRequest;
import flash.display.BitmapData;

var ba:ByteArray = new ByteArray;

var picBMP:Bitmap; var picBMD:BitmapData; 
var pic_canvas:Sprite = new Sprite(); //container for image

var loader:Loader = new Loader(); //image loader

loader.load(new URLRequest("pic1.jpg")); //load some JPG/PNG/GIF/SWF
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, img_load_Complete);

function img_load_Complete(evt:Event):void
{
    picBMP = loader.content as Bitmap;
    pic_canvas.addChild(picBMP);

    stage.addChild(pic_canvas); //container now added to stage (screen)

    BMP_toBytes(); //do function to convert to bytes
}

function BMP_toBytes():void
{
    var PC = pic_canvas; //shortcut reference to pic_canvas (less typing)

    picBMD = new BitmapData(PC.width, PC.height, true, 0xFFFFFF);
    picBMD.draw(PC); //make bitmapdata snapshot of container

    ba = picBMD.getPixels(picBMD.rect);
    trace("BA length : " + ba.length); //check how many bytes in there

    ba.position = 0; //reset position to avoid "End of File error"

    bytes_toPixels(); //do function for bytes as pixels to some new container
}


function bytes_toPixels():void
{
    var new_BMP:Bitmap = new Bitmap; var new_BMD:BitmapData; 
    var new_canvas:Sprite = new Sprite(); //another new container for pixels

    ba.position = 0; //reset position to avoid "End of File error"

    //we can reuse picBMD W/H sizes since we have copy of same pixels
    new_BMD = new BitmapData(picBMD.width, picBMD.height, true, 0xFFFFFFFF);
    new_BMD.setPixels(new_BMD.rect, ba); //paste from BA bytes
    new_BMP.bitmapData = new_BMD; //update Bitmap to hold new data

    new_canvas.x = 150; new_canvas.y = 0; new_canvas.addChild(new_BMP);
    stage.addChild(new_canvas); //add to screen
}

Hope it helps..



来源:https://stackoverflow.com/questions/27444583/get-a-bytearray-from-bitmapdata-in-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!