Bitmap.getpixel 读取的颜色不正确?

发布于 2024-12-18 02:31:14 字数 1775 浏览 3 评论 0原文

我正在尝试制作一个非常简单的平铺引擎。但是,使用 Bitmap.getpixel(x,y) 并不总是能正确匹配颜色。它似乎对 0xFFFFFFFF0xFF0000000xFF1896000xFF18FF00 表现良好,但对 有问题代码>0xFF186600。我尝试将其更改为多种不同的相似颜色,但它似乎仍然无法正确读取。我正在与一个简单的 switch 语句进行比较。这是我的方法的代码

public void LoadLevel(Canvas canvas, int levelName)
{

        Bitmap level = BitmapFactory.decodeResource(getResources(), levelName);
        Bitmap startTile = BitmapFactory.decodeResource(getResources(), R.drawable.starttile);

        canvas.drawColor(Color.WHITE);

        int drawX = 0;
        int drawY = 0;

        for(int y = 0; y < level.getHeight(); y++)
        {
            for(int x = 0; x < level.getWidth(); x++)
            {
                switch(level.getPixel(x, y))
                {
                    case 0xFF000000: break;

                    case 0xFFFFFFFF: 
                        canvas.drawBitmap(startTile, drawX, drawY, null); 
                        break;

                    case 0xFF189600: 
                        canvas.drawBitmap(startTile, drawX, drawY, null); 
                        break;

                    case 0xFF18FF00: 
                        canvas.drawBitmap(startTile, drawX, drawY, null); 
                        break;

                    case 0xFF186600: 
                        canvas.drawBitmap(startTile, drawX, drawY, null); 
                        break;
                }

                Log.d("Color", Integer.toString(level.getPixel(x, y)));

                drawX += 128;
            }
            drawX = 0;
            drawY += 128;
        }
}

根据日志,颜色是“-15177472”。我不确定实际上是什么颜色...所以我不确定 -15177472 == 0xFF186600

我做错了什么而没有获得像素?安卓会改变图像吗?我应该使用安全颜色吗?

I am trying to make a very simple tile engine. However, using the Bitmap.getpixel(x,y) is not always matching the color correctly. It seems to be doing fine with 0xFFFFFFFF, 0xFF000000, 0xFF189600, and 0xFF18FF00, but has a problem with 0xFF186600. I tried changing it to multiple different similar colors, but it still doesn't seem to be reading it correctly. I am comparing with a simple switch statement. Here is the code for my method

public void LoadLevel(Canvas canvas, int levelName)
{

        Bitmap level = BitmapFactory.decodeResource(getResources(), levelName);
        Bitmap startTile = BitmapFactory.decodeResource(getResources(), R.drawable.starttile);

        canvas.drawColor(Color.WHITE);

        int drawX = 0;
        int drawY = 0;

        for(int y = 0; y < level.getHeight(); y++)
        {
            for(int x = 0; x < level.getWidth(); x++)
            {
                switch(level.getPixel(x, y))
                {
                    case 0xFF000000: break;

                    case 0xFFFFFFFF: 
                        canvas.drawBitmap(startTile, drawX, drawY, null); 
                        break;

                    case 0xFF189600: 
                        canvas.drawBitmap(startTile, drawX, drawY, null); 
                        break;

                    case 0xFF18FF00: 
                        canvas.drawBitmap(startTile, drawX, drawY, null); 
                        break;

                    case 0xFF186600: 
                        canvas.drawBitmap(startTile, drawX, drawY, null); 
                        break;
                }

                Log.d("Color", Integer.toString(level.getPixel(x, y)));

                drawX += 128;
            }
            drawX = 0;
            drawY += 128;
        }
}

According to the log, the color is "-15177472". I am not sure what color that actually is though... So I am not sure if -15177472 == 0xFF186600

What am I doing incorrectly to not get the pixel? Is android changing the image? Are there safe colors I am suppose to use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍风燃霜 2024-12-25 02:31:14

-151774720xFF186900。请注意您要查找的 9 数字而不是 6

您从哪里获得这些预期值,以及如何加载位图?如果您在绘图程序中创建位图,然后将它们加载到 Android 应用程序中,您可能会发现它们被解压缩为 16bpp,在这种情况下,当将像素值转换回 32bpp 时,精度会有所损失 <代码>getPixel()。

-15177472 is 0xFF186900. Note the 9 digit instead of the 6 you were looking for.

Where are you getting these expected values from, and how are you loading the bitmaps? If you are creating the bitmaps in a drawing program, and then loading them in your Android app, you may find that they are being decompressed as 16bpp, in which case there will be some loss of accuracy when converting pixel values back to 32bpp for getPixel().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文