在as3中将位图转换为矢量

时间:2013-10-24 15:56:50

标签: actionscript-3 flash vector bitmap

我一直在尝试将我从相机输入捕获的位图转换为矢量,然后将矢量点转换为数组以供日后使用。

但是我在Flash中找不到任何此类功能,因此我一直在尝试使用Corey O'Neil创建的“矢量化包”。这似乎工作,但无法删除我的位图上的白色背景(我正在拍摄带有一条线的纸张图片)。即使手动执行此操作并使背景透明仍然不会产生任何结果。

这是我的代码:

public class DocumentRich extends MovieClip{

    public var initBox2D:Main;
    var cam:Camera = Camera.getCamera(); 
    var vid:Video = new Video(420, 300); 
    var myVector:Vectorize;
    public function DocumentRich()
    {
        if(stage){
            initBox2D = new Main();
            addChild(initBox2D);
            //addChild(new movieMonitor());
        } 

        addEventListener(MouseEvent.CLICK, clickHandler, false, 0, true);
        StartAnim();


    }

    function StartAnim():void
    {
        //SET UP WORLD
        initBox2D.isDebugMode(false);
        initBox2D.mouseDragEnabled(true);
        Main.world.SetGravity(new b2Vec2(0,0));


        cam.setQuality(0, 100);
        cam.setMode(420, 300, 30, true);
        trace(cam.height);
        vid.attachCamera(cam); 
        addChild(vid);
        addChild(go);
    }

    function clickHandler(m:MouseEvent){
        trace(m.target.name);
        switch(m.target.name){
            case 'go':
                goButtonFun();
            break;

        }
    }

    function goButtonFun():void{
        var screenShot:BitmapData = new BitmapData(cam.width, cam.height); 
        var screenShot_image:Bitmap=new Bitmap(screenShot);
        screenShot.draw(vid) ;
        ReduceColors.reduceColors(screenShot, 0, true, false);
        screenShot.colorTransform(new Rectangle(0, 0, screenShot.width, screenShot.height), new ColorTransform(2,2,2,1) );

        ///// --------- MAY NOT NEED THIS ----------- ////////////
        for(var i:int = 0; i<screenShot.width; i++)
        {
            for(var j:int = 0; j<screenShot.height; j++)
            {
                if(screenShot.getPixel(i,j) == 0xffffff)
                {
                    var transparent:uint = 0x00000000;
                    screenShot.setPixel32(i, j, transparent);
                }
            }
        }

        myVector = new Vectorize(screenShot_image, 23, 2, 1, 3, 0xFFFFFF);
        myVector.addEventListener(Vectorize.INIT, traceStatus);
        myVector.addEventListener(Vectorize.DESPECKLE_START, traceStatus);
        myVector.addEventListener(Vectorize.DESPECKLE_COMPLETE, traceStatus);
        myVector.addEventListener(Vectorize.GROUPING_START, traceStatus);
        myVector.addEventListener(Vectorize.GROUPING_COMPLETE, traceStatus);
        myVector.addEventListener(Vectorize.EDGING_START, traceStatus);
        myVector.addEventListener(Vectorize.EDGING_COMPLETE, traceStatus);

        myVector.addEventListener(Vectorize.DESPECKLE_PROGRESS, traceProgress);
        myVector.addEventListener(Vectorize.GROUPING_PROGRESS, traceProgress);
        myVector.addEventListener(Vectorize.EDGING_PROGRESS, traceProgress);

        myVector.addEventListener(Vectorize.COMPLETE, showVector);
        myVector.vectorize();

        //addChild(screenShot_image);
        addChild(go);
        removeChild(vid);
        cam = null;
        vid.attachCamera(null);
        vid = null;
    }

    function traceStatus(event:Event):void
    {
        trace(event.type);
    }
    function traceProgress(event:Event):void
    {
        var progress:int = event.target.percentDone * 100;
        trace(event.type + ": " + progress + "%");
    }
    function showVector(event:Event):void
    {
        trace(event.type);
        addChild(myVector);
    }

}

有什么建议吗?

感谢。

更新 对不起任何困惑,基本上我想要一种方法来跟踪透明位图,并获得所述位图中的形状点的数组。像素数据的数组太大了...我想要一个矩形的4个点,不管它的大小......

1 个答案:

答案 0 :(得分:0)

显然你正在寻找BitmapData.getVector()。它可以从Flash player 10获得,所以你应该随意使用它。

编辑:在我重新阅读您的问题之后,我理解您希望将您的位图解析为矢量 - 并且矢量不是像素数组,而是某个行的开始和结束。然后是的,如果你能够调用threshold()以使线的像素为一种颜色,而另一种颜色的背景,则可以调用getColorBoundsRect()并查询矩形的所有4个角。具有该行颜色的那些是您的起点和终点坐标,存储它们。

相关问题