Android - 计算没有矩阵的像素旋转?并检查像素是否在 View 中

标签 android math matrix rotation

我希望有人能帮助我。我正在制作一个图像处理应用程序,我发现我需要一种更好的方法来加载大图像。

我的计划是遍历图像的“假设”像素(一个覆盖基本图像宽度/高度的“for 循环”,因此每次迭代代表一个像素),缩放/平移/旋转该像素的相对位置到 View ,然后使用此信息确定哪些像素正在 View 本身中显示,然后使用 BitmapRegionDecoder 和 BitmapFactory.Options 的组合仅加载输出实际需要的图像部分而不是完整的(即使缩放)图像。

到目前为止,我似乎已经正确地涵盖了图像的比例和翻译,但我似乎无法弄清楚如何计算旋转。因为它不是真正的位图像素,所以我不能使用 Matrix.rotate =( 这是 View onDraw 中的图像转换,imgPosX 和 imgPosY 保持图像的中心点:

m.setTranslate(-userImage.getWidth() / 2.0f, -userImage.getHeight() / 2.0f);
m.postScale(curScale, curScale);
m.postRotate(angle);
m.postTranslate(imgPosX, imgPosY);
mCanvas.drawBitmap(userImage.get(), m, paint);

到目前为止,这是我尝试确定屏幕上是否有图像像素的数学方法:

for(int j = 0;j < imageHeight;j++) {
    for(int i = 0;i < imageWidth;i++) {

        //image starts completely center in view, assume image is original size for simplicity
        //this is the original starting position for each pixel
        int x = Math.round(((float) viewSizeWidth / 2.0f) - ((float) newImageWidth / 2.0f) + i);
        int y = Math.round(((float) viewSizeHeight / 2.0f) - ((float) newImageHeight / 2.0f) + j);

        //first we scale the pixel here, easy operation
        x = Math.round(x * imageScale);
        y = Math.round(y * imageScale);

        //now we translate, we do this by determining how many pixels
        //our images x/y coordinates have differed from it's original
        //starting point, imgPosX and imgPosY in the view start in center
        //of view

        x = x + Math.round((imgPosX - ((float) viewSizeWidth / 2.0f)));
        y = y + Math.round((imgPosY - ((float) viewSizeHeight / 2.0f)));

        //TODO need rotation here

    }
}

所以,假设我的数学直到旋转是正确的(可能不正确,但到目前为止它似乎有效),那么我将如何计算从该像素位置开始的旋转?我试过其他类似的问题,例如:

Link 1

Link 2

Link 3

在不使用旋转的情况下,我希望实际出现在屏幕上的像素被表示出来(我制作了以 1 和 0 输出结果的文本文件,这样我就可以直观地表示屏幕上的内容),但是找到了公式在这些问题中,信息不是预期的。 (场景:我旋转了一张图片,所以只有左上角在 View 中可见。使用 Here 中的信息旋转像素,我应该会在图片的左上角看到一组 1 的三角形输出文件,但事实并非如此)

那么,如何在不使用 Android 矩阵的情况下计算旋转后的像素位置?但仍然得到相同的结果。

如果我把它完全搞砸了,我很抱歉 =( 任何帮助将不胜感激,这个项目已经进行了很长时间,我想最终完成大声笑

如果您需要更多信息,我会尽可能多地提供 =) 谢谢您的宝贵时间

我意识到这个问题特别困难,所以我会在 SO 允许的情况下尽快发布赏金。

最佳答案

您不需要创建自己的 Matrix,使用现有的 Matrix。 http://developer.android.com/reference/android/graphics/Matrix.html

您可以使用

将位图坐标映射到屏幕坐标
float[] coords = {x, y};
m.mapPoints(coords);
float sx = coords[0];
float sy = coords[1];

如果你想将屏幕映射到位图坐标,你可以创建逆矩阵

Matrix inverse = new Matrix(m);
inverse.inverse();
inverse.mapPoints(...)

我认为您的整体方法会很慢,因为从 Java 对 CU 进行像素操作会产生大量开销。正常绘制位图时,像素操作是在 GPU 上完成的。

关于Android - 计算没有矩阵的像素旋转?并检查像素是否在 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24543302/

相关文章:

android - 将图像发送到 Node 服务器并调用 OCR microsoft vision API

c# - 是由偏离水平面大于 45 度的两点组成的线

c++ - Opengl 相机和乘法矩阵

algorithm - 如何从单个集合中枚举组合的组合

python - 使用NumPy最小化此错误功能

c++ - 将 3x1 或 1x3 cv::Mat 转换为 cv::Point3d?

c - 在 C 中的字符串矩阵上重新分配空间

android - 在 android 上使用 protobuf 将数据发送到 WCF 服务(使用 protobuf.net)

java - Firebase 时间戳不检索 SimpleDateFormat 中的时间戳

android - 服务生命周期