AS3的png图片编码器

关于AS3的png图片编码器:
代码如下:
  1. /*
  2. Adobe Systems Incorporated(r) Source Code License Agreement
  3. Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved.
  4. Please read this Source Code License Agreement carefully before using
  5. the source code.
  6. Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive,
  7. no-charge, royalty-free, irrevocable copyright license, to reproduce,
  8. prepare derivative works of, publicly display, publicly perform, and
  9. distribute this source code and such derivative works in source or
  10. object code form without any attribution requirements. 
  11. The name "Adobe Systems Incorporated" must not be used to endorse or promote products
  12. derived from the source code without prior written permission.
  13. You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and
  14. against any loss, damage, claims or lawsuits, including attorney's
  15. fees that arise or result from your use or distribution of the source
  16. code.
  17. THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT
  18. ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
  19. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  ALSO, THERE IS NO WARRANTY OF
  21. NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT.  IN NO EVENT SHALL MACROMEDIA
  22. OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  25. OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF
  28. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. package com. adobe. images
  31. {
  32. import flash. geom.*;
  33. import flash. display. Bitmap;
  34. import flash. display. BitmapData;
  35. import flash. utils. ByteArray;
  36.  
  37. public class PNGEncoder
  38. {
  39.     public static function encode (img:BitmapData ):ByteArray {
  40.         // Create output byte array
  41.         var png:ByteArray = new ByteArray ( );
  42.         // Write PNG signature
  43.         png. writeUnsignedInt (0x89504e47 );
  44.         png. writeUnsignedInt (0x0D0A1A0A );
  45.         // Build IHDR chunk
  46.         var IHDR:ByteArray = new ByteArray ( );
  47.         IHDR. writeInt (img. width );
  48.         IHDR. writeInt (img. height );
  49.         IHDR. writeUnsignedInt (0x08060000 ); // 32bit RGBA
  50.         IHDR. writeByte ( 0 );
  51.         writeChunk (png,0x49484452,IHDR );
  52.         // Build IDAT chunk
  53.         var IDAT:ByteArray= new ByteArray ( );
  54.         for ( var i: int= 0;i < img. height;i++ ) {
  55.             // no filter
  56.             IDAT. writeByte ( 0 );
  57.             var p:uint;
  58.             var j: int;
  59.             if ( !img. transparent ) {
  60.                 for (j= 0;j < img. width;j++ ) {
  61.                     p = img. getPixel (j,i );
  62.                     IDAT. writeUnsignedInt (
  63.                         uint ( ( (p&0xFFFFFF ) << 8 )|0xFF ) );
  64.                 }
  65.             } else {
  66.                 for (j= 0;j < img. width;j++ ) {
  67.                     p = img. getPixel32 (j,i );
  68.                     IDAT. writeUnsignedInt (
  69.                         uint ( ( (p&0xFFFFFF ) << 8 )|
  70.                         (p>>> 24 ) ) );
  71.                 }
  72.             }
  73.         }
  74.         IDAT. compress ( );
  75.         writeChunk (png,0x49444154,IDAT );
  76.         // Build IEND chunk
  77.         writeChunk (png,0x49454E44, null );
  78.         // return PNG
  79.         return png;
  80.     }
  81.     private static var crcTable: Array;
  82.     private static var crcTableComputed: Boolean = false;
  83.     private static function writeChunk (png:ByteArray,
  84.             type:uint, data:ByteArray ): void {
  85.         if (!crcTableComputed ) {
  86.             crcTableComputed = true;
  87.             crcTable = [ ];
  88.             var c:uint;
  89.             for ( var n:uint = 0; n < 256; n++ ) {
  90.                 c = n;
  91.                 for ( var k:uint = 0; k < 8; k++ ) {
  92.                     if (c & 1 ) {
  93.                         c = uint (uint (0xedb88320 ) ^
  94.                             uint (c >>> 1 ) );
  95.                     } else {
  96.                         c = uint (c >>> 1 );
  97.                     }
  98.                 }
  99.                 crcTable [n ] = c;
  100.             }
  101.         }
  102.         var len:uint = 0;
  103.         if ( data != null ) {
  104.             len = data. length;
  105.         }
  106.         png. writeUnsignedInt (len );
  107.         var p:uint = png. position;
  108.         png. writeUnsignedInt ( type );
  109.         if ( data != null ) {
  110.             png. writeBytes ( data );
  111.         }
  112.         var e:uint = png. position;
  113.         png. position = p;
  114.         c = 0xffffffff;
  115.         for ( var i: int = 0; i < (e-p ); i++ ) {
  116.             c = uint (crcTable [
  117.                 (c ^ png. readUnsignedByte ( ) ) &
  118.                 uint (0xff ) ] ^ uint (c >>> 8 ) );
  119.         }
  120.         c = uint (c^uint (0xffffffff ) );
  121.         png. position = e;
  122.         png. writeUnsignedInt (c );
  123.     }
  124. }
  125. }

如何使用:

Flex Paint (需要Flash 9)


1、如图设置界面;
2、定义一个BitmapData 对象
   var bd:BitmapData = new BitmapData(canvas.width,canvas.height); 
3、将canvas中的像素拷贝到这个对象里
   bd.draw(canvas);
4、然后将BitmapData 转换成png格式的ByteArray 对象
5、将最后的png二进制传到服务器端保存.

转载于:https://www.cnblogs.com/FireYang/archive/2007/02/10/646839.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值