位图返回奇怪的颜色值

时间:2013-07-23 01:41:21

标签: android colors bitmap getpixel

我正在尝试将标准.jpeg图像传输到Color值数组中,这些值由Bitmap的getPixels方法存储为int值。我的代码将加载到图像中并将其数据发送到一维int数组,但是当我将它们与原始图像进行比较时,某些值没有任何意义。我知道图像正在被正确读取,因为我的程序将其打印到屏幕上。有谁知道为什么我的输出包含这些奇怪的值?

        /* Test */
    ImageView image = (ImageView) findViewById(R.id.imageView1);
    Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.testsmall);
    image.setImageBitmap(bMap);

    int[] pixels = new int[bMap.getHeight() * bMap.getWidth()];

    bMap.getPixels(pixels, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());

    int[][] colors2D = new int[bMap.getWidth()][bMap.getHeight()];
    Log.i("State", "Start");
    for (int x = 0; x < bMap.getWidth(); x++)
    {
        for (int y = 0; y < bMap.getHeight(); y++)
        {
            colors2D[x][y] = pixels[x + y * bMap.getWidth()];
            Log.i("Inside", "X: " + x + ", Y: " + y + ", Pixel: " + pixels[x + y * bMap.getWidth()]);
        }
        Log.i("Outside", "New Line");
    }
    Log.i("State", "End");
    /* End Test */

额外信息:

以下包含该部分程序的LogCat输出。奇怪的价值观在这里。 Android的Color类将White定义为-1,将Black定义为-16777216。

http://pastebin.com/GxF4j0ef

这是testSmall.jpg。你可以看到它很小。仅测量16x16。

https://www.dropbox.com/s/8hbbopoaozuu9ya/testSmall.jpg

1 个答案:

答案 0 :(得分:2)

事实证明,该图像是由Android O.S.缩放的。因为我把它放在drawable文件夹中。事实证明,如果您不想缩放图像,则应将其放在名为drawable-nodpi的文件夹中。下面的链接帮我解决了这个问题。

Bitmap getWidth returns wrong value