论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Flash AS教程
Tag:2.0,3.0菜鸟,游戏,,cs,技巧,源码,,文本,文字,函数,音乐,随机,拖拽,asp,access,xml,mc,视频教程

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

文章类别:Flash AS | 发表日期:2012-4-12 9:56:19

as3如何把2D图片分析成文本功能

    今天给大家介绍一篇在游戏开发中常用到的功能就是利用as3把2D图片分析成文本文本功能

下面我们来看看具体的操作过程与as实现代码:
    对于一个像素的任意点,可以得到他的灰度。灰度计算公式;
    而每一个文字也有其对应的明暗,比如实心的一个球,就要比空心的显得更暗,因为它更加的紧密,求文字明暗的方法

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);
        }
    }
 
 
}

视频教程列表
文章教程搜索
 
Flash AS推荐教程
Flash AS热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058