ActionScript 2.0 Screen Capture

2019-08-08 12:47发布

问题:

I made a flash scene, and I want to make a button that, when pressed, saves the scene, or a part of it as JPG. I found a nice tutorial (http://www.screentime.com/software/flash-projector/docs/AS3-mApp-captureScreenToJP.htm), but when I insert the code:

mApplication.captureScreenToJPG(fileName:screen.jpg[, x:100][, y:540][, width:100][, height:400]) : Void

...it doesn't do anything.

回答1:

To start, I have two remarks :

  • The code mentioned in your question is not a standard ActionScript code, you should have a special application to get that code working. In your case, It's the mProjector of ScreentimeMedia, which will convert the SWF to a desktop application that can do many things that a standard SWf file can't do like taking a screen shot and saving it as image file.

  • You should know that ActionScript 2 can not write files to hard disk, unless shared object of course. So, to get a snapshot of your scene, you have to send image to an external "script" which can save it as a file, like a PHP script for example.

To do that, take this example, where I have 3 buttons (btn_stage, btn_mc, btn_zone) and 1 MovieClip (movie_clip), and here I am assuming that your swf is within a web application which can execute a PHP script :

ActionScript 2 code :

var bmp_data:BitmapData;

btn_stage.onPress = function() {    
    // here we will take a snapshot of our scene
    // if you want take all the scene (with empty and blank areas), we should use Stage.width and Stage.height : 
    // bmp_data = new BitmapData(Stage.width, Stage.height);
    // if you want only take areas with object and shapes, ..., you can use _root dimensions :
    bmp_data = new BitmapData(_root._width, root._height);
    bmp_data.draw(_root);

    take_snapshot(bmp_data);    
} 
btn_mc.onPress = function() {
    // to take a snapshot only of a MovieClip in our Stage : 
    bmp_data = new BitmapData(movie_clip._width, movie_clip._height); 
    bmp_data.draw(movie_clip);

    take_snapshot(bmp_data);
} 
btn_zone.onPress = function() {
    // if we want to take a snapshot of a specific zone (area) in our Stage
    // we start by drawing all our Stage into a BitmapData object
    var bmp_data_temp:BitmapData = new BitmapData(Stage.width, Stage.height);
        bmp_data_temp.draw(_root);

    // then we copy only the specific area from it, here we will take a square of 100px starting from (100, 100) to (200, 200)
    bmp_data = new BitmapData(100, 100);
    bmp_data.copyPixels(bmp_data_temp, new Rectangle(100, 100, 100, 100), new Point(0, 0));

    take_snapshot(bmp_data);
}

function take_snapshot(bmp_data:BitmapData) {

    var pixels:Array = [];
    var w:Number = bmp_data.width;
    var h:Number = bmp_data.height;

    // get all pixels of our image (BitmapData)
    for (var i = 0; i<= w; i++) {
        for (var j = 0; j <= h; j++) {
            var tmp = bmp_data.getPixel(i, j).toString(16);
            pixels.push(tmp);
        }
    }
    var php_return:LoadVars = new LoadVars();
    var image_sender:LoadVars = new LoadVars();
        image_sender.image = pixels.toString();
        image_sender.width = w;
        image_sender.height = h;

        // send pixels, image width and height to our php script using a LoadVars to save our image
        image_sender.sendAndLoad('http://www.example.com/save_image.php', php_return, 'POST');

}

PHP code :

<?php

    $pixels = explode(',', $_POST['image']);
    $width = $_POST['width'];
    $height = $_POST['height'];

    // create our image 
    $image = imagecreatetruecolor($width, $height);

    // draw our image pixel by pixel
    $i = 0;
    for($x = 0; $x <= $width; $x++){
        for($y = 0; $y <= $height; $y++){
            $int = hexdec($pixels[$i++]);
            $color = ImageColorAllocate ($image, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
            imagesetpixel($image, $x, $y, $color);
        }
    }

    // save our image as snapshot.jpg 
    imagejpeg($image, 'snapshot.jpg');

    // frees our image from memory
    imagedestroy($image);

    // this message will be sent to our swf after saving the image
    echo 'your image was created successfully.';

?>

Of course this is just a simple example to show you a manner to do what you are looking for.

Hope that can help.