Flash技巧教程:as3把2D图片分析成文本文本功能

http://www.webjx.com/  2012-03-25 19:49:04  来源:网页教学网 

Webjx核心提示:一篇在游戏开发中常用到的功能就是利用as3把2D图片分析成文本文本功能,下面我们来看看具体的操作过程与as实现代码吧。

一篇在游戏开发中常用到的功能就是利用as3把2D图片分析成文本文本功能,下面我们来看看具体的操作过程与as实现代码吧。
对于一个像素的任意点,可以得到他的灰度。灰度计算公式
  • getR(color) * 0.299 + getG(color) * 0.587 + getB(color) * 0.114;

而每一个文字也有其对应的明暗,比如实心的一个球,就要比空心的显得更暗,因为它更加的紧密,求文字明暗的方法

private function getDensity(t:String):Number {
            var ttf:TextField = new TextField;
            ttf.autoSize = TextFieldAutoSize.LEFT;
            ttf.text = t;
 
 
            var bmd:BitmapData = new BitmapData(ttf.width, ttf.height, false, 0xffffff);
            bmd.draw(ttf);
            var c:int = 0;
            for (var x:int = 0; x < bmd.width;x++ ) {
                for (var y:int = 0; y < bmd.height;y++ ) {
                    if (bmd.getPixel(x,y)!=0xffffff) {
                        c++;
                    }
                }
            }
            return c / ttf.width / ttf.height;
        }

完整代码

package
 
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Rectangle;
    import flash.net.FileReference;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    import sliz.miniui.Button;
    import sliz.utils.UIUtils;
    /**
     * ...
     * @author lizhi
     */
 
    public class TestText extends Sprite
    {
        private var fonts:Array = [];
        private var tf:TextField;
 
 
        private var file:FileReference;
        private var loader:Loader;
        public function TestText()
        {
            tf = new TextField;
            var tfm:TextFormat = new TextFormat;
            tfm.size = 10;
            tfm.leading = -2;
            tf.defaultTextFormat = tfm;
            tf.autoSize = TextFieldAutoSize.LEFT;
            var text:String = " ";
            var min:int = 32;
            var max:int = 120;
            while (++min < max) {
                var c:String = String.fromCharCode(min);
                text += c;
            }
            text += "           .'"*#@%+-)(TiI?><~";
            for (var i:int = 0; i < text.length;i++ ) {
                c = text.charAt(i);
                fonts.push({d:getDensity(c),t:c});
            }
            fonts.sortOn("d", Array.NUMERIC|Array.DESCENDING);
 
 
            //[Embed(source = '123.png')]var ic:Class;
            //var bmd:BitmapData = (new ic as Bitmap).bitmapData;
            //render(bmd);
            addChild(tf);
 
 
            UIUtils.changeStage(stage);
 
 
            var explorer:Button = new Button("explorer image", 2, 20, this, browse);
 
 
            loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoad2);
        }
 
 
        private function render(bmd:BitmapData):void {
            tf.width = 1000;
            tf.height = 1000;
            var x:int = 0;
            var y:int = 0;
            tf.text = "";
            while (y < bmd.height) {
                while (x<bmd.width) {
                    var gray:Number = getGray(bmd.getPixel(x, y)) / 0xff;
                    var index:int = int((fonts.length - 1) * gray);
                    tf.appendText(fonts[index].t);
                    var r:Rectangle = tf.getCharBoundaries(tf.text.length - 1);
                    x += r.width;
                }
                tf.appendText("n");
                y = r.bottom
                x = 0;
            }
        }
 
 
        private function getDensity(t:String):Number {
            var ttf:TextField = new TextField;
            ttf.autoSize = TextFieldAutoSize.LEFT;
            ttf.text = t;
 
 
            var bmd:BitmapData = new BitmapData(ttf.width, ttf.height, false, 0xffffff);
            bmd.draw(ttf);
            var c:int = 0;
            for (var x:int = 0; x < bmd.width;x++ ) {
                for (var y:int = 0; y < bmd.height;y++ ) {
                    if (bmd.getPixel(x,y)!=0xffffff) {
                        c++;
                    }
                }
            }
            return c / ttf.width / ttf.height;
        }
 
 
        private function getR(color:uint):uint {
            return color << 8 >>> 24;
        }
        private function getG(color:uint):uint {
            return color << 16 >>> 24;
        }
        private function getB(color:uint):uint {
            return color << 24 >>> 24;
        }
        private function getGray(color:uint):uint {
            return getR(color) * 0.299 + getG(color) * 0.587 + getB(color) * 0.114;
        }
 
 
        private function browse(e:Event):void {
            file = new FileReference();
            file.browse();
            file.addEventListener(Event.SELECT, onSelect);
        }
 
 
        private function onSelect(e:Event):void {
            file.load();
            file.addEventListener(Event.COMPLETE, onLoad);
        }
 
 
        private function onLoad(e:Event):void {
 
 
            loader.loadBytes(file.data);
 
 
        }
 
 
        private function onLoad2(e:Event):void {
            var content:Bitmap = loader.contentLoaderInfo.content as Bitmap;
            render(content.bitmapData);
        }
    }
 
 
}

更多