最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 网络设计学院 > Flash教程

用FLASH生成PNG效果

日期:11-16    来源:中国设计秀    作者:cnwebshow.com

注:耗子英语水平一般,可能有写地方翻译(或许称不上翻译)的驴唇不对马嘴,但是希望大家能从这篇文章中学到一点东西:)呵呵。有纰漏的地方希望大家指正。关于PNG的编码模式,请大家自己在搜索引擎上进行查找AS3中的PNG编码! 作者:kaourantin.net JlD中国设计秀
我希望各位已经使用过了AS3——给我带来了强烈震撼的编程语言!就象广告词中说的一样:“一切皆有可能”;对AS3来讲,真的是这样:)特别是在我们接触到新的类,比如ByteArray 和新的数据类型,比如UINT、INT。本文为这些特性提供了一个具体的实例,程序的代码并不完全是我所编写的,我只是修正了原来程序中的一些BUG。这是一个单纯的PNG编码工具,但我们可以感受到它能为我们带来的强大功能:我们只需要输入一个bitmapdata数据,程序会为我们返回已经进行完PNG编码的ByteArray数据。接下来我们可以做的更多,比如传送到我们的服务器,进行图片处理。原来我们要通过zlib进行烦琐的数据压缩,而现在,对AS3来说,这真的只是小菜一碟!JlD中国设计秀
JlD中国设计秀
这个类的具体用法如下,你只需要建立一个BitMapData类,然后通过以下方式使用本类就可以了:JlD中国设计秀

var myPNG:ByteArray = PNGEnc.encode(myBitmapData);JlD中国设计秀

怎么样!非常简单吧?当然。我们可以通过继承使它工作的更好~那么让我们一起来看一下完成这些工作的类代码:JlD中国设计秀

import flash.geom.*; JlD中国设计秀
import flash.display.*; JlD中国设计秀
import flash.util.*; JlD中国设计秀
public class PNGEnc JlD中国设计秀
{ JlD中国设计秀
  public static function encode(img:BitmapData):ByteArray JlD中国设计秀
  { JlD中国设计秀
    // 建立输出用ByteArray类型数据 JlD中国设计秀
    var png:ByteArray = new ByteArray(); JlD中国设计秀
    //写入PNG头文件 JlD中国设计秀
    png.writeUnsignedInt(0x89504e47); JlD中国设计秀
    png.writeUnsignedInt(0x0D0A1A0A); JlD中国设计秀
    // 建立IHDR数据块 JlD中国设计秀
    var IHDR:ByteArray = new ByteArray(); JlD中国设计秀
    IHDR.writeInt(img.width); JlD中国设计秀
    IHDR.writeInt(img.height); JlD中国设计秀
    IHDR.writeUnsignedInt(0x08060000); JlD中国设计秀
    // 32位RGBA的处理 JlD中国设计秀
    IHDR.writeByte(0); JlD中国设计秀
    writeChunk(png,0x49484452,IHDR); JlD中国设计秀
    // 建立IDAT数据块 JlD中国设计秀
    var IDAT:ByteArray= new ByteArray(); JlD中国设计秀
    for(var i:int=0;i < img.height;i++) JlD中国设计秀
    { JlD中国设计秀
        // no filter JlD中国设计秀
        IDAT.writeByte(0); JlD中国设计秀
        var p:uint; JlD中国设计秀
        if ( !img.transparent ) JlD中国设计秀
        { JlD中国设计秀
          for(var j:int=0;j < img.width;j++) JlD中国设计秀
          {    JlD中国设计秀
            p = img.getPixel(j,i); JlD中国设计秀
            IDAT.writeUnsignedInt(uint(((p&0xFFFFFF) << 8)|0xFF)); JlD中国设计秀
          } JlD中国设计秀
        } else { JlD中国设计秀
          for(var j:int=0;j < img.width;j++) JlD中国设计秀
          {  JlD中国设计秀
            p = img.getPixel32(j,i); JlD中国设计秀
            IDAT.writeUnsignedInt( uint(((p&0xFFFFFF) << 8)|(shr(p,24)))); JlD中国设计秀
          } JlD中国设计秀
        }  JlD中国设计秀
    } JlD中国设计秀
    IDAT.compress(); JlD中国设计秀
    writeChunk(png,0x49444154,IDAT); JlD中国设计秀
    // 建立IEND数据块 JlD中国设计秀
    writeChunk(png,0x49454E44,null); JlD中国设计秀
    // 返回PNG JlD中国设计秀
    return png; JlD中国设计秀
  } JlD中国设计秀
   JlD中国设计秀
  private static var crcTable:Array; JlD中国设计秀
  private static var crcTableComputed:Boolean = false; JlD中国设计秀
   JlD中国设计秀
  private static function writeChunk(png:ByteArray, type:uint, data:ByteArray) JlD中国设计秀
  {  JlD中国设计秀
    if (!crcTableComputed) JlD中国设计秀
    { JlD中国设计秀
        crcTableComputed = true; JlD中国设计秀
        crcTable = []; JlD中国设计秀
        for (var n:uint = 0;n < 256;n++) JlD中国设计秀
        {    JlD中国设计秀
          var c:uint = n; JlD中国设计秀
          for (var k:uint = 0;k < 8;k++) JlD中国设计秀
          {  JlD中国设计秀
            if (c & 1) JlD中国设计秀
            { JlD中国设计秀
                c = uint(uint(0xedb88320)^uint(c >>> 1)); JlD中国设计秀
            } else {  JlD中国设计秀
                c = uint(c >>> 1); JlD中国设计秀
            } JlD中国设计秀
          } JlD中国设计秀
          crcTable[n] = c; JlD中国设计秀
        } JlD中国设计秀
    } JlD中国设计秀
     JlD中国设计秀
    var len:uint = 0; JlD中国设计秀
    if (data != null) JlD中国设计秀
    { JlD中国设计秀
        len = data.length; JlD中国设计秀
    } JlD中国设计秀
     JlD中国设计秀
    png.writeUnsignedInt(len); JlD中国设计秀
    var p:uint = png.position; JlD中国设计秀
    png.writeUnsignedInt(type); JlD中国设计秀
    if ( data != null ) JlD中国设计秀
    { JlD中国设计秀
        png.writeBytes(data); JlD中国设计秀
    } JlD中国设计秀
     JlD中国设计秀
    var e:uint = png.position; JlD中国设计秀
    png.position = p; JlD中国设计秀
    var c:uint = 0xffffffff; JlD中国设计秀
     JlD中国设计秀
    for (var i:int = 0;i < (e-p);i++) JlD中国设计秀
    { JlD中国设计秀
        c = uint(crcTable[(c ^ png.readUnsignedByte())&uint(0xff)] ^ uint(c >>> 8)); JlD中国设计秀
    } JlD中国设计秀
     JlD中国设计秀
    c = uint(c^uint(0xffffffff)); JlD中国设计秀
    png.position = e; JlD中国设计秀
    png.writeUnsignedInt(c); JlD中国设计秀
  } JlD中国设计秀
}JlD中国设计秀