Note: The mouse has a moderate level of English. It may be difficult to write local translation (or not translate), but I hope you can learn something from this article: Haha. I hope you can correct the mistakes. For the PNG encoding mode, search for the PNG encoding in AS3 on the search engine! Author: kaourantin.net
I hope you have already used AS3-a strong programming language! As the advertisement word says: "Everything is possible." for AS3, this is true: especially when we come into contact with new classes, such as ByteArray and new data types, such as UINT and INT. This article provides a specific example for these features. The code of the program is not completely compiled by me. I just fixed some bugs in the original program. This is a pure PNG encoding tool, but we can feel that it can bring us powerful features: we only need to input a bitmapdata, the program will return bytes of PNG encoded data for us. Next we can do more, for example, transfer to our server for image processing. We used to compress the cumbersome data through zlib. Now, for AS3, this is really just a piece of cake!
The usage of this class is as follows. You only need to create a BitMapData class and use this class in the following ways:
Var myPNG: ByteArray = PNGEnc. encode (myBitmapData );
How about it! Is it very simple? Of course. We can make it work better through inheritance ~ Let's take a look at the class code to complete these jobs:
Import flash. geom .*;
Import flash. display .*;
Import flash. util .*;
Public class PNGEnc
{
Public static function encode (img: BitmapData): ByteArray
{
// Create ByteArray data for output
Var png: ByteArray = new ByteArray ();
// Write the PNG header file
Png. writeUnsignedInt (0x89504e47 );
Png. writeUnsignedInt (0x0D0A1A0A );
// Create an IHDR data block
Var IHDR: ByteArray = new ByteArray ();
IHDR. writeInt (img. width );
IHDR. writeInt (img. height );
IHDR. writeUnsignedInt (0x08060000 );
// 32-bit RGBA processing
IHDR. writeByte (0 );
WriteChunk (png, 0x49484452, IHDR );
// Create an IDAT data block
Var IDAT: ByteArray = new ByteArray ();
For (var I: int = 0; I {
// No filter
IDAT. writeByte (0 );
Var p: uint;
If (! Img. transparent)
{
& Nbsp
<