简体   繁体   中英

Get Pixels From Rotated Bitmap Android

I am working on a game in Android and I have Bitmap objects being drawn. They are rotated using the Matrix class. The problem I am having is being able to access the pixels of the rotated 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...

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. ie a small ctrclkwise rotation and you start at the top right index.

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). 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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