gpt4 book ai didi

java - 与 Bitmap.Config.ALPHA_8 的像素完美碰撞

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:54:20 26 4
gpt4 key购买 nike

我找到了这段像素完美碰撞检查代码,并在我的代码中使用了它:

public boolean isCollisionDetected(
Bitmap bitmap1, int x1, int y1,
Bitmap bitmap2, int x2, int y2
) {
Rect bounds1 = new Rect(
x1, y1, x1 + bitmap1.getWidth(), y1 + bitmap1.getHeight()
);
Rect bounds2 = new Rect(
x2, y2, x2 + bitmap2.getWidth(), y2 + bitmap2.getHeight()
);

if (Rect.intersects(bounds1, bounds2)) {
Rect collisionBounds = getCollisionBounds(bounds1, bounds2);
for (int i = collisionBounds.left; i < collisionBounds.right; i++) {
for (int j = collisionBounds.top; j < collisionBounds.bottom; j++) {
int bitmap1Pixel = bitmap1.getPixel(i - x1, j - y1);
int bitmap2Pixel = bitmap2.getPixel(i - x2, j - y2);
if (isFilled(bitmap1Pixel) && isFilled(bitmap2Pixel)) {
return true;
}
}
}
}
return false;
}

private Rect getCollisionBounds(Rect rect1, Rect rect2) {
int left = (int) Math.max(rect1.left, rect2.left);
int top = (int) Math.max(rect1.top, rect2.top);
int right = (int) Math.min(rect1.right, rect2.right);
int bottom = (int) Math.min(rect1.bottom, rect2.bottom);
return new Rect(left, top, right, bottom);
}

private boolean isFilled(int pixel) {
return pixel != Color.TRANSPARENT;
}

而且它运行完美,从来没有出现过任何问题。直到我将图像设置为 Bitmap.Config.ALPHA_8使用此代码(由于 RAM 问题):

private Bitmap convert(Bitmap bitmap, Bitmap.Config config) {
Bitmap convertedBitmap =
Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), config);
Canvas canvas = new Canvas(convertedBitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawBitmap(bitmap, 0, 0, paint);
return convertedBitmap;
}

为什么什么都不会发生,它确实在 isCollisionDetected 中,我已经检查过日志!但它不会检测碰撞...任何人都可以提供检查位图与 Bitmap.Config.ALPHA_8 像素完美碰撞的工作功能吗?

最佳答案

调用 getPixel()在具有 ALPHA_8 配置的位图上将始终返回零。这似乎是a bug .

您可以通过将每个位图的像素存储为字节数组来解决此问题:

byte[] pixelData = getPixels(convert(bitmap, Bitmap.Config.ALPHA_8));

...

public byte[] getPixels(Bitmap bmp) {
int bytes = bmp.getRowBytes() * bmp.getHeight();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
bmp.copyPixelsToBuffer(buffer);
return buffer.array();
}

您需要稍微修改一下碰撞检测函数:

public boolean isCollisionDetected(
byte[] pixels1, Bitmap bitmap1, int x1, int y1,
byte[] pixels2, Bitmap bitmap2, int x2, int y2
) {
Rect bounds1 = new Rect(
x1, y1, x1 + bitmap1.getWidth(), y1 + bitmap1.getHeight()
);
Rect bounds2 = new Rect(
x2, y2, x2 + bitmap2.getWidth(), y2 + bitmap2.getHeight()
);

if (Rect.intersects(bounds1, bounds2)) {
Rect collisionBounds = getCollisionBounds(bounds1, bounds2);
for (int i = collisionBounds.left; i < collisionBounds.right; i++) {
for (int j = collisionBounds.top; j < collisionBounds.bottom; j++) {
byte bitmap1Pixel = pixels1[((j - y1) * width1) + (i - x1)];
byte bitmap2Pixel = pixels2[((j - y2) * width2) + (i - x2)];
if (isFilled(bitmap1Pixel) && isFilled(bitmap2Pixel)) {
return true;
}
}
}
}
return false;
}

关于java - 与 Bitmap.Config.ALPHA_8 的像素完美碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29012066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com