Screencapture and Save to JPG, Actionscript 2.0

十年热恋 提交于 2021-01-29 05:20:58

问题


I have created a flash file which capable of creating movie clips by clicking each of respected buttons. What I want to do is, after I positioning all of these movie clips created, I would like to save it into JPEG or PNG image file. I have created a save button and named it "save_page_btn". I tried to find tutorial using AS 2.0 but it is seems to no avail. I have no basic in AS 3.0. Can anyone help me to find this solution.

Thanks everyone!


回答1:


ActionScript 2 :

For ActionScript 2, you have to save your image using a server side script, like a PHP script which I used in my answer for this question.

ActionScript 3 :

For ActionScript 3, things are more easy because FileReference give us the ability to save a file directly to our machine.

So for you case, you want to save your image as a jpg or a png file, which you can do using the JPGEncoder and PNGEncoder included in the as3corelib library. You can download it from here and then include it in your project from : File > ActionScript Settings ... > Library Path and then press the Browse to SWC file tab and select your as3corelib.swc downloaded file. Then you can do like this :

// in my stage, I have 2 buttons : btn_save_jpg and btn_save_png, and a MovieClip : movie_clip

import com.adobe.images.JPGEncoder;
import com.adobe.images.PNGEncoder;

btn_save_jpg.addEventListener(MouseEvent.CLICK, save_img);
btn_save_png.addEventListener(MouseEvent.CLICK, save_img);

function save_img(e:MouseEvent):void {

    // verify which button is pressed using its name
    var is_jpg:Boolean = (e.currentTarget.name).substr(-3, 3) == 'jpg'; 
    // you can also write it : var is_jpg:Boolean = e.currentTarget === btn_save_jpg;   

    // create our BitmapData and draw within our movie_clip MovieClip
    var bmd_src:BitmapData = new BitmapData(movie_clip.width, movie_clip.height)
        bmd_src.draw(movie_clip);

    if(is_jpg){     
        // if it's the btn_save_jpg button which is pressed, so create our JPGEncoder instance
        // for the btn_save_png button, we don't need to create an instance of PNGEncoder 
        // because PNGEncoder.encode is a static function so we can call it directly : PNGEncoder.encode()
        var encoder:JPGEncoder = new JPGEncoder(90);
        // 90 is the quality of our jpg, 0 is the worst and 100 is the best
    }

    // get encoded BitmapData as a ByteArray
    var stream:ByteArray = is_jpg ? encoder.encode(bmd_src) : PNGEncoder.encode(bmd_src);

    // open the save dialog to save our image 
    var file:FileReference = new FileReference();
        file.save(stream, 'snapshot_' + getTimer() + (is_jpg ? '.jpg' : '.png'));

}

If you have a question about the AS3 ( or even the AS2 ) code, don't hesitate to ask it using the comments area.

Hope that can help.



来源:https://stackoverflow.com/questions/28011448/screencapture-and-save-to-jpg-actionscript-2-0

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