Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android getPixels() possibly a silly mistake?

Tags:

android

bitmap

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

like image 713
Jay Avatar asked Dec 03 '22 07:12

Jay


1 Answers

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

like image 163
con_9 Avatar answered Jan 01 '23 16:01

con_9