论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Flash AS编程教程
Tag:2.0,3.0菜鸟,游戏,,cs,技巧,源码,,文本,文字,函数,音乐,随机,拖拽,asp,access,xml,mc,视频教程

PNGEncoder 实现图片数据保存

文章类别:Flash AS编程 | 发表日期:2008-10-6 18:13:15


用as3实现图片数据保存:

PNGEncoder.as
程序代码 程序代码
package
{
        import flash.geom.*;
        import flash.display.*;
        import flash.utils.*;
        public class PNGEncoder
        {

            public static function encode(img:BitmapData):ByteArray {
                // Create output byte array
                var png:ByteArray = new ByteArray();
                // Write PNG signature
                png.writeUnsignedInt(0x89504e47);
                png.writeUnsignedInt(0x0D0A1A0A);
                // Build IHDR chunk
                var IHDR:ByteArray = new ByteArray();
                IHDR.writeInt(img.width);
                IHDR.writeInt(img.height);
                IHDR.writeUnsignedInt(0x08060000); // 32bit RGBA
                IHDR.writeByte(0);
                writeChunk(png,0x49484452,IHDR);
                // Build IDAT chunk
                var IDAT:ByteArray= new ByteArray();
                for(var i:int=0;i < img.height;i++) {
                    // no filter
                    IDAT.writeByte(0);
                    var p:uint;
                    if ( !img.transparent ) {
                        for(var j:int=0;j < img.width;j++) {
                            p = img.getPixel(j,i);
                            IDAT.writeUnsignedInt(
                                uint(((p&0xFFFFFF) << 8)|0xFF));
                        }
                    } else {
                        for(var k:int=0;k < img.width;k++) {
                            p = img.getPixel32(k,i);
                            IDAT.writeUnsignedInt(
                                uint(((p&0xFFFFFF) << 8)|(p >>> 24)));
                        }
                    }
                }
                IDAT.compress();
                writeChunk(png,0x49444154,IDAT);
                // Build IEND chunk
                writeChunk(png,0x49454E44,null);
                // return PNG
                return png;
            }

            private static var crcTable:Array;
            private static var crcTableComputed:Boolean = false;

            private static function writeChunk(png:ByteArray,
                    type:uint, data:ByteArray) {
                if (!crcTableComputed) {
                    crcTableComputed = true;
                    crcTable = [];
                    for (var n:uint = 0; n < 256; n++) {
                        var c_1:uint = n;
                        for (var k:uint = 0; k < 8; k++) {
                            if (c_1 & 1) {
                                c_1 = uint(uint(0xedb88320) ^
                                    uint(c_1 >>> 1));
                            } else {
                                c_1 = uint(c_1 >>> 1);
                            }
                        }
                        crcTable[n] = c_1;
                    }
                }
                var len:uint = 0;
                if (data != null) {
                    len = data.length;
                }
                png.writeUnsignedInt(len);
                var p:uint = png.position;
                png.writeUnsignedInt(type);
                if ( data != null ) {
                    png.writeBytes(data);
                }
                var e:uint = png.position;
                png.position = p;
                var c:uint = 0xffffffff;
                for (var i:int = 0; i < (e-p); i++) {
                    c = uint(crcTable[
                        (c ^ png.readUnsignedByte()) &
                        uint(0xff)] ^ uint(c >>> 8));
                }
                c = uint(c^uint(0xffffffff));
                png.position = e;
                png.writeUnsignedInt(c);
            }
        }
}


swf的代码:
程序代码 程序代码
var url:String = "savePic.asp";

var bpd:BitmapData = new BitmapData(a_mc.width, a_mc.height);
bpd.draw(a_mc);
var dat:ByteArray = PNGEncoder.encode(bpd);


var request:URLRequest = new URLRequest(url);
request.contentType = "application/octet-stream";
request.data = dat;
request.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
loader.dataFormat=URLLoaderDataFormat.BINARY;
try {
    loader.load(request);
} catch (error:ArgumentError) {
    trace("An ArgumentError has occurred.");
} catch (error:SecurityError) {
    trace("A SecurityError has occurred.");
}



asp代码:
程序代码 程序代码
<%
dim strSaveFileName
dim strNow
strnow =replace(replace(replace(now(), ":", ""), "-", ""), " ", "")
if request.servervariables("REQUEST_METHOD")="POST" then '当前为post
FormSize=Request.TotalBytes
FormData=Request.BinaryRead(FormSize) '接收数据到FormData
strSaveFileName =strNow &".png"
set dr=CreateObject("Adodb.Stream")
dr.Mode=3
dr.Type=1
dr.Open
dr.Write(FormData)
dr.SaveToFile Server.MapPath(strSaveFileName),2
dr.Close
set dr=nothing
response.Write(strSaveFileName)
end if
%>


===========
这里还有jsp的代码:
程序代码 程序代码
<%@ page contentType="text/html; charset=utf-8" language="java"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%
int v;
String filePath = request.getRealPath(System.currentTimeMillis()+".jpg");
BufferedInputStream inputStream = new BufferedInputStream(request.getInputStream());
FileOutputStream outputStream = new FileOutputStream(new File(filePath));
byte [] bytes = new byte[1024];
while((v=inputStream.read(bytes))>0){
        outputStream.write(bytes,0,v);
}
outputStream.close();
inputStream.close();
%>


php的代码:
程序代码 程序代码
<?
$data = file_get_contents('php://input');

echo file_put_contents("2.png",$data);
?>


dot net 的代码:
程序代码 程序代码
Stream pngFile = Request.InputStream;
        int v;
        string path = Server.MapPath("Test.png");
         byte[] arrayByte = new byte[1024];

        // Create the new, empty data file.
        if (File.Exists(path))
        {
            Response.Write("already exists!");
            return;
        }
        FileStream fs = new FileStream(path, FileMode.CreateNew);
        while ((v = pngFile.Read(arrayByte, 0, 1024)) > 0)
        {
            fs.Write(arrayByte, 0, v);
        }
        fs.Close();


文件下载

实现png透明
只用在 new BitmapData()里加上最后一个参数赋值为0x00就可以了.
var bpd:BitmapData = new BitmapData(a_mc.width, a_mc.height,true,0x00);
视频教程列表
文章教程搜索
 
Flash AS推荐教程
Flash AS热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058