当前位置: 动力学知识库 > 问答 > 编程问答 >

bitmap - Android getPixels() possibly a silly mistake?

问题描述:

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

网友答案:

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

网友答案:

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

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: