Category: Flash Preview mode: Common | Row form

I think this is a useful class for web application. just a simple example, you can encode a BitmapData object into Base64 string  by the encodeBase64 method and save it into database,  when you need to use the BitmapData, use the decodeBase64 method to decode Base64 string into BitmapData object.
 

package com.foxarc.images {  
 
    import flash.display.BitmapData;  
    import flash.geom.Rectangle;  
    import flash.utils.ByteArray;          
    import com.foxarc.util.Base64;  
      
    public class BitmapEncoder {  
          
        public static function encodeByteArray(data:BitmapData):ByteArray{  
            if(data == null){  
                throw new Error("data parameter can not be empty!");  
            }  
            var bytes:ByteArray = data.getPixels(data.rect);  
            bytes.writeShort(data.width);  
            bytes.writeShort(data.height);  
            bytes.writeBoolean(data.transparent);  
            bytes.compress();  
            return bytes;  
        }  
        public static function encodeBase64(data:BitmapData):String{  
            return Base64.encodeByteArray(encodeByteArray(data));  
        }  
          
        public static function decodeByteArray(bytes:ByteArray):BitmapData{  
            if(bytes == null){  
                throw new Error("bytes parameter can not be empty!");  
            }  
            bytes.uncompress();  
            if(bytes.length <  6){  
                throw new Error("bytes parameter is a invalid value");  
            }             
            bytes.position = bytes.length - 1;  
            var transparent:Boolean = bytes.readBoolean();  
            bytes.position = bytes.length - 3;  
            var height:int = bytes.readShort();  
            bytes.position = bytes.length - 5;  
            var width:int = bytes.readShort();  
            bytes.position = 0;  
            var datas:ByteArray = new ByteArray();            
            bytes.readBytes(datas,0,bytes.length - 5);  
            var bmp:BitmapData = new BitmapData(width,height,transparent,0);  
            bmp.setPixels(new Rectangle(0,0,width,height),datas);  
            return bmp;  
        }  
          
        public static function decodeBase64(data:String):BitmapData{              
            return decodeByteArray(Base64.decodeToByteArray(data));  
        }         
          
        public function BitmapEncoder() {  
            throw new Error("BitmapEncoder  is a static class!");  
        }  
          
    }  
      
}  

Tags: actionscript convert BitmapData ByteArray Base64

Categorize:Flash | Link | Comment: 0 | Read times: 5605

In a recently project I've needed some shape based hit detection methods in As 3.0. Here's a description of each of the methods.

ComplexHitTestObject

HitTest.complexHitTestObject( target1:DisplayObject, target2:DisplayObject, accuracy:Number );

Returns a Boolean depending on whether the 2 DisplayObjects passed as parameters are overlapping each other. The 3rd parameter specifies how accurate the test it. The higher the number, the greater the size the BitmapData is drawn internally, and therefore the scale at which the 2 DisplayObjects are drawn onto the BitmapData. The default for the accuracy parameter is 1. If you try and pass a value of 0 or less the method will thorw an error.

IntersectionRectangle

HitTest.intersectionRectangle( target1:DisplayObject, target2:DisplayObject );

Returns a Rectangle matching the co-ordinates where the boundaries of the 2 DisplayObjects passed as parameters overlap. This method does a simple test much like hitTestObject().

ComplexIntersectionRectangle

click here to see more...

Tags: actionscript HitTest method shape

Categorize:Flash | Link | Comment: 0 | Read times: 4064

Base64 encode and decode is very useful in both web application and desktop application. Here is the ActionScript 3 Base64 code.

//Base64.as 

package com.foxarc.util{  
    import flash.utils.ByteArray;  
    public class Base64 {  
        private static  const encodeChars:Array =   
        ['A','B','C','D','E','F','G','H',  
        'I','J','K','L','M','N','O','P',  
        'Q','R','S','T','U','V','W','X',  
        'Y','Z','a','b','c','d','e','f',  
        'g','h','i','j','k','l','m','n',  
        'o','p','q','r','s','t','u','v',  
        'w','x','y','z','0','1','2','3',  
        '4','5','6','7','8','9','+','/'];  
        private static  const decodeChars:Array =   
        [-1, -1, -1, -1, -1, -1, -1, -1,  
        -1, -1, -1, -1, -1, -1, -1, -1,  
        -1, -1, -1, -1, -1, -1, -1, -1,  
        -1, -1, -1, -1, -1, -1, -1, -1,  
        -1, -1, -1, -1, -1, -1, -1, -1,  
        -1, -1, -1, 62, -1, -1, -1, 63,  
        52, 53, 54, 55, 56, 57, 58, 59,  
        60, 61, -1, -1, -1, -1, -1, -1,  
        -1,  0,  1,  2,  3,  4,  5,  6,  
         7,  8,  9, 10, 11, 12, 13, 14,  
        15, 16, 17, 18, 19, 20, 21, 22,  
        23, 24, 25, -1, -1, -1, -1, -1,  
        -1, 26, 27, 28, 29, 30, 31, 32,  
        33, 34, 35, 36, 37, 38, 39, 40,  
        41, 42, 43, 44, 45, 46, 47, 48,  
        49, 50, 51, -1, -1, -1, -1, -1];  
        public static function encode(data:ByteArray):String {  
            var out:Array = [];  
            var i:int = 0;  
            var j:int = 0;  
            var r:int = data.length % 3;  
            var len:int = data.length - r;  
            var c:int;  
            while (i < len) {  
                c = data[i++] << 16 | data[i++] << 8 | data[i++];  
                out[j++] = encodeChars[c >> 18] + encodeChars[c >> 12 & 0x3f] + encodeChars[c >> 6 & 0x3f] + encodeChars[c & 0x3f];  
            }  
            if (r == 1) {  
                c = data[i++];  
                out[j++] = encodeChars[c >> 2] + encodeChars[(c & 0x03) << 4] + "==";  
            }  
            else if (r == 2) {  
                c = data[i++] << 8 | data[i++];  
                out[j++] = encodeChars[c >> 10] + encodeChars[c >> 4 & 0x3f] + encodeChars[(c & 0x0f) << 2] + "=";  
            }  
            return out.join('');  
        }  
        public static function decode(str:String):ByteArray {  
            var c1:int;  
            var c2:int;  
            var c3:int;  
            var c4:int;  
            var i:int;  
            var len:int;  
            var out:ByteArray;  
            len = str.length;  
            i = 0;  
            out = new ByteArray();  
            while (i < len) {  
                // c1  
                do {  
                    c1 = decodeChars[str.charCodeAt(i++) & 0xff];  
                } while (i < len && c1 == -1);  
                if (c1 == -1) {  
                    break;  
                }  
                // c2      
                do {  
                    c2 = decodeChars[str.charCodeAt(i++) & 0xff];  
                } while (i < len && c2 == -1);  
                if (c2 == -1) {  
                    break;  
                }  
                out.writeByte((c1 << 2) | ((c2 & 0x30) >> 4));  
                // c3  
                do {  
                    c3 = str.charCodeAt(i++) & 0xff;  
                    if (c3 == 61) {  
                        return out;  
                    }  
                    c3 = decodeChars[c3];  
                } while (i < len && c3 == -1);  
                if (c3 == -1) {  
                    break;  
                }  
                out.writeByte(((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2));  
                // c4  
                do {  
                    c4 = str.charCodeAt(i++) & 0xff;  
                    if (c4 == 61) {  
                        return out;  
                    }  
                    c4 = decodeChars[c4];  
                } while (i < len && c4 == -1);  
                if (c4 == -1) {  
                    break;  
                }  
                out.writeByte(((c3 & 0x03) << 6) | c4);  
            }  
            return out;  
        }  
    }  
}  

Tags: Base64 encoder decoder actionscript

Categorize:Flash | Link | Comment: 0 | Read times: 9824

looking for some flash charting components or libraries? want to create animated, compact, interactive and great looking charts? Following is a list of them supports majority of browsers

Google Chart API
Big company's product and not a component or library. It seems more simple than all above but the same useful. You can get a PNG-format image in response to a URL from Google Chart API. Line, bar, and pie charts can be generated. You can easily include a Chart API image in a webpage by embedding a URL within an <img> tag.

 Google-Chart-API

Open Flash Chart
Open Flash Chart is an open source project(a Flash charting component) started by John Glazebrook to provide flash charts that can be embedded in web pages.It is fairly easy to setup and has classes written in PHP, Perl, Python, Java, Ruby on Rails, and .Net to connect to the Chart. You can create some really nice looking Bar Charts, Pie Charts, Area Charts and etc. It isn’t as flashy as any of these products, but that tends to be a good thing for charting components.

open-flash-chart 

click here to see more...

Tags: Flash animated chart components

Categorize:Flash | Link | Comment: 0 | Read times: 434