views:

2779

answers:

1

Is there any open source flash utility that i can embed on a webpage and use it to capture user webcam image or short duration clips and do "POST" to my server servlet ? i do not looking for streaming video and so i do not need red5 or flash server. can you folks elaborate ...?

+2  A: 

This sounds like a good candidate for Thibault Imbert's AS3 GIF Animation Encoding Class. I've used it about 2 years ago for a project at University. You can check it out here. In my opinion you would have 3 steps:

//1.Create a Camera object

//this code comes from the LiveDocs
var camera:Camera = Camera.getCamera();
var video:Video;            
if (camera != null) {
    video = new Video(camera.width * 2, camera.height * 2);
    video.attachCamera(camera);
    addChild(video);
} else {
    trace("You need a camera.");
}

//2. Take one or more 'screenshots' using BitmapData

    var screenshot:BitmapData = new BitmapData(video.width,video.height,false,0x009900);
    screenshot.draw(video);
    //you would probably save more of these in an array called screenshots maybe

//3. Create a GIFEncoder and send it to the server:



//assuming screenshots is an array of BitmapData objects previously saved
var animationEncoder:GIFEncoder = new GIFEncoder();
animationEncoder.setRepeat(0);
animationEncoder.setDelay (150);
animationEncoder.start();
for(var i:int = 1 ; i < screenshots.length ; i++){
    animationEncoder.addFrame(screenshots[i]);
}
animationEncoder.finish();
//save it on the server
var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream");//binary header
var gifRequest:URLRequest = new URLRequest ('http://yourServer/writeGIF.php?name=myFile.gif&amp;method=download');
gifRequest.requestHeaders.push (header);
gifRequest.method = URLRequestMethod.POST;
gifRequest.data = animationEncoder.stream;
sendToURL(gifRequest);



//Obviously you would have listeners to check if everything was ok, and when the operation //is complete.

//The PHP code would be something simple as this

    <?php

    $method = $_GET['method'];
    $name = $_GET['name'];

    if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {

        // get bytearray
        $gif = $GLOBALS["HTTP_RAW_POST_DATA"];

        // add headers for download dialog-box
        header('Content-Type: image/gif');
        header('Content-Length: '.strlen($gif ));
        header('Content-disposition:'.$method.'; filename="'.$name.'".gif');

        echo $gif ;

    }  else echo 'An error occured.';

    ?>

That should be it.

Make sure you check out Thibault Imbert's AS3 GIF Animation Encoding Class and this Web-Cam-Stop-Motion fun app :) Lee Felarca's SimpleFlvWriter is worth looking at as well, depending on your needs.

George Profenza
Do you think it is a good idea to convert flv into GIF then sending it with POST method?!! I think it is time and resource consuming. isn't it? I know the answer is complete for this question. But I want to know whether it is a good solution or not?
Morteza M.
@Morteza M. I don't think converting flv into GIF and sending it with POSt is a good idea. That is not what my answer states. What I say is this: for "short duration clips and do "POST" to my server servlet" you can get away with an animated GIF...no FLV. Otherwise, if having no sound, and a very short duration isn't suited for the needs, try the SimpleFLVWriter or something to bypass "do not need red5 or flash server". Red5 would be a more robust/flexible solution in general. The GIF option is, well...an option.
George Profenza