简体   繁体   English

从Android的旋转位图获取像素

[英]Get Pixels From Rotated Bitmap Android

I am working on a game in Android and I have Bitmap objects being drawn. 我正在使用Android开发游戏,并且正在绘制Bitmap对象。 They are rotated using the Matrix class. 使用Matrix类旋转它们。 The problem I am having is being able to access the pixels of the rotated Bitmap . 我遇到的问题是能够访问旋转的Bitmap的像素。 When I access them after rotation the pixels are still representing the unrotated version. 旋转后访问它们时,像素仍然代表未旋转的版本。 Does anyone have any ideas? 有人有什么想法吗? Maybe I can rotate the pixel array based on an arbitrary number of degrees? 也许我可以根据任意数量的角度旋转像素阵列? I have seen solutions that create a new bitmaps on the fly based on the newly rotated images, but I can't do this because of performance issues. 我已经看到了基于新旋转的图像即时创建新位图的解决方案,但是由于性能问题,我无法做到这一点。 Thanks! 谢谢!

Here is how I draw the bitmap currently (The bitmap is drawn using the matrix as its position)... 这是我当前绘制位图的方式(位图是使用矩阵作为其位置绘制的)...

canvas.drawBitmap(bitmapPlayer, positionMatrix, null);

Here is how I create the positionMatrix variable... 这是我创建positionMatrix变量的方法...

Matrix m = new Matrix();
m.postRotate(degrees, bitmapCenterX, bitmapCenterY);
positionMatrix.set(m);

Here is how I access the pixels currently (This accesses the pixel array I created below)... 这是我当前访问像素的方式(这将访问下面创建的像素数组)...

getPixel(x, y);

Here is how I build the pixel array that I have tried to modify... 这是我构建尝试修改的像素阵列的方法...

// Build pixel 2d array
pixelData = new int[bitmapPlayer.getWidth()][bitmapPlayer.getHeight()];

for(int x=0; x<bitmapPlayer.getWidth(); x++) {
    for(int y=0; y<bitmapPlayer.getHeight(); y++) {
        pixelData[x][y] = bitmapPlayer.getPixel(x, y);
    }
}

Here is how I have tried to manipulate the array... 这是我尝试操纵数组的方式...

public void rotatePixels(int degrees) {
    int[][] newPixelData = new int[bitmapPlayer.getWidth()][bitmapPlayer.getHeight()];

    double cos = Math.cos(Math.toRadians(degrees));
    double sin = Math.sin(Math.toRadians(degrees));

    for(int x=0; x<bitmapPlayer.getWidth(); x++) {
        for(int y=0; y<bitmapPlayer.getHeight(); y++) {
            if(pixelData[x][y] != 0) {
                int currentX = x + getTopLeftX();
                int currentY = y + getTopLeftY();

                int nextX = (int)((currentX * cos) - (currentY * sin));
                int nextY = (int)((currentX * sin) + (currentY * cos));

                if(nextX >= 0 && nextX < bitmapPlayer.getWidth() && nextY >= 0 && nextY < bitmapPlayer.getHeight()) {
                      newPixelData[nextX][nextY] = 1;
                }
            }
        }
    }

    this.pixelData = newPixelData;
}

Have you tried: 你有没有尝试过:

Matrix mat = new Matrix();
mat.postRotate(45);
Bitmap newBm = Bitmap.createBitmap(bitmapPlayer, 0, 0, bitmapPlayer.width(), bitmapPlayer.height(), mat, true);

and then accessing the pixels of the new Bitmap? 然后访问新位图的像素?

I think right now you just draw a rotated bitmap on the canvas, you're not actually rotating the bitmap 我想现在您只是在画布上绘制了一个旋转的位图,实际上并没有旋转该位图

EDIT: 编辑:

The way you do it in your orinigal post won't work because you start at (0,0) and work your way down the left column... Depending on the rotation degree you'd have to start at a different place and then go down a different column. 您在原始帖子中执行此操作的方式将不起作用,因为您从(0,0)开始并一直沿左列向下移动...根据旋转角度,您必须从另一个位置开始,然后往下走另一列。 ie a small ctrclkwise rotation and you start at the top right index. 也就是说,以ctrclkwise方式进行小旋转,您将从右上角的索引开始。

Bitmap bm = someImage;
int startx, starty;
int degree = rotateDegree % 360; // counterclockwise
if (degree >= 0 && degree < 90) {
    startx = bm.getWidth();
    starty = 0;
} else if (degree >= 90 && degree < 180) {
    startx = bm.getWidth();
    starty = bm.getWidth();
} else if (degree >= 180 && degree < 270) {
    startx = 0();
    starty = bm.getWidth();
} else {
    startx = 0;
    starty = 0;
}

And then try traversing the image that way, starting at (startx, starty) and adding or subtracting the correct angle (you can use some boolean to keep track of whether you're adding or subtracting from x or y respectively). 然后尝试以这种方式遍历图像,从(startx,starty)开始并添加或减去正确的角度(可以使用一些布尔值来跟踪分别是从x还是从y减去)。 Let me know if this works, and if it doesn't can you be a little more specific about where you think the problem might be? 让我知道这是否可行,如果不能奏效,您是否可以更具体地说明问题所在?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM