开发者

Taking a snapshot of a loader

I'm attempting to take an image from a networked camera and store it as a bitmap. I discovered that flash didn't support MJPEG so I found a class someone had written that could talk to the camera and output on the screen. Then I edited it so that I could request the current image at any time and store it in a ByteArray. I've managed to load that ByteArray with a loader cl开发者_开发问答ass and place it on the stage, but I'm not sure how to save that as a Bitmap? Thanks

Edit: Sorry to be clear I want to store it inside to FLA so that I can carry on manipulating it


By "store it in the FLA", if you mean you want to extract that bitmap image from the Loader so you can access the bitmap data etc then here we go:

var myLoader:Loader = new Loader();

myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
myLoader.load(new URLRequest("http://somewhere.com/picture.jpg"));

private function onLoaded(e:Event):void
{
   try{
      //We do this in a try/catch statement just in case we accidentally load a SWF and try to cast it as a bitmap or something of this nature.
      var myBitmap:Bitmap = LoaderInfo(e.currentTarget).content as Bitmap;
   }catch (e:Error){
      //Do something about the error
   }       
}

VERY IMPORTANT INFORMATION HERE:

If you want to save the image to the disk or anywhere else after loading it with the loader object, BEWARE that by accessing the binary data via LoaderInfo().bytes will give you CORRUPTED DATA WHEN YOU SAVE. This is because the flash VM actually slightly modifies the binary data to display it graphically within the loader object. If you want to load and show your image + be able to save it you need to load the raw binary data using a plain URLLoader object and casting the contents of URLLoader.data as a byte array, then use Loader.loadBytes(bytes, LoaderContext), while setting allowLoadBytesCodeExecution inside the LoaderContext object supplied to the second argument to TRUE (only if you are loading a swf from a bytearray). Hope this helps.


If your app is online, you would need a server side script to create and save the image.

Basically the code could be as follows:

var request:URLRequest = new URLRequest( mailLoc );
request.contentType = 'application/octet-stream';
request.method = URLRequestMethod.POST;
request.data = bArray;

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, MailCompleteHandler);
loader.addEventListener( IOErrorEvent.IO_ERROR, _onImageError );
try
{
    loader.load(request);
}
catch(error:Error)
{
    trace("Unable to load URL");
}


private function _onImageError(e:IOErrorEvent):void {
    trace("IOErrorEvent: ",e.type," : ",e.text) 
}

On the server side, php code:

//bindary data.
$image_bytes = $GLOBALS["HTTP_RAW_POST_DATA"];
//change to whatever works for you
$file_name = "testfile.jpg";
$file_path = "../uploads/$file_name";

$file = fopen( $file_path, 'w+' );

if ( !fwrite( $file, $image_bytes ) ) {
    return "Error writing to file: $file";
}

fclose( $file );

You can check read this to get more info:

  • Save php
  • Saving flash graphics

[EDIT] Capturing the bitmapdata:

//loader should be your loader instance displaying your graphic.
var snapshot:BitmapData = capture(loader);
var result:Bitmap = new Bitmap( snapshoot );

public function capture( target:DisplayObject ):BitmapData {
    var rect:Object = target.getBounds( target );
    var width:Number = rect.width;
    var height:Number = rect.height;
    var bitmap:BitmapData = new BitmapData( width+1 , height+1, true, 0x00000000 );
    var matrix:Matrix = new Matrix();
    matrix.translate( -rect.x , -rect.y );
    bitmap.draw( target , matrix );
    return bitmap;
}


Thanks for all the responses but none of these seemed to work for what I was trying to do, I can't say that isn't down to my inexperience though. This is what worked for me:

var ldr:Loader = new Loader;
ldr.loadBytes(snapshot);
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);

private function onComplete(e:Event):void {
    var loader:Loader = (e.target as LoaderInfo).loader;
trace(loader);
var bmp:Bitmap = Bitmap(loader.content);
addChild(bmp);
}

I was having trouble at first because I didn't realise the loader didn't dispatch the complete event but the loaderinfo.

Thanks for the help everyone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜