zoukankan      html  css  js  c++  java
  • 精确选择识别png图片有像素的区域

    /**
     *
     * *---------------------------------------*
     * |  ***精确选择识别png图片有像素的区域***  |
     * *---------------------------------------*
     **
     * 编辑修改收录:fengzi(疯子、wu341、wgq341)
     *
     * 不会写代码,我是代码搬运工。
     *
     * 联系方式:QQ(493712833)。
     *
     * 随   笔: https://www.cnblogs.com/fengziwu/
     *
     * 版权协议:请自觉遵守LGPL协议,欢迎修改、复制、转载、传播给更多需要的人。
     * 免责声明:任何因使用此软件导致的纠纷与软件/程序开发者无关。
     * 日   期: 2019.05.08
     *
     */
     
    package fengzi.bmd
    {
    	import flash.display.Bitmap;
    	import flash.display.BitmapData;
    	import flash.display.MovieClip;
    	import flash.display.Sprite;
    	import flash.events.Event;
    	import flash.events.MouseEvent;
    	import flash.geom.Matrix;
    	import flash.geom.Point;
    	import flash.geom.Rectangle;
    
    
    	public class InteractivePNG extends MovieClip
    	{
    
    		// -== Public Properties ==-
    		public function get interactivePngActive():Boolean
    		{
    			return _interactivePngActive;
    		}
    
    
    		public function get alphaTolerance():uint
    		{
    			return _threshold;
    		}
    		public function set alphaTolerance(value : uint):void
    		{
    			_threshold = Math.min(255,value);
    		}
    
    		// Excluded from documentation for simplicity, a note is provided under disableInteractivePNG.
    
    		override public function set hitArea(value : Sprite):void
    		{
    			if (value!=null && super.hitArea==null)
    			{
    				disableInteractivePNG();
    			}
    			else if (value==null && super.hitArea!=null)
    			{
    				enableInteractivePNG();
    			}
    			super.hitArea = value;
    		}
    
    		// Excluded from documentation for simplicity, a note is provided under disableInteractivePNG.
    
    		override public function set mouseEnabled(enabled : Boolean):void
    		{
    			// indicates that mouse has entered clip bounds.
    			if (isNaN(_buttonModeCache)==false)
    			{
    				disableInteractivePNG();
    			}
    			super.mouseEnabled = enabled;
    		}
    
    		// -== Private Properties ==-
    
    
    		protected var _threshold:uint = 128;
    
    		protected var _transparentMode:Boolean = false;
    
    		protected var _interactivePngActive:Boolean = false;
    
    		protected var _bitmapHit:Boolean = false;
    
    		protected var _basePoint:Point;
    
    		protected var _mousePoint:Point;
    
    		protected var _bitmapForHitDetection:Bitmap;
    
    		protected var _buttonModeCache:Number = NaN;
    
    		// -== Public Methods ==-
    
    
    		public function InteractivePNG():void
    		{
    			super();
    			_basePoint = new Point();
    			_mousePoint = new Point();
    			enableInteractivePNG();
    		}
    
    
    		public function drawBitmapHitArea(event:Event=null):void
    		{
    			var isRedraw:Boolean = (_bitmapForHitDetection != null);
    			if (isRedraw)
    			{
    				try
    				{
    					removeChild(_bitmapForHitDetection);
    				}
    				catch (e:Error)
    				{
    				}
    			}
    			var bounds:Rectangle = getBounds(this);
    			var left:Number = bounds.left;
    			var top:Number = bounds.top;
    			var b:BitmapData = new BitmapData(bounds.width,bounds.height,true,0);
    			_bitmapForHitDetection = new Bitmap(b);
    
    			// (So that it is not a mystery if the displaylist is being inspected!)
    			_bitmapForHitDetection.name = "interactivePngHitMap";
    			_bitmapForHitDetection.visible = false;
    			var mx:Matrix = new Matrix();
    			mx.translate(-left, -top);
    			b.draw(this, mx);
    			addChildAt(_bitmapForHitDetection, 0);
    			_bitmapForHitDetection.x = left;
    			_bitmapForHitDetection.y = top;
    		}
    
    
    		public function disableInteractivePNG():void
    		{
    			deactivateMouseTrap();
    			removeEventListener(Event.ENTER_FRAME, trackMouseWhileInBounds);
    			try
    			{
    				removeChild(_bitmapForHitDetection);
    			}
    			catch (e:Error)
    			{
    			}
    			_bitmapForHitDetection == null;
    			super.mouseEnabled = true;
    			_transparentMode = false;
    			setButtonModeCache(true);
    			_bitmapHit = false;
    			_interactivePngActive = false;
    		}
    
    
    		public function enableInteractivePNG():void
    		{
    			disableInteractivePNG();
    			if (hitArea!=null)
    			{
    				return;
    			}
    			activateMouseTrap();
    			_interactivePngActive = true;
    		}
    
    		// -== Private Methods ==-
    
    
    		protected function activateMouseTrap():void
    		{
    
    			//useCapture=true, priority=high, weakRef=true
    			addEventListener(MouseEvent.ROLL_OVER, captureMouseEvent, false, 10000, true);
    			addEventListener(MouseEvent.MOUSE_OVER, captureMouseEvent, false, 10000, true);
    			addEventListener(MouseEvent.ROLL_OUT, captureMouseEvent, false, 10000, true);
    			addEventListener(MouseEvent.MOUSE_OUT, captureMouseEvent, false, 10000, true);
    			addEventListener(MouseEvent.MOUSE_MOVE, captureMouseEvent, false, 10000, true);
    		}
    
    
    		protected function deactivateMouseTrap():void
    		{
    			removeEventListener(MouseEvent.ROLL_OVER, captureMouseEvent);
    			removeEventListener(MouseEvent.MOUSE_OVER, captureMouseEvent);
    			removeEventListener(MouseEvent.ROLL_OUT, captureMouseEvent);
    			removeEventListener(MouseEvent.MOUSE_OUT, captureMouseEvent);
    			removeEventListener(MouseEvent.MOUSE_MOVE, captureMouseEvent);
    		}
    
    
    		protected function captureMouseEvent(event : Event):void
    		{
    			if (! _transparentMode)
    			{
    				if (event.type == MouseEvent.MOUSE_OVER || event.type == MouseEvent.ROLL_OVER)
    				{
    					// The buttonMode state is cached then disabled to avoid a cursor flicker
    					//buttonMode状态被缓存,然后被禁用以避免光标闪烁。
    					// at the movieclip bounds. Reenabled when bitmap is hit.
    					//在移动边界。点击位图时重新启用。
    					setButtonModeCache();
    					_transparentMode = true;
    					super.mouseEnabled = false;
    					// activates bitmap hit & exit tracking
    					//激活位图命中和退出跟踪
    					addEventListener(Event.ENTER_FRAME, trackMouseWhileInBounds, false, 10000, true);
    					// important: Immediate response, and sets _bitmapHit to correct state for event suppression.
    					//重要提示:立即响应,并将“位图命中”设置为事件抑制的正确状态。
    					trackMouseWhileInBounds();
    				}
    			}
    
    			if (! _bitmapHit)
    			{
    				event.stopImmediatePropagation();
    			}
    		}
    
    
    		protected function trackMouseWhileInBounds(event:Event=null):void
    		{
    			if (bitmapHitTest() != _bitmapHit)
    			{
    				_bitmapHit = ! _bitmapHit;
    
    				// Mouse is now on a nonclear pixel based on alphaTolerance. Reenable mouse events.
    				//鼠标现在位于基于alphatolerance的非清晰像素上。重新启用鼠标事件。
    				if (_bitmapHit)
    				{
    					deactivateMouseTrap();
    					setButtonModeCache(true, true);
    					_transparentMode = false;
    					// This will trigger rollOver & mouseOver events
    					//这将触发滚动和鼠标悬停事件
    					super.mouseEnabled = true;
    				}
    				else if (!_bitmapHit)
    				{
    					// Mouse is now on a clear pixel based on alphaTolerance. Disable mouse events but .
    					//鼠标现在位于基于alphatolerance的清晰像素上。禁用鼠标事件。
    					_transparentMode = true;
    
    					// This will trigger rollOut & mouseOut events
    					//这将触发卷展栏和鼠标输出事件
    					super.mouseEnabled = false;
    				}
    			}
    
    			// When mouse exits this MovieClip's bounds, end tracking & restore all.
    			//当鼠标退出movieclip的边界时,结束跟踪并全部恢复。
    			var localMouse:Point = _bitmapForHitDetection.localToGlobal(_mousePoint);
    			if (hitTestPoint( localMouse.x, localMouse.y)==false)
    			{
    				removeEventListener(Event.ENTER_FRAME, trackMouseWhileInBounds);
    				_transparentMode = false;
    				setButtonModeCache(true);
    				super.mouseEnabled = true;
    				activateMouseTrap();
    			}
    		}
    
    
    
    
    
    		protected function bitmapHitTest():Boolean
    		{
    			if (_bitmapForHitDetection==null)
    			{
    				drawBitmapHitArea();
    			}
    			_mousePoint.x = _bitmapForHitDetection.mouseX;
    			_mousePoint.y = _bitmapForHitDetection.mouseY;
    			return _bitmapForHitDetection.bitmapData.hitTest(_basePoint, _threshold, _mousePoint);
    		}
    
    
    		protected function setButtonModeCache(restore:Boolean=false, retain:Boolean=false):void
    		{
    			if (restore)
    			{
    				if (_buttonModeCache==1)
    				{
    					buttonMode = true;
    				}
    				if (! retain)
    				{
    					_buttonModeCache = NaN;
    				}
    				return;
    			}
    			_buttonModeCache = (buttonMode==true ? 1 : 0);
    			buttonMode = false;
    		}
    	}
    }
    

      

  • 相关阅读:
    蒙特卡洛采样、重要性采样
    伯努利分布和高斯分布下的最大似然估计、交叉熵
    对于分类问题的神经网络最后一层的函数:sigmoid、softmax与损失函数
    android 侧滑菜单
    安卓Animation类与xml制作动画
    LeetCode题解 #3 Longest Substring Without Repeating Characters
    GY89的使用
    使用GY89的BMP180模块获取温度和压强(海拔)
    STM32与PC机串口通讯
    STM32使用无源蜂鸣器演奏歌曲
  • 原文地址:https://www.cnblogs.com/fengziwu/p/10908764.html
Copyright © 2011-2022 走看看