/**
*
* *---------------------------------------*
* | ***精确选择识别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;
}
}
}

  

精确选择识别png图片有像素的区域的更多相关文章

  1. 精确选择识别png图片有像素的区域(使用方法)

    /** * * *---------------------------------------* * | ***精确选择识别png图片有像素的区域*** | * *----------------- ...

  2. [ActionScript 3.0] 利用InteractivePNG.as类精确选择识别png图片有像素的区域

    用法:如果是把png直接导入flash转换成影片剪辑,只需在影片剪辑属性中勾选为ActionScript导出(x),并把基类里的flash.display.MovieClip替换成Interactiv ...

  3. Canvas识别相似图片

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  4. 以API方式调用C# dll,使用OneNote2013 sp1实现OCR识别本地图片

    http://www.cnblogs.com/Charltsing/p/OneNoteOCRAPI.html OneNote2013 OCR API调用使用说明2019.4.17 使用说明:1.安装干 ...

  5. 用Delphi直接获取bmp图片的像素

    用Delphi直接获取bmp图片的像素,并存储显示出.(此像素主要用在LED上显示).希望高手能给出代码啊!! function getImagePixels(f: string): Integer; ...

  6. iOS图片的伪裁剪(改变图片的像素值)

    0x00 原理 利用一张图片事先画好的图片(以下称为蒙板),盖在要被裁剪的的图片上,然后遍历蒙板上的像素点,修改被裁剪图片对应位置的像素的色值即可得到一些我们想要的不规则图片了(比如人脸) 0x01 ...

  7. Android实现OCR扫描识别数字图片之图片扫描识别

    [Android实例] Android实现OCR扫描识别数字图片之图片扫描识别 Android可以识别和扫描二维码,但是识别字符串呢? google提供了以下解决方案用的是原来HP的相关资料. 可以吧 ...

  8. win32加载图片获得像素值

    在写光栅渲染器时,需要加载图片获得像素以便进行纹理插值,试了几种方法发现下面这种比价简单,效率也可以接受 Texture2D是我自己定义的类,其中m_pixelBuffer是一个动态二维数组,每个元素 ...

  9. Android 从 Android 本地图库选择多个图片

    原文地址 本文说明如何从 Android 本地图库选择多个图片.作者考虑很多解决方案. 演示从 Android 本地图库选择多个图片,有两个方法可以实现从图库中选择多个图片: 用 Intent 获取多 ...

随机推荐

  1. cube.js 学习(十一)cube + gitbase 分析git 代码

    这个是一个简单的demo,使用gitbase+cube 分析git 仓库代码 需求 我们平时使用的gitlab,或者gogs 等git 仓库管理工具,有自己的管理强项,但是对于分析上可能就不是那么强大 ...

  2. 【树形DP】骑士

    骑士 题目描述 \(Z\)国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬. 最近发生了一件可怕的事情,邪恶的\(Y\)国发动了一场针对Z国的侵略 ...

  3. 49、Spark Streaming基本工作原理

    一.大数据实时计算介绍 1.概述 Spark Streaming,其实就是一种Spark提供的,对于大数据,进行实时计算的一种框架.它的底层,其实,也是基于我们之前讲解的Spark Core的. 基本 ...

  4. CF1220题解

    D 考虑从0出发,两个属于集合的元素\(x,y\) \(ax=by\),则形成奇环需要\(a+b\equiv 1(\% 2)\) 需要无奇环,\(\frac{lcm(x,y)}{x}+\frac{lc ...

  5. msyql8.0编译安装

    1.安装依赖 yum  -y install wget  cmake gcc gcc-c++ncurses  ncurses-devel  libaio-devel openssl openssl-d ...

  6. Intellij IDEA 智能补全的 10 个姿势,太牛逼了。。

    一年多前,栈长那时候刚从 Eclipse 转型 IDEA 成功,前面转了好多次,都是失败史,都是泪..后面我就在微信公众号 "Java技术栈" 写了这篇文章:Intellij ID ...

  7. Spring Cloud Feign踩坑记录(二)

    注意,以下的Feign遇到的坑,在高版本中有些已经修复. 某些项目由于历史包袱原因,无法进行全面升级,才需要修补这些坑. 1.启动报错:not annotated with HTTP method t ...

  8. Systemback制作大于4G的Ubuntu系统镜像

    1 安装Systemback 依此执行如下命令. sudo apt-get update sudo add-apt-repository ppa:nemh/systemback sudo apt-ge ...

  9. Tracking without bells and whistles

    Tracking without bells and whistles 2019-08-07 20:46:12 Paper: https://arxiv.org/pdf/1903.05625 Code ...

  10. 深度强化学习 之 运行环境 mujoco 报错 ERROR: GLEW initalization error: Missing GL version

    使用  mujoco环境  运行代码,报错 ERROR: GLEW initalization error: Missing GL version 一直无法解决,发现网址: https://blog. ...