Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从PNG图像的非透明像素创建区域_Java_Android - Fatal编程技术网

Java 从PNG图像的非透明像素创建区域

Java 从PNG图像的非透明像素创建区域,java,android,Java,Android,我需要从PNG图像中提取一个像素完美的区域来检测碰撞。我发现以下代码是一个解决方案: public static Region create(Image image) { Region region = new Region(); for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { if (Col

我需要从PNG图像中提取一个像素完美的区域来检测碰撞。我发现以下代码是一个解决方案:

public static Region create(Image image) {

    Region region = new Region();

    for (int i = 0; i < image.getWidth(); i++) {
        for (int j = 0; j < image.getHeight(); j++) {
            if (Color.alpha(image.getRGB(i, j)) == 255) {
                region.op(new Rect(i, j, 1, 1), Region.Op.UNION);
            }
        }
    }

    return region;

    }

冲突检测不起作用,当我从返回的区域调用方法
.isEmpty()
时,它返回true。知道为什么吗?

您可以在AndroidImage类中创建一个函数

public Region getTranspRegion()
{
    BitmapDrawable bitmapChecker = new BitmapDrawable(bitmap);
    Region transpRegion = bitmapChecker.getTransparentRegion();
    return transpRegion;
}
然后在你的主课上:

Region imageRegion = new Region(imageX, imageY, image.getWidth(), image.getHeight());
Region collisionRegion = new Region(imageRegion);
collisionRegion.op(image.getTranspRegion(), Op.DIFFERENCE);
比检查每个像素容易得多。我想你的问题可能是你调用了getRGB,这似乎不会给你Alpha值


我不知道如何处理带有两个区域的实际检测部分,不过…

学习如何构造Rect对象
Region imageRegion = new Region(imageX, imageY, image.getWidth(), image.getHeight());
Region collisionRegion = new Region(imageRegion);
collisionRegion.op(image.getTranspRegion(), Op.DIFFERENCE);