惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
WordPress大学
WordPress大学
罗磊的独立博客
小众软件
小众软件
I
InfoQ
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hugging Face - Blog
Hugging Face - Blog
MyScale Blog
MyScale Blog
博客园 - 聂微东
Microsoft Security Blog
Microsoft Security Blog
H
Help Net Security
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园_首页
S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
P
Proofpoint News Feed
博客园 - 司徒正美
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
Jina AI
Jina AI
N
Netflix TechBlog - Medium

博客园 - FireYang

blog搬家 Flex的全局变量(原创翻译) 关于flex皮肤 设计模式 Adobe Flex 2: Training from the Source 电子书下载 欢迎加入RIA SEED ! 能移动的RichTextEdit - FireYang - 博客园 as 3.0一百个小技巧 有趣的自制卡通软件 Flex 2.0.1中css的新用法 flex 资源 Advanced ActionScript 3 with Design Patterns 北京记事:老刘的珍贵礼物 一个n的flex组件(SpringGraph Flex Component) flex中datagrid中的过滤功能 一些常用的javascript函数(方法) Apollo测试通知登记 ms的小技巧 actionscript代码练习作品
AS3的png图片编码器
FireYang · 2007-02-10 · via 博客园 - FireYang

关于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. public class PNGEncoder

  37. {

  38. public static function encode(img:BitmapData):ByteArray {

  39. // Create output byte array

  40. var png:ByteArray = new ByteArray();

  41. // Write PNG signature

  42.         png.writeUnsignedInt(0x89504e47);

  43.         png.writeUnsignedInt(0x0D0A1A0A);

  44. // Build IHDR chunk

  45. var IHDR:ByteArray = new ByteArray();

  46.         IHDR.writeInt(img.width);

  47.         IHDR.writeInt(img.height);

  48.         IHDR.writeUnsignedInt(0x08060000); // 32bit RGBA

  49.         IHDR.writeByte(0);

  50.         writeChunk(png,0x49484452,IHDR);

  51. // Build IDAT chunk

  52. var IDAT:ByteArray= new ByteArray();

  53. for(var i:int=0;i < img.height;i++) {

  54. // no filter

  55.             IDAT.writeByte(0);

  56. var p:uint;

  57. var j:int;

  58. if ( !img.transparent ) {

  59. for(j=0;j < img.width;j++) {

  60.                     p = img.getPixel(j,i);

  61.                     IDAT.writeUnsignedInt(

  62.                         uint(((p&0xFFFFFF) << 8)|0xFF));

  63. }

  64. } else {

  65. for(j=0;j < img.width;j++) {

  66.                     p = img.getPixel32(j,i);

  67.                     IDAT.writeUnsignedInt(

  68.                         uint(((p&0xFFFFFF) << 8)|

  69. (p>>>24)));

  70. }

  71. }

  72. }

  73.         IDAT.compress();

  74.         writeChunk(png,0x49444154,IDAT);

  75. // Build IEND chunk

  76.         writeChunk(png,0x49454E44,null);

  77. // return PNG

  78. return png;

  79. }

  80. private static var crcTable:Array;

  81. private static var crcTableComputed:Boolean = false;

  82. private static function writeChunk(png:ByteArray,

  83. type:uint, data:ByteArray):void {

  84. if (!crcTableComputed) {

  85.             crcTableComputed = true;

  86.             crcTable = [];

  87. var c:uint;

  88. for (var n:uint = 0; n < 256; n++) {

  89.                 c = n;

  90. for (var k:uint = 0; k < 8; k++) {

  91. if (c & 1) {

  92.                         c = uint(uint(0xedb88320) ^

  93.                             uint(c >>> 1));

  94. } else {

  95.                         c = uint(c >>> 1);

  96. }

  97. }

  98.                 crcTable[n] = c;

  99. }

  100. }

  101. var len:uint = 0;

  102. if (data != null) {

  103.             len = data.length;

  104. }

  105.         png.writeUnsignedInt(len);

  106. var p:uint = png.position;

  107.         png.writeUnsignedInt(type);

  108. if ( data != null ) {

  109.             png.writeBytes(data);

  110. }

  111. var e:uint = png.position;

  112.         png.position = p;

  113.         c = 0xffffffff;

  114. for (var i:int = 0; i < (e-p); i++) {

  115.             c = uint(crcTable[

  116. (c ^ png.readUnsignedByte()) &

  117.                 uint(0xff)] ^ uint(c >>> 8));

  118. }

  119.         c = uint(c^uint(0xffffffff));

  120.         png.position = e;

  121.         png.writeUnsignedInt(c);

  122. }

  123. }

  124. }

如何使用:

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