[ActionScript 3.0] AS3.0 模拟套索工具抠图的两种方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[ActionScript 3.0] AS3.0 模拟套索工具抠图的两种方法相关的知识,希望对你有一定的参考价值。

方法之一:遮罩法

package com.fylibs.tools
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.DisplayObject;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.geom.Rectangle;

    /**
     * @author:Frost.Yen
     * @E-mail:[email protected]
     * @create:    2016-6-30 上午11:46:23
     *
     */
    public class LassoTool2
    {
        private var _editor:Sprite;
        private var _outline:Sprite = new Sprite();
        private var _mask:Sprite = new Sprite();
        private var _cords:Vector.<Point>=new Vector.<Point>;
        private var _copyBmp:Bitmap = new Bitmap();
        private var _copyContainer:Sprite = new Sprite();
        private var _source:BitmapData;
        private var _curr:Sprite;
        public function LassoTool2(e:Sprite,source:BitmapData)
        {
            _editor = e;
            _source = source;
            _copyBmp.bitmapData = _source.clone();
            _copyContainer.addChild(_mask);
            _copyContainer.addChild(_copyBmp);
            _copyBmp.mask = _mask;
            _editor.addChild(_copyContainer);
            _editor.addChild(_outline);
            
            addEventListeners();
        }
        private function addEventListeners():void
        {
            _editor.addEventListener(MouseEvent.MOUSE_DOWN,onEditorDown);
        }
        private function onEditorDown(e:MouseEvent):void
        {
            _editor.addEventListener(MouseEvent.MOUSE_MOVE,onEditorMove);
            _editor.addEventListener(MouseEvent.MOUSE_UP,onEditorUp);
            _cords = Vector.<Point>([]);
            _cords.push(getPosition());
            
            _outline.graphics.clear();
            _outline.graphics.lineStyle(1,0xffffff,0.8);
            _outline.graphics.moveTo(_editor.mouseX,_editor.mouseY);
            
            _mask.graphics.clear();
            _mask.graphics.lineStyle(1,0xffffff,0.8);
            _mask.graphics.moveTo(_editor.mouseX,_editor.mouseY);
        }
        private function onEditorMove(e:MouseEvent):void
        {
            _cords.push(getPosition());
            _outline.graphics.lineTo(_editor.mouseX,_editor.mouseY);
        }
        private function onEditorUp(e:MouseEvent):void
        {
            _editor.removeEventListener(MouseEvent.MOUSE_MOVE,onEditorMove);
            _editor.removeEventListener(MouseEvent.MOUSE_UP,onEditorUp);
            _outline.graphics.lineTo(_cords[0].x,_cords[0].y);
            _mask.graphics.lineTo(_cords[0].x,_cords[0].y);
            //trace(_outline.getRect(_editor));
            
            fillSelection(_outline);
            fillSelection(_mask);
        
            var rect:Rectangle = _outline.getRect(_editor);
            var temp:BitmapData = new BitmapData(rect.width,rect.height,true,0);
            var matrix:Matrix = new Matrix();
            matrix.translate(-rect.x,-rect.y);
            temp.draw(_copyContainer,matrix);
            
            var sp:Sprite = new Sprite();
            var bmp:Bitmap = new Bitmap(temp);
            sp.x = rect.x;
            sp.y = rect.y;
            sp.addChild(bmp);
            _editor.addChild(sp);
            
            sp.addEventListener(MouseEvent.MOUSE_DOWN,onCutDown);
        }
        
        private function onCutDown(e:MouseEvent):void
        {
            e.stopImmediatePropagation();
            _curr = e.currentTarget as Sprite;
            _curr.startDrag();
            e.currentTarget.parent.stage.addEventListener(MouseEvent.MOUSE_UP,onCutUp)
        }
        private function onCutUp(e:MouseEvent):void
        {
            _curr.stopDrag();
            e.currentTarget.removeEventListener(MouseEvent.MOUSE_UP,onCutUp);
        }
        private function getPosition():Point
        {
            return new Point(_editor.mouseX,_editor.mouseY);
        }
        private function fillSelection(target:Sprite):void
        {
            target.graphics.beginFill(0xffffff);
            target.graphics.moveTo(_cords[0].x,_cords[0].y);
            for(var i:int=1;i<_cords.length;i++){
                target.graphics.lineTo(_cords[i].x,_cords[i].y);
            }
            target.graphics.lineTo(_cords[0].x,_cords[0].y);
            target.graphics.endFill();
        }
    }
}

方法之二:像素填充法

package com.fylibs.tools
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    
    /**
     * @author:Frost.Yen
     * @E-mail:[email protected]
     * @create:    2016-6-29 上午10:36:08
     *
     */
    public class LassoTool1 extends Object
    {
        private var _editor:Sprite;
        private var _outline:Sprite = new Sprite();
        private var _cords:Vector.<Point>=new Vector.<Point>;
        private var _mask:BitmapData;
        private var _source:BitmapData;
        private var _curr:Sprite;
        public function LassoTool1(e:Sprite,source:BitmapData)
        {
            _editor = e;
            _source = source;
            addEventListeners();
        }
        private function addEventListeners():void
        {
            _editor.addEventListener(MouseEvent.MOUSE_DOWN,onEditorDown);
        }
        private function onEditorDown(e:MouseEvent):void
        {
            _editor.addEventListener(MouseEvent.MOUSE_MOVE,onEditorMove);
            _editor.addEventListener(MouseEvent.MOUSE_UP,onEditorUp);
            _cords = Vector.<Point>([]);
            _cords.push(getPosition());
            _editor.addChild(_outline);
            _outline.graphics.clear();
            _outline.graphics.lineStyle(1,0xffffff,0.8);
            _outline.graphics.moveTo(_editor.mouseX,_editor.mouseY);
        }
        private function onEditorMove(e:MouseEvent):void
        {
            _cords.push(getPosition());
            _outline.graphics.lineTo(_editor.mouseX,_editor.mouseY);
        }
        private function onEditorUp(e:MouseEvent):void
        {
            _editor.removeEventListener(MouseEvent.MOUSE_MOVE,onEditorMove);
            _editor.removeEventListener(MouseEvent.MOUSE_UP,onEditorUp);
            _outline.graphics.lineTo(_cords[0].x,_cords[0].y);
            
            //trace(_outline.getRect(_editor));

            fillSelection(_outline);
            var rect:Rectangle = _outline.getRect(_editor);
            if(_mask){
                _mask.dispose();
            }
            _mask = new BitmapData(_editor.width,_editor.height,true,0xffffff);
            _mask.draw(_outline);
            
            //getDraws(_mask,rect,0xffffff);
            
            var bmpd:BitmapData = new BitmapData(rect.width,rect.height,true,0);
            setPixels(bmpd,rect,getPixels(_source,rect,getDraws(_mask,rect,0xffffffff)));
            
            var sp:Sprite = new Sprite();
            var bmp:Bitmap = new Bitmap(bmpd);
            sp.x = rect.x;
            sp.y = rect.y;
            sp.addChild(bmp);
            _editor.addChild(sp);
            
            sp.addEventListener(MouseEvent.MOUSE_DOWN,onCutDown);
        }
        
        private function onCutDown(e:MouseEvent):void
        {
            e.stopImmediatePropagation();
            _curr = e.currentTarget as Sprite;
            _curr.startDrag();
            e.currentTarget.parent.stage.addEventListener(MouseEvent.MOUSE_UP,onCutUp)
        }
        private function onCutUp(e:MouseEvent):void
        {
            _curr.stopDrag();
            e.currentTarget.removeEventListener(MouseEvent.MOUSE_UP,onCutUp);
        }
        private function getPosition():Point
        {
            return new Point(_editor.mouseX,_editor.mouseY);
        }
        private function fillSelection(target:Sprite):void
        {
            target.graphics.beginFill(0xffffff);
            target.graphics.moveTo(_cords[0].x,_cords[0].y);
            for(var i:int=1;i<_cords.length;i++){
                
                target.graphics.lineTo(_cords[i].x,_cords[i].y);
            }
            target.graphics.lineTo(_cords[0].x,_cords[0].y);
            target.graphics.endFill();
        }
        private function getDraws(source:BitmapData,rect:Rectangle,findColor:uint):Vector.<Point>
        {
            source.lock();
            var draws:Vector.<Point> = new Vector.<Point>;
            for(var i:int=0;i<rect.width;i++){
                for(var j:int=0;j<rect.height;j++){
                    if(source.getPixel32(i+rect.x,j+rect.y) == findColor){
                        draws.push(new Point(i+rect.x,j+rect.y));
                    }
                }
            }
            //trace(draws.length,findColor);
            source.unlock();
            return draws;
        }
        private function getPixels(source:BitmapData,rect:Rectangle,draws:Vector.<Point>):Vector.<Object>
        {
            source.lock();
            var objV:Vector.<Object> = new Vector.<Object>;
            for(var i:int=0;i<draws.length;i++){
                var obj:Object = {};
                obj.p = draws[i];
                obj.color = source.getPixel32(draws[i].x,draws[i].y);
                objV.push(obj);
            }
            source.unlock();
            return objV;
        }
        private function setPixels(target:BitmapData,rect:Rectangle,color:Vector.<Object>):BitmapData
        {
            target.lock();
            for(var i:int = 0;i<color.length;i++){
                target.setPixel32(color[i].p.x-rect.x,color[i].p.y-rect.y,color[i].color);
            }
            target.unlock();
            return target;
        }
    }
}

 

调用方法:

package
{
    import com.fylibs.tools.LassoTool1;
    import com.fylibs.tools.LassoTool2;
    
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLRequest;
    
    /**
     * @author:Frost.Yen
     * @E-mail:[email protected]
     * @create:    2016-6-29 上午10:40:54
     *
     */
    [SWF(width="1920",height="1080",backgroundColor="0xcccccc")]
    public class TestLasso extends Sprite
    {
        private var _editor:Sprite;
        private var _lassoTool:LassoTool2;
        [Embed(source="QQ图片20150811130307.jpg")] 
        private var Source:Class;
        public function TestLasso()
        {
            super();
            _editor = new Sprite();
            this.addChild(_editor);
            var bmp:Bitmap = new Source();
            _editor.addChild(bmp);
            if(!_lassoTool){
                _lassoTool = new LassoTool2(_editor,bmp.bitmapData);
            }
        }
    }
}

 

以上是关于[ActionScript 3.0] AS3.0 模拟套索工具抠图的两种方法的主要内容,如果未能解决你的问题,请参考以下文章

[ActionScript 3.0] AS3.0和AS2.0的相互通信

[ActionScript 3.0] AS3.0 马赛克效果

[ActionScript 3.0] AS3.0 生成xml方法之一

[ActionScript 3.0] AS3.0 模拟套索工具抠图的两种方法

ActionScript 3 TextManager ActionScript 3.0类

ActionScript 3 ActionScript 3.0算术运算符