ActionScript 2.0屏幕捕获

时间:2015-01-11 13:03:31

标签: flash actionscript

我制作了一个flash场景,我想制作一个按钮,当按下该按钮时,会保存场景,或者将其中一部分保存为JPG。 我发现了一个很好的教程(http://www.screentime.com/software/flash-projector/docs/AS3-mApp-captureScreenToJP.htm),但是当我插入代码时:

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

......它没有做任何事情。

1 个答案:

答案 0 :(得分:0)

首先,我有两点评论:

  • 您的问题中提到的代码不是标准的ActionScript代码,您应该有一个特殊的应用程序来使代码正常工作。在你的情况下,它是ScreentimeMedia的mProjector,它将SWF转换为桌面应用程序,可以执行许多标准SWF文件无法进行屏幕截图并将其保存为图像文件的操作。

  • 您应该知道ActionScript 2无法将文件写入硬盘,当然除非是共享对象。因此,要获得场景的快照,您必须将图像发送到外部脚本"它可以将其保存为文件,例如PHP脚本。

要做到这一点,举个例子,我有3个按钮(btn_stage,btn_mc,btn_zone)和1个MovieClip(movie_clip),这里我假设你的swf在一个可以执行PHP脚本的web应用程序中:

ActionScript 2代码:

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代码:

<?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.';

?>

当然,这只是一个简单的例子,向您展示了一种做您正在寻找的方式。

希望可以提供帮助。