问题
is there a way for flash AS3 to take a snapshot of the frame and then save it as jpeg with out using classes
回答1:
Sure... try searching on google... there are tons of ways. That is assuming you are ok with the standard flash classes. If you literally mean no classes at all then good luck to you.
Here is one way of doing it. flash-php-upload
Basically the key points are you create a JPEG Encoder that will encode your image a JPEG (you could do a PNG if you want also) like this:
var jpgEncoder:JPGEncoder;
jpgEncoder = new JPGEncoder(90);
Next you encode the stage by first drawing its data to a bitmap, then ecoding that:
var bitmapData:BitmapData = new BitmapData(stage.width, stage.height);
bitmapData.draw(stage, new Matrix());
img = jpgEncoder.encode(bitmapData);
Now depending on what flash you are using you can do the following to prompt the user to save:
var file:FileReference = new FileReference();
file.save(img, "filename.jpg");
Or if you want to save it to the server you can do the following:
var sendHeader:URLRequestHeader = new URLRequestHeader("Content-type","application/octet-stream");
var sendReq:URLRequest = new URLRequest("path-to-php.php");
sendReq.requestHeaders.push(sendHeader);
sendReq.method = URLRequestMethod.POST;
sendReq.data = img;
var sendLoader:URLLoader;
sendLoader = new URLLoader();
sendLoader.addEventListener(Event.COMPLETE, imageSentHandler);
sendLoader.load(sendReq);
And then you need a PHP file with the following:
<?php
if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {
$filename = "filename.jpg";
$fp = fopen( $filename,"wb");
fwrite( $fp, $GLOBALS[ 'HTTP_RAW_POST_DATA' ] );
fclose( $fp );
echo "filename=".$filename."&base=".$_SERVER["HTTP_HOST"].dirname($_SERVER["PHP_SELF"]);
}
?>
Please be aware that I have not tested this code for errors but the general idea is correct and you should be able to use it and make the changes you need for your program.
来源:https://stackoverflow.com/questions/9404723/is-there-a-way-for-flash-as3-to-take-a-snapshot-of-a-frame-then-save-it-as-jpeg