ActionScript - BitmapData 上的 getPixel() 坐标与变换矩阵?
我用位图资源的位图数据填充了一个圆形。
我需要从中心旋转圆,因此我将位图添加到圆的中心,并使用矩阵变换将位图数据向上和向左移动,使其看起来居中。
现在我尝试使用 getPixel() 读取位图数据,但返回值已关闭,因为它返回位图数据的未转换位置而不是可见的转换后的位图数据。
Bitmap(Sprite(evt.currentTarget).getChildAt(0)).bitmapData.getPixel(evt.localX, evt.localY)
如何通过矩阵变换从位图中准确读取像素数据?
用代码更新。假设我的示例中的 redCircleData 颜色更丰富,并且值得检索像素数据。
var redCircleData:Sprite = new Sprite();
redCircleData.graphics.beginFill(0xFF0000, 1.0);
redCircleData.graphics.drawCircle(0, 0, 100);
redCircleData.graphics.endFill();
var matrix:Matrix = new Matrix();
matrix.tx = redCircleData.width / 2;
matrix.ty = redCircleData.height / 2;
var myBitmapData:BitmapData = new BitmapData(redCircleData.width, redCircleData.height, true, 0x00FFFFFF);
myBitmapData.draw(redCircleData, matrix);
var theBitmap:Bitmap = new Bitmap(myBitmapData, PixelSnapping.AUTO, true);
theBitmap.x -= matrix.tx;
theBitmap.y -= matrix.ty;
addChild(theBitmap);
i've filled a circle shape with the bitmap data of a bitmap asset.
i need to rotate the circle from the center, so i added the bitmap to the center of the circle and used a matrix transformation to shift the bitmapdata upward and leftward so it appears centered.
now i'm trying to read the bitmap data with getPixel()
but the return value is off since it is returning the untransformed position of the bitmap data instead of the visible transformed bitmapdata.
Bitmap(Sprite(evt.currentTarget).getChildAt(0)).bitmapData.getPixel(evt.localX, evt.localY)
how can i get an accurate reading of the pixel data from a bitmap with a matrix transformation?
updated with code. assume the redCircleData in my example is more colorful and something worth retrieving pixel data.
var redCircleData:Sprite = new Sprite();
redCircleData.graphics.beginFill(0xFF0000, 1.0);
redCircleData.graphics.drawCircle(0, 0, 100);
redCircleData.graphics.endFill();
var matrix:Matrix = new Matrix();
matrix.tx = redCircleData.width / 2;
matrix.ty = redCircleData.height / 2;
var myBitmapData:BitmapData = new BitmapData(redCircleData.width, redCircleData.height, true, 0x00FFFFFF);
myBitmapData.draw(redCircleData, matrix);
var theBitmap:Bitmap = new Bitmap(myBitmapData, PixelSnapping.AUTO, true);
theBitmap.x -= matrix.tx;
theBitmap.y -= matrix.ty;
addChild(theBitmap);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Matrix 类为您提供了一些非常方便的函数来处理您的问题:
Matrix.invert();
Matrix.transformPoint();
获取矩阵的逆矩阵(反转它的副本),然后用它变换你的 Point(e.localX,e.localY) 。使用转换后的点坐标作为 getPixel() 的参数。 BitmapData 只是内存中的 RGB 或 RGBA 像素块,其中 (0,0) 是左上角。您需要通过用于显示它的变换向后工作,以找到鼠标下方的位图的像素。这就是逆矩阵为你做的事情。
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3 /flash/geom/Matrix.html
The Matrix class gives you some very handy functions to deal with your problem:
Matrix.invert();
Matrix.transformPoint();
Get the inverse of your matrix (invert a copy of it) and then transform your Point(e.localX,e.localY) with it. Use the transformed point coords as your arguments to getPixel(). BitmapData is just a block of RGB or RGBA pixels in memory, where (0,0) is the upper left. You need the to work backward through the transform you use to display it to find the pixel of the bitmap that is under your mouse. That's what the inverted matrix does for you.
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/geom/Matrix.html
我不确定您想要做的事情是否可行(至少我从未尝试过,而且我的印象是您无法真正将转换应用于
BitmapData 对象,但我可能是错的)。但是,您可以使用您感兴趣的区域的快照创建一个新的
BitmapData
并从此新对象中读取像素。这样你就可以轻松实现你想要的。I'm not sure if what you're trying to do is possible (I've never tried it, at least, and I'm under the impression that you can't really apply a transformation to a
BitmapData
object, but I might be wrong). However, you could create a newBitmapData
with a snapshot of the region you're interested in and read the pixels from this new object. This way you could easily achieve what you want.