as3 屏幕截图教程(附源码及代码说明)
////////////////////////////////// as源码
//////////////////////////////////
import JPGEncoder;
//设置要截屏的范围
var jpg:BitmapData=new
BitmapData(stage.stageWidth,stage.stageHeight);//图片大小
jpg.draw(root);//截取整个舞台
var jpgCode:JPGEncoder=new
JPGEncoder(85);//设置编码器(图片品质)
var
jpgStream:ByteArray=jpgCode.encode(jpg);//对截取的图片进行编码
//和PHP交互
var url:URLRequestHeader=new URLRequestHeader("Content-type",
"application/octet-stream");
var request:URLRequest = new
URLRequest("download.php?name=jpg.jpg");
request.requestHeaders.push(url);
request.method = URLRequestMethod.POST;
request.data = jpgStream;
navigateToURL(request, "_blank");
////////////////////////////////// PHP源码
//////////////////////////////////
<?php
if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
// get bytearray
$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
// add headers for download dialog-box
header("=Content-Type: image/jpeg");
header("Content-Disposition: attachment;
filename=".$_GET["name"]);
echo $jpg;
}
?>
//////////////////////////////////
JPGEncoder源码 //////////////////////////////////
package {
import flash.geom.*;
import flash.display.*;
import flash.utils.*;
public class JPGEncoder
{
// Static
table initialization
private var ZigZag:Array =
[
0, 1, 5,
6,14,15,27,28,
2, 4,
7,13,16,26,29,42,
3,
8,12,17,25,30,41,43,
9,11,18,24,31,40,44,53,
10,19,23,32,39,45,52,54,
20,22,33,38,46,51,55,60,
21,34,37,47,50,56,59,61,
35,36,48,49,57,58,62,63
];
private var YTable:Array = new
Array(64);
private var UVTable:Array =
new Array(64);
private var fdtbl_Y:Array =
new Array(64);
private var fdtbl_UV:Array =
new Array(64);
private function
initQuantTables(sf:int):void
{
var
i:int;
var
t:Number;
var
YQT:Array = [
16, 11, 10, 16, 24, 40, 51, 61,
12, 12, 14, 19, 26, 58, 60, 55,
14, 13, 16, 24, 40, 57, 69, 56,
14, 17, 22, 29, 51, 87, 80, 62,
18, 22, 37, 56, 68,109,103, 77,
24, 35, 55, 64, 81,104,113, 92,
49, 64, 78, 87,103,121,120,101,
72, 92, 95, 98,112,100,103, 99
];
for (i = 0;
i < 64; i++) {
t = Math.floor((YQT[i]*sf+50)/100);
if (t < 1) {
t = 1;
} else if (t > 255) {
t = 255;
}
YTable[ZigZag[i]] = t;
}
var
UVQT:Array = [
17, 18, 24, 47, 99, 99, 99, 99,
18, 21, 26, 66, 99, 99, 99, 99,
24, 26, 56, 99, 99, 99, 99, 99,
47, 66, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99
];
for (i = 0;
i < 64; i++) {
t = Math.floor((UVQT[i]*sf+50)/100);
if (t < 1) {
t = 1;
} else if (t > 255) {
t = 255;
}
UVTable[ZigZag[i]] = t;
}
var
aasf:Array = [
1.0, 1.387039845, 1.306562965, 1.175875602,
1.0, 0.785694958, 0.541196100, 0.275899379
];
i = 0;
for (var
row:int = 0; row < 8; row++)
{
for (var col:int = 0; col < 8; col++)
{
fdtbl_Y[i] = (1.0 / (YTable [ZigZag[i]] * aasf[row] * aasf[col] *
8.0));
fdtbl_UV[i] = (1.0 / (UVTable[ZigZag[i]] * aasf[row] * aasf[col] *
8.0));
i++;