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


如何使用:

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二进制传到服务器端保存.

来自:http://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、付费专栏及课程。

余额充值