Android getPixels() possibly a silly mistake?

余生颓废 提交于 2020-01-03 17:46:04

问题


Okay, this is quite simple to understand, but for some bizarre reason I can't get it working.. I've simplified this example from the actual code.

InputStream is = context.getResources().openRawResource(R.raw.someimage);
Bitmap bitmap = BitmapFactory.decodeStream(is);
try
{
    int[] pixels = new int[32*32];
    bitmap.getPixels(pixels, 0, 800, 0, 0, 32, 32);
}
catch(ArrayIndexOutOfBoundsException ex)
{
    Log.e("testing", "ArrayIndexOutOfBoundsException", ex);
}

Why on earth do I keep getting an ArrayIndexOutOfBoundsException? the pixels array is 32x32 and as far as I'm aware I'm correctly using getPixels. The image dimensions is 800x800 and I am attempting to retrieve a 32x32 section. The image is a 32-bit PNG which is being reported as ARGB-8888.

Any ideas? even if I'm being an idiot! I'm about to throw the keyboard out of the window :D


回答1:


You're overflowing the destination buffer because you're asking for a stride of 800 entries between rows.

http://developer.android.com/reference/android/graphics/Bitmap.html#getPixels%28int[],%20int,%20int,%20int,%20int,%20int,%20int%29




回答2:


use bitmap width as stride, in ur case 32

 bitmap.getPixels(pixels, 0, 32, 0, 0, 32, 32);

every row gap with 800 causes ur pixelarray to get out of bound

"I'm about to throw the keyboard out of the window " funny lol




回答3:


You getting OutOfBounds exception becacuse stride is applied to pixels array not to the original bitmap,so in your case you're trying to retrieve 32*800 pixels which doesn't fit into your array.



来源:https://stackoverflow.com/questions/7364188/android-getpixels-possibly-a-silly-mistake

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!