Navigation

Previous

Archives

2007年5月5日 @ 11:24

AS3的png图片编码器

From: http://www.jamesward.com/wordpress/2006/08/16/flex-paint-flex-display-object-to-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二进制传到服务器端保存.

Share

Filed under: 资料 · Tags: flex  

Articles related:

The Flex Style Explorer is a Proud Papa  (2007-4-12 11:3:42)

Flex Effects Explorer  (2007-3-12 17:26:37)

Leave a Comment

◎welcome to give out your point。

Misc

Linkage